<commit_before>import os
import datetime

from django.db import models
from django.db.models import Q
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist

from django.contrib.localflavor.us.models import PhoneNumberField

from components.floors.models import Floor
from components.makahiki_base import get_current_round
from components.prizes import POINTS_PER_TICKET

class InvalidRoundException(Exception):
  def __init__(self, value):
    self.value = value
    
  def __str__(self):
    return repr(self.value)
  
def _get_rounds():
  """Retrieves the rounds from the competition settings in a format that can be used in the ScoreboardEntry model."""
  
  return ((key, key) for key in settings.COMPETITION_ROUNDS.keys())

class ScoreboardEntry(models.Model):
  """Defines a class that tracks the user's scores in the rounds of the competition."""
  
  profile = models.ForeignKey("Profile", editable=False)
  round_name = models.CharField(max_length="30", choices=_get_rounds(), editable=False)
  points = models.IntegerField(default=0, editable=False)
  last_awarded_submission = models.DateTimeField(null=True, blank=True, editable=False)
  
  class Meta:
    unique_together = (("profile", "round_name",),)
    
  @staticmethod
  def user_round_overall_rank(user, round_name):
    entry, created = ScoreboardEntry.objects.get_or_create(
      profile=user.get_profile(), 
      round_name=round_name
    )
    
    # Check if the user has done anything.
    if entry.last_awarded_submission:
      return ScoreboardEntry.objects.filter(
          Q(points__gt=entry.points) | 
          Q(points=entry.points, last_awarded_submission__gt=entry.last_awarded_submission),
          round_name=round_name,
          profile__user__is_staff=False,
          profile__user__is_superuser=False,
      ).count() + 1
      
    # Users who have not done anything yet are assumed to be last.
    return ScoreboardEntry.objects.filter(
        points__gt=entry.points,
        round_name=round_name,
        profile__user__is_staff=False,
        profile__user__is_superuser=False,
    ).count() + 1
    
  @staticmethod
  def user_round_floor_rank(user, round_name):
    floor = user.get_profile().floor
    entry, created = ScoreboardEntry.objects.get_or_create(
      profile=user.get_profile(), 
      round_name=round_name
    )
    
    if entry.last_awarded_submission:
      return ScoreboardEntry.objects.filter(
          Q(points__gt=entry.points) | 
          Q(points=entry.points, last_awarded_submission__gt=entry.last_awarded_submission),
          profile__floor=floor,
          round_name=round_name,
      ).count() + 1
    else:
      return ScoreboardEntry.objects.filter(
          points__gt=entry.points,
          profile__floor=floor,
          round_name=round_name,
      ).count() + 1

def _get_available_themes():
  """Retrieves the available themes from the media folder."""
  
  theme_dir = os.path.join(settings.PROJECT_ROOT, "media", "css")
  # Returns a list of tuples representing the name of the theme and the directory of the theme
  return ((item, item) for item in os.listdir(theme_dir))
  
class Profile(models.Model):
  TEXT_CARRIERS = (
    ('att', 'AT&T'),
    ('sprint', 'Sprint'),
    ('tmobile', 'T-Mobile'),
    ('verizon', 'Verizon'),
  )
  
  user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
  name = models.CharField(_('name'), max_length=50)
  first_name = models.CharField(_('first_name'), max_length=50, null=True, blank=True)
  last_name = models.CharField(_('last_name'), max_length=50, null=True, blank=True)
  about = models.TextField(_('about'), null=True, blank=True)
  points = models.IntegerField(default=0, editable=False)
  last_awarded_submission = models.DateTimeField(null=True, blank=True, editable=False)
  floor = models.ForeignKey(Floor, null=True, blank=True)
  contact_email = models.EmailField(null=True, blank=True)
  contact_text = PhoneNumberField(null=True, blank=True)
  contact_carrier = models.CharField(max_length=50, choices=TEXT_CARRIERS, null=True, blank=True)
  enable_help = models.BooleanField(default=True)
  
  setup_profile = models.BooleanField(default=False, editable=False)
  setup_complete = models.BooleanField(default=False, editable=False)
  
  daily_visit_count = models.IntegerField(default=0, editable=False)
  last_visit_date = models.DateField(null=True, blank=True)
  
  def __unicode__(self):
      return self.user.username
  
  def get_absolute_url(self):
      return ('profile_detail', None, {'username': self.user.username})
  get_absolute_url = models.permalink(get_absolute_url)
  
  @staticmethod
  def points_leaders(num_results=10, round_name=None):
    """
    Returns the top points leaders out of all users.
    """
    if round_name:
      return Profile.objects.filter(
          scoreboardentry__round_name=round_name,
      ).order_by("-scoreboardentry__points", "-scoreboardentry__last_awarded_submission")[:num_results]
    
    return Profile.objects.all().order_by("-points", "-last_awarded_submission")[:num_results]
  
  def available_tickets(self):
    """
    Returns the number of raffle tickets the user has available.
    """
    total_tickets = self.points / POINTS_PER_TICKET
    allocated_tickets = self.user.raffleticket_set.count()
    
    return total_tickets - allocated_tickets
    
  def current_round_points(self):<commit_msg>Printed username instead of profile name in prizes, which is not good.<commit_after>import os
import datetime

from django.db import models
from django.db.models import Q
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ObjectDoesNotExist

from django.contrib.localflavor.us.models import PhoneNumberField

