<commit_before>from datetime import datetime, timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Sum
from django.db.models.signals import post_save
from race.models import Map, Run, BestRun
from django_countries import CountryField


def generate_random_key():
	return User.objects.make_random_password(length=32)


class UserProfile(models.Model):
	user = models.OneToOneField(User, unique=True, related_name='profile')
	# TODO change token every password change
	api_token = models.CharField(max_length=32, default=generate_random_key,
		unique=True)
	registration_ip = models.IPAddressField(blank=True, null=True)
	last_connection_at = models.DateTimeField(auto_now_add=True)
	last_played_server = models.ForeignKey('race.Server', blank=True, null=True,
		related_name='players')
	#last_activity_at = models.DateTimeField(auto_now_add=True)
	#last_activity_ip = models.IPAddressField(blank=True, null=True)
	country = CountryField(blank=True)
	points = models.IntegerField(default=0)

	has_skin = models.BooleanField(default=False)
	skin_name = models.CharField(max_length=40, blank=True)
	skin_body_color = models.CharField(max_length=7, blank=True)
	skin_feet_color = models.CharField(max_length=7, blank=True)
	skin_body_color_raw = models.IntegerField(max_length=8, blank=True, null=True)
	skin_feet_color_raw = models.IntegerField(max_length=8, blank=True, null=True)

	@property
	def completions(self):
		return Run.objects.filter(user=self.user).count()

	@property
	def playtime(self):
		return Run.objects.filter(user=self.user).aggregate(
			Sum('time')
		)['time__sum']

	@property
	def position(self):
		return User.objects.exclude(profile__points__lte=0) \
			.exclude(is_active=False) \
			.filter(profile__points__gte=self.points) \
			.order_by('profile__points').count()

	def map_position(self, map_name):
		# first, retrieve players map points
		try:
			map_obj = Map.objects.get(name=map_name)
			player_points = BestRun.objects.get(map=map_obj, user=self.user).points
		except (Map.DoesNotExist, BestRun.DoesNotExist):
			return None
		# now, count all players with equal/greater point count
		return BestRun.objects.filter(map=map_obj).exclude(points__lte=0) \
			.exclude(user__is_active=False).filter(points__gte=player_points).count()

	def update_connection(self, server):
		self.last_connection_at = datetime.now()
		self.last_played_server = server
		self.save()

	def is_online(self):
		return self.last_connection_at >= datetime.now()-timedelta(minutes=10)

	def playing_on(self):
		return self.last_played_server if self.is_online() else None

	def best_score(self, map_id):
		try:
			map_obj = Map.objects.get(pk=map_id)
			return BestRun.objects.get(map=map_obj, user=self.user).run
		except (Map.DoesNotExist, BestRun.DoesNotExist):
			return None

	SKIN_LIST = (
		'bluekitty', 'bluestripe', 'brownbear', 'cammo',
		'cammostripes', 'coala', 'default', 'limekitty',
		'pinky', 'redbopp', 'redstripe', 'saddo', 'toptri',
		'twinbop', 'twintri', 'warpaint'
	)

	def get_skin(self):
		if not self.has_skin:
			return None
		return {
			'url': '{0}images/skins/{1}.png'.format(settings.MEDIA_URL, self.skin_name),
			'body_color': self.skin_body_color,
			'feet_color': self.skin_feet_color,
		}

	class Meta:
		get_latest_by = 'user__created_at'

	def __unicode__(self):
		possessive = '' if self.user.username.endswith('s') else 's'
		return u"{0}'{1} profile".format(self.user.username, possessive)

	def get_absolute_url(self):
		return self.user.get_absolute_url()


def post_user_save(instance, **kwargs):
	if kwargs['created']:
		# very special case
		if instance.id == 0:
			return
		profile = UserProfile(user=instance)
		profile.save()
<commit_msg>changed place where user api key is generated<commit_after>from datetime import datetime, timedelta
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.db.models import Sum
from django.db.models.signals import post_save
from race.models import Map, Run, BestRun
from django_countries import CountryField


