class Badge(models.Model): """Defines Badge model.""" THEME_CHOICES = ( ('1', "theme 1"), ('2', "theme 2"), ('3', "theme 3"), ('4', "theme 4"), ('5', "theme 5"), ) TRIGGER_CHOICES = ( ('daily', 'daily'), ('smartgrid', 'smartgrid'), ) name = models.CharField(max_length=255, help_text="The name of the badge") label = models.CharField(max_length=20, help_text="The label of the badge") description = models.CharField(max_length=255, help_text="The description of the badge") hint = models.CharField(max_length=255, help_text="The Hint of the badge") slug = models.CharField( max_length=255, help_text="A unique identifier of the badge. Automatically generated " "if left blank.") image = models.ImageField( max_length=255, blank=True, null=True, upload_to=media_file_path(_MEDIA_LOCATION), help_text="The image of the badge.", ) award_condition = models.CharField( max_length=1024, help_text="if the condition is True, the badge will be awarded. " + settings.PREDICATE_DOC_TEXT) award_trigger = models.CharField( max_length=10, choices=TRIGGER_CHOICES, default='daily', help_text="The trigger of evaluating the award condition.") theme = models.CharField(max_length=1, choices=THEME_CHOICES, default='6', help_text="The theme for the badge.") points = models.IntegerField(default=0, help_text="Points awarded for getting badge.") priority = models.IntegerField( default=0, help_text= "sorting order in the badge list, lower value (higher priority) appears first." ) admin_tool_tip = "Player Badges" def __unicode__(self): return self.name
class RafflePrize(models.Model): """RafflePrize model""" round = models.ForeignKey(RoundSetting, null=True, blank=True, help_text="The round of the raffle prize.") title = models.CharField(max_length=50, help_text="The title of the raffle prize.") value = models.IntegerField(help_text="The value of the raffle prize") description = models.TextField( help_text="Description of the raffle prize. Uses " \ "<a href='http://daringfireball.net/projects/markdown/syntax'>Markdown</a> " \ "formatting." ) image = models.ImageField(max_length=1024, upload_to=media_file_path(_MEDIA_LOCATION), blank=True, help_text="A picture of the raffle prize.") winner = models.ForeignKey( User, null=True, blank=True, help_text="The winner of the raffle prize. Normally, this is " "randomly picked by the system at the end of the round.") admin_tool_tip = "" def __unicode__(self): if self.round == None: return "%s: %s" % (self.round, self.title) else: return "%s: %s" % (self.round.name, self.title) def add_ticket(self, user): """Adds a ticket from the user if they have one. Throws an exception if they cannot add a ticket.""" ticket = RaffleTicket(raffle_prize=self, user=user) ticket.save() cache_mgr.delete('get_quests-%s' % user.username) def remove_ticket(self, user): """Removes an allocated ticket.""" # Get the first ticket that matches the query. ticket = RaffleTicket.objects.filter(raffle_prize=self, user=user)[0] ticket.delete() def allocated_tickets(self, user=None): """Returns the number of tickets allocated to this prize. Takes an optional argument to return the number of tickets allocated by the user.""" query = self.raffleticket_set if user: query = query.filter(user=user) return query.count()
class UploadImage(models.Model): """Defines the table for uploaded images.""" image = models.ImageField( upload_to=media_file_path(_MEDIA_LOCATION_UPLOAD), max_length=255, blank=True, null=True, help_text="The uploaded image.", ) def __unicode__(self): return self.image.name
class Sponsor(models.Model): """Defines the sponsor for this challenge.""" challenge = models.ForeignKey("ChallengeSetting") priority = models.IntegerField(default="1", help_text="The priority of the sponsor") name = models.CharField( help_text="The name of the sponsor.", max_length=200, ) url = models.CharField( help_text="The url of the sponsor.", max_length=200, ) logo_url = models.CharField( blank=True, null=True, help_text="The url of the sponsor logo.", max_length=200, ) logo = models.ImageField( upload_to=media_file_path(_MEDIA_LOCATION), max_length=255, blank=True, null=True, help_text="The logo of the sponsor.", ) class Meta: """meta""" ordering = [ 'priority', 'name', ] def __unicode__(self): return ""
class Action(models.Model): """Activity Base class.""" TYPE_CHOICES = ( ('activity', 'Activity'), ('commitment', 'Commitment'), ('event', 'Event'), ('filler', 'Filler'), ) RESOURCE_CHOICES = ( ('energy', 'Energy'), ('water', 'Water'), ('waste', 'Waste'), ) VIDEO_SOURCE_CHOICES = (('youtube', 'youtube'), ) users = models.ManyToManyField(User, through="ActionMember") name = models.CharField(max_length=20, help_text="The name of the action.") slug = models.SlugField( help_text= "A unique identifier of the action. Automatically generated if left blank.", unique=True, ) title = models.CharField(max_length=200, help_text="The title of the action.") image = models.ImageField( max_length=255, blank=True, null=True, upload_to=media_file_path(_MEDIA_LOCATION_ACTION), help_text= "Uploaded image for the activity. This will appear under the title when " "the action content is displayed.") video_id = models.CharField( null=True, blank=True, max_length=200, help_text= "The id of the video (optional). Currently only YouTube video is supported. " "This is the unique id of the video as identified by the YouTube video url." ) video_source = models.CharField(null=True, blank=True, max_length=20, choices=VIDEO_SOURCE_CHOICES, help_text="The source of the video.") embedded_widget = models.CharField( null=True, blank=True, max_length=50, help_text="The name of the embedded widget (optional).") description = models.TextField( help_text="The discription of the action. " + settings.MARKDOWN_TEXT) type = models.CharField(max_length=20, choices=TYPE_CHOICES, help_text="The type of the actions.") pub_date = models.DateField( default=datetime.date.today(), verbose_name="Publication date", help_text="Date from which the action will be available for users.") expire_date = models.DateField( null=True, blank=True, verbose_name="Expiration date", help_text="Date after which the action will be marked as expired.") unlock_condition = models.CharField( max_length=400, null=True, blank=True, help_text="if the condition is True, the action will be unlocked. " + settings.PREDICATE_DOC_TEXT) unlock_condition_text = models.CharField( max_length=400, null=True, blank=True, help_text= "The description of the unlock condition. It will be displayed to players when " "the lock icon is clicked.") related_resource = models.CharField( max_length=20, null=True, blank=True, choices=RESOURCE_CHOICES, help_text="The resource type this action related.") social_bonus = models.IntegerField(default=0, help_text="Social bonus point value.") point_value = models.IntegerField( default=0, help_text="The point value to be awarded.") def get_classname(self): """Returns the classname.""" return self._meta.module_name def __unicode__(self): return "%s: %s" % (self.type.capitalize(), self.title) def get_action(self, action_type): """Returns the concrete action object by type.""" return action_type.objects.get(action_ptr=self.pk) def admin_link(self): """returns the hardcoded link to edit the action.""" return "<a href='/challenge_setting_admin/smartgrid/%s/%s/'>%s</a>" % \ (self.type, self.pk, self.name)
class Team(models.Model): """Represents the team that a player belongs to.""" group = models.ForeignKey(Group, help_text="The group this team belongs to.") name = models.CharField(help_text="The team name", max_length=50) size = models.IntegerField( null=True, blank=True, default=0, help_text="The size of the team. It is the total number of " "residents in the team. Non-zero value will be used to " "normalize the team total score and participation rate.") logo = models.ImageField( max_length=1024, blank=True, null=True, upload_to=media_file_path(_MEDIA_LOCATION), help_text="The logo of the team.", ) admin_tool_tip = "The team a player belongs to. Teams are required." def __unicode__(self): return self.name def current_round_rank(self): """Gets the ranking of this team during the current round.""" current_round = challenge_mgr.get_round_name() return score_mgr.team_rank(self, round_name=current_round) def rank(self, round_name=None): """Returns the rank of the team across all groups.""" return score_mgr.team_rank(self, round_name=round_name) def current_round_points(self): """Returns the number of points for the current round.""" current_round = challenge_mgr.get_round_name() return score_mgr.team_points(self, round_name=current_round) def points(self, round_name=None): """Returns the total number of points for the team. Optional parameter for a round.""" return score_mgr.team_points(self, round_name) def points_leaders(self, num_results=None, round_name=None): """Gets the individual points leaders for the team.""" entries = score_mgr.player_points_leaders_in_team( self, num_results, round_name) if entries: return entries else: return None def save(self, *args, **kwargs): """Custom save method.""" super(Team, self).save(*args, **kwargs) # also create the resource goal settings from apps.widgets.resource_goal.models import EnergyGoalSetting, WaterGoalSetting if not EnergyGoalSetting.objects.filter(team=self): EnergyGoalSetting(team=self, wattdepot_source_name=self.name).save() if not WaterGoalSetting.objects.filter(team=self): WaterGoalSetting(team=self).save() cache_mgr.clear() class Meta: """Meta""" ordering = ("group", "name")
class ChallengeSetting(models.Model): """Defines the global settings for the challenge.""" THEME_CHOICES = ((key, key) for key in settings.INSTALLED_THEMES) domain = models.CharField( default="localhost", help_text="The domain name of this challenge.", max_length=100, ) logo = models.ImageField( upload_to=media_file_path(_MEDIA_LOCATION), max_length=255, blank=True, null=True, help_text="The logo of the challenge.", ) name = models.CharField( default="Kukui Cup", help_text="The name of the challenge.", max_length=50, ) theme = models.CharField( default="theme-forest", help_text="The default theme for this challenge.", choices=THEME_CHOICES, max_length=50, ) team_label = models.CharField( default="Team", help_text="The display label for 'teams'.", max_length=50, ) # CAS settings use_cas_auth = models.BooleanField(default=False, help_text="Use CAS authentication ?") cas_server_url = models.CharField( null=True, blank=True, help_text="The URL for CAS authentication service. " \ "Example: https://login.its.hawaii.edu/cas/", max_length=100,) cas_auth_text = models.TextField( default="###I have a CAS email", help_text= "A button will appear if there is other auth method(s) selected. " "This is the text for the CAS auth button. " + settings.MARKDOWN_TEXT, max_length=255, ) # LDAP settings use_ldap_auth = models.BooleanField(default=False, help_text="Use LDAP authentication ?") ldap_server_url = models.CharField( null=True, blank=True, help_text= "The URL for LDAP authentication service. Example: ldap://localhost:10389", max_length=100, ) ldap_search_base = models.CharField( null=True, blank=True, help_text= "The search base for the ldap service. Example: ou=users,ou=system", max_length=100, ) ldap_auth_text = models.TextField( default="###I have a LDAP email", help_text= "A button will appear if there is other auth method(s) selected. " "This is the text for the LDAP auth button. " + settings.MARKDOWN_TEXT, max_length=255, ) # internal authentication use_internal_auth = models.BooleanField( default=False, help_text="Use internal authentication ?") internal_auth_text = models.TextField( default="###Others", help_text= "A button will appear if there is other auth method(s) selected. " "This is the text for the internal auth button. " + settings.MARKDOWN_TEXT, max_length=255, ) # Wattdepot server wattdepot_server_url = models.CharField( null=True, blank=True, help_text="The URL for Wattdepot service. " \ "Example: http://localhost:8194/wattdepot", max_length=100,) # email settings email_enabled = models.BooleanField( default=False, help_text="Enable email ?", ) contact_email = models.CharField( default="*****@*****.**", help_text="The contact email of the admin.", max_length=100, ) email_host = models.CharField( null=True, blank=True, help_text="The host name of the email server.", max_length=100, ) email_port = models.IntegerField( default=587, help_text="The port of the email server", ) email_use_tls = models.BooleanField( default=True, help_text="Use TLS in the email server ?", ) # landing page content settings landing_slogan = models.TextField( default="The Kukui Cup: Lights off, game on!", help_text="The slogan text in the landing page. " + settings.MARKDOWN_TEXT, max_length=255, ) landing_introduction = models.TextField( default="Aloha! Welcome to the Kukui Cup.", help_text="The introduction in the landing page. " + settings.MARKDOWN_TEXT, max_length=500, ) landing_participant_text = models.TextField( default="###I am registered", max_length=255, help_text="The text of the participant button in the landing page. " + settings.MARKDOWN_TEXT) landing_non_participant_text = models.TextField( default="###I am not registered.", max_length=255, help_text="The text of the non participant button in the landing page. " + settings.MARKDOWN_TEXT) admin_tool_tip = "The global settings for the challenge. (Name, landing page, " + \ "about page, and sponsors)" class Meta: """meta""" verbose_name = "global setting" def __unicode__(self): return self.name def is_multi_auth(self): """returns true if use_cas and either use ldap or internal.""" if self.use_cas_auth: if self.use_ldap_auth or self.use_internal_auth: return True elif self.use_ldap_auth and self.use_internal_auth: return True return False def round_info(self): """returns the info for all rounds.""" return settings.COMPETITION_ROUNDS def about_page_text(self): """returns the about page text.""" aboutpage, _ = AboutPage.objects.get_or_create(pk=1) return aboutpage.about_page_text def save(self, *args, **kwargs): """Custom save method.""" super(ChallengeSetting, self).save(*args, **kwargs) cache_mgr.delete("challenge")
class Prize(models.Model): """Represents a prize in the system.""" AWARD_TO_CHOICES = ( ("individual_overall", "Individual (Overall)"), ("individual_team", "Individual (Team)"), ("team_overall", " Team (Overall)"), ("team_group", " Team (Group)"), ("group", " Group"), ) AWARD_CRITERIA_CHOICES = ( ("points", "Points"), ("energy_usage", "Energy Usage"), ("energy_goal", "Energy Goal"), ("water_usage", "Water Usage"), ("water_goal", "Water Goal"), ) round = models.ForeignKey(RoundSetting, null=True, blank=True, help_text="The round of the prize") title = models.CharField(max_length=50, help_text="The title of the prize.") short_description = models.TextField( help_text= "Short description of the prize. This should include information about who " "can win it. It is usually displayed in the prize list page.") long_description = models.TextField( help_text= "Detailed information about the prize. It is usually displayed in the details " "view of the prize.") value = models.IntegerField(help_text="The value of the prize.") image = models.ImageField(max_length=1024, upload_to=media_file_path(_MEDIA_LOCATION), blank=True, help_text="A picture of the prize.") award_to = models.CharField( max_length=50, choices=AWARD_TO_CHOICES, help_text= "The category of the award. This is used to calculated who or which team is " "winning for which category.") competition_type = models.CharField( max_length=50, choices=AWARD_CRITERIA_CHOICES, help_text="The competition type of the award.") admin_tool_tip = "Prizes for the highest score" def __unicode__(self): if self.round == None: return "%s: %s" % (self.round, self.title) else: return "%s: %s" % (self.round.name, self.title) class Meta: """meta""" unique_together = ("round", "award_to", "competition_type") ordering = ("round__name", "award_to", "competition_type") def num_awarded(self, team=None): """Returns the number of prizes that will be awarded for this prize.""" _ = team if self.award_to in ("individual_overall", "team_overall", "group"): # For overall prizes, it is only possible to award one. return 1 elif self.award_to in ("team_group", "individual_group"): # For dorm prizes, this is just the number of groups. return Group.objects.count() elif self.award_to == "individual_team": # This is awarded to each team. return Team.objects.count() raise Exception("Unknown award_to value '%s'" % self.award_to) def leader(self, team=None): """Return the prize leader.""" if self.round == None: round_name = "Round 1" else: round_name = self.round.name if self.competition_type == "points": return self._points_leader(team) elif self.competition_type == "energy": return resource_mgr.resource_leader("energy", round_name=round_name) elif self.competition_type == "energy_goal": return resource_goal.resource_goal_leader("energy", round_name=round_name) elif self.competition_type == "water": return resource_mgr.resource_leader("water", round_name=round_name) elif self.competition_type == "water_goal": return resource_goal.resource_goal_leader("water", round_name=round_name) def _points_leader(self, team=None): """Return the point leader.""" if self.round == None: round_name = "Round 1" else: round_name = self.round.name leader = None if self.award_to == "individual_overall": leader = player_mgr.points_leader(round_name=round_name) elif self.award_to == "team_group": if team: leaders = team.group.team_points_leaders(num_results=1, round_name=round_name) if leaders: leader = leaders[0] elif self.award_to == "team_overall": leader = team_mgr.team_points_leader(round_name=round_name) elif self.award_to == "group": leader = team_mgr.group_points_leader(round_name=round_name) elif self.award_to == "individual_team": if team: leaders = team.points_leaders(num_results=1, round_name=round_name) if leaders: leader = leaders[0] else: raise Exception("'%s' is not implemented yet." % self.award_to) return leader