from components.floors.models import Floor
from components.makahiki_base import get_current_round
from components.prizes import POINTS_PER_TICKET

class InvalidRoundException(Exception):
  def __init__(self, value):
    self.value = value
    
  def __str__(self):
    return repr(self.value)
  
def _get_rounds():
  """Retrieves the rounds from the competition settings in a format that can be used in the ScoreboardEntry model."""
  
  return ((key, key) for key in settings.COMPETITION_ROUNDS.keys())

class ScoreboardEntry(models.Model):
  """Defines a class that tracks the user's scores in the rounds of the competition."""
  
  profile = models.ForeignKey("Profile", editable=False)
  round_name = models.CharField(max_length="30", choices=_get_rounds(), editable=False)
  points = models.IntegerField(default=0, editable=False)
  last_awarded_submission = models.DateTimeField(null=True, blank=True, editable=False)
  
  class Meta:
    unique_together = (("profile", "round_name",),)
    
  @staticmethod
  def user_round_overall_rank(user, round_name):
    entry, created = ScoreboardEntry.objects.get_or_create(
      profile=user.get_profile(), 
      round_name=round_name
    )
    
    # Check if the user has done anything.
    if entry.last_awarded_submission:
      return ScoreboardEntry.objects.filter(
          Q(points__gt=entry.points) | 
          Q(points=entry.points, last_awarded_submission__gt=entry.last_awarded_submission),
          round_name=round_name,
          profile__user__is_staff=False,
          profile__user__is_superuser=False,
      ).count() + 1
      
    # Users who have not done anything yet are assumed to be last.
    return ScoreboardEntry.objects.filter(
        points__gt=entry.points,
        round_name=round_name,
        profile__user__is_staff=False,
        profile__user__is_superuser=False,
    ).count() + 1
    
  @staticmethod
  def user_round_floor_rank(user, round_name):
    floor = user.get_profile().floor
    entry, created = ScoreboardEntry.objects.get_or_create(
      profile=user.get_profile(), 
      round_name=round_name
    )
    
    if entry.last_awarded_submission:
      return ScoreboardEntry.objects.filter(
          Q(points__gt=entry.points) | 
          Q(points=entry.points, last_awarded_submission__gt=entry.last_awarded_submission),
          profile__floor=floor,
          round_name=round_name,
      ).count() + 1
    else:
      return ScoreboardEntry.objects.filter(
          points__gt=entry.points,
          profile__floor=floor,
          round_name=round_name,
      ).count() + 1

def _get_available_themes():
  """Retrieves the available themes from the media folder."""
  
  theme_dir = os.path.join(settings.PROJECT_ROOT, "media", "css")
  # Returns a list of tuples representing the name of the theme and the directory of the theme
  return ((item, item) for item in os.listdir(theme_dir))
  
class Profile(models.Model):
  TEXT_CARRIERS = (
    ('att', 'AT&T'),
    ('sprint', 'Sprint'),
    ('tmobile', 'T-Mobile'),
    ('verizon', 'Verizon'),
  )
  
  user = models.ForeignKey(User, unique=True, verbose_name=_('user'))
  name = models.CharField(_('name'), max_length=50)
  first_name = models.CharField(_('first_name'), max_length=50, null=True, blank=True)
  last_name = models.CharField(_('last_name'), max_length=50, null=True, blank=True)
  about = models.TextField(_('about'), null=True, blank=True)
  points = models.IntegerField(default=0, editable=False)
  last_awarded_submission = models.DateTimeField(null=True, blank=True, editable=False)
  floor = models.ForeignKey(Floor, null=True, blank=True)
  contact_email = models.EmailField(null=True, blank=True)
  contact_text = PhoneNumberField(null=True, blank=True)
  contact_carrier = models.CharField(max_length=50, choices=TEXT_CARRIERS, null=True, blank=True)
  enable_help = models.BooleanField(default=True)
  
  setup_profile = models.BooleanField(default=False, editable=False)
  setup_complete = models.BooleanField(default=False, editable=False)
  
  daily_visit_count = models.IntegerField(default=0, editable=False)
  last_visit_date = models.DateField(null=True, blank=True)
  
  def __unicode__(self):
      return self.name
  
  def get_absolute_url(self):
      return ('profile_detail', None, {'username': self.user.username})
  get_absolute_url = models.permalink(get_absolute_url)
  
  @staticmethod
  def points_leaders(num_results=10, round_name=None):
    """
    Returns the top points leaders out of all users.
    """
    if round_name:
      return Profile.objects.filter(
          scoreboardentry__round_name=round_name,
      ).order_by("-scoreboardentry__points", "-scoreboardentry__last_awarded_submission")[:num_results]
    
    return Profile.objects.all().order_by("-points", "-last_awarded_submission")[:num_results]
  
  def available_tickets(self):
    """
    Returns the number of raffle tickets the user has available.
    """
    total_tickets = self.points / POINTS_PER_TICKET
    allocated_tickets = self.user.raffleticket_set.count()
    
    return total_tickets - allocated_tickets
    
  def current_round_points(self):
{'fraction_non_alphanumeric': 0.10144797997854844, 'fraction_numerical': 0.0033964962459778333, 'mean_word_length': 4.194521819870009, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5314476', 'n_tokens_mistral': 3515, 'n_tokens_neox': 3465, 'n_words': 738}