def generate_random_key():
	return User.objects.make_random_password(length=32)


class UserProfile(models.Model):
	user = models.OneToOneField(User, unique=True, related_name='profile')
	# TODO change token every password change
	api_token = models.CharField(max_length=32, unique=True)
	registration_ip = models.IPAddressField(blank=True, null=True)
	last_connection_at = models.DateTimeField(auto_now_add=True)
	last_played_server = models.ForeignKey('race.Server', blank=True, null=True,
		related_name='players')
	#last_activity_at = models.DateTimeField(auto_now_add=True)
	#last_activity_ip = models.IPAddressField(blank=True, null=True)
	country = CountryField(blank=True)
	points = models.IntegerField(default=0)

	has_skin = models.BooleanField(default=False)
	skin_name = models.CharField(max_length=40, blank=True)
	skin_body_color = models.CharField(max_length=7, blank=True)
	skin_feet_color = models.CharField(max_length=7, blank=True)
	skin_body_color_raw = models.IntegerField(max_length=8, blank=True, null=True)
	skin_feet_color_raw = models.IntegerField(max_length=8, blank=True, null=True)

	@property
	def completions(self):
		return Run.objects.filter(user=self.user).count()

	@property
	def playtime(self):
		return Run.objects.filter(user=self.user).aggregate(
			Sum('time')
		)['time__sum']

	@property
	def position(self):
		return User.objects.exclude(profile__points__lte=0) \
			.exclude(is_active=False) \
			.filter(profile__points__gte=self.points) \
			.order_by('profile__points').count()

	def map_position(self, map_name):
		# first, retrieve players map points
		try:
			map_obj = Map.objects.get(name=map_name)
			player_points = BestRun.objects.get(map=map_obj, user=self.user).points
		except (Map.DoesNotExist, BestRun.DoesNotExist):
			return None
		# now, count all players with equal/greater point count
		return BestRun.objects.filter(map=map_obj).exclude(points__lte=0) \
			.exclude(user__is_active=False).filter(points__gte=player_points).count()

	def update_connection(self, server):
		self.last_connection_at = datetime.now()
		self.last_played_server = server
		self.save()

	def is_online(self):
		return self.last_connection_at >= datetime.now()-timedelta(minutes=10)

	def playing_on(self):
		return self.last_played_server if self.is_online() else None

	def best_score(self, map_id):
		try:
			map_obj = Map.objects.get(pk=map_id)
			return BestRun.objects.get(map=map_obj, user=self.user).run
		except (Map.DoesNotExist, BestRun.DoesNotExist):
			return None

	SKIN_LIST = (
		'bluekitty', 'bluestripe', 'brownbear', 'cammo',
		'cammostripes', 'coala', 'default', 'limekitty',
		'pinky', 'redbopp', 'redstripe', 'saddo', 'toptri',
		'twinbop', 'twintri', 'warpaint'
	)

	def get_skin(self):
		if not self.has_skin:
			return None
		return {
			'url': '{0}images/skins/{1}.png'.format(settings.MEDIA_URL, self.skin_name),
			'body_color': self.skin_body_color,
			'feet_color': self.skin_feet_color,
		}

	class Meta:
		get_latest_by = 'user__created_at'

	def __unicode__(self):
		possessive = '' if self.user.username.endswith('s') else 's'
		return u"{0}'{1} profile".format(self.user.username, possessive)

	def get_absolute_url(self):
		return self.user.get_absolute_url()


def post_user_save(instance, **kwargs):
	if kwargs['created']:
		# very special case
		if instance.id == 0:
			return
		profile = UserProfile(user=instance)
		profile.api_key = generate_random_key()
		profile.save()

{'fraction_non_alphanumeric': 0.12294758964928412, 'fraction_numerical': 0.005254170497832654, 'mean_word_length': 5.840970350404312, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2730907', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2613, 'n_words': 496}