def team_rank(team, round_name=None): """Returns the rank of the team across all groups.""" if not round_name: round_name = challenge_mgr.get_round_name() aggregate = ScoreboardEntry.objects.filter( profile__team=team, round_name=round_name).aggregate(points=Sum("points"), last=Max("last_awarded_submission")) points = aggregate["points"] or 0 last_awarded_submission = aggregate["last"] # Group by teams, filter out other rounds, and annotate. annotated_teams = ScoreboardEntry.objects.values("profile__team").filter( round_name=round_name).annotate(team_points=Sum("points"), last_awarded=Max("last_awarded_submission")) count = annotated_teams.filter(team_points__gt=points).count() # If there was a submission, tack that on to the count. if last_awarded_submission: count = count + annotated_teams.filter( team_points=points, last_awarded_submission__gt=last_awarded_submission ).count() return count + 1
def player_rank(profile, round_name=None): """user round overall rank""" if not round_name: round_name = challenge_mgr.get_round_name() entry = None try: entry = ScoreboardEntry.objects.get(profile=profile, round_name=round_name) except ObjectDoesNotExist: pass # Check if the user has done anything. if entry and 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, ).count() + 1 if entry: points = entry.points else: points = 0 # Users who have not done anything yet are assumed to be last. return ScoreboardEntry.objects.filter( points__gt=points, round_name=round_name, ).count() + 1
def group_active_participation(num_results=None, round_name=None): """Calculate active participation.""" if not round_name: round_name = challenge_mgr.get_round_name() participation = cache_mgr.get_cache('group_active_p-%s' % slugify(round_name)) if participation is None: active_participation = Group.objects.filter( team__profile__scoreboardentry__points__gte=score_mgr. active_threshold_points(), team__profile__scoreboardentry__round_name=round_name).annotate( user_count=Count('team__profile')).order_by('-user_count') if num_results: active_participation = active_participation[:num_results] participation = [] for g in active_participation: group_size = 0 for t in g.team_set.all(): group_size += t.profile_set.count() g.active_participation = (g.user_count * 100) / group_size participation.append(g) for g in Group.objects.all(): if len(participation) == num_results: break if not g in active_participation: g.active_participation = 0 participation.append(g) cache_mgr.set_cache('group_active_p-%s' % slugify(round_name), participation, 1800) return participation
def team_points(team, round_name=None): """Returns the total number of points for the team. Optional parameter for a round.""" if not round_name: round_name = challenge_mgr.get_round_name() dictionary = ScoreboardEntry.objects.filter(profile__team=team, round_name=round_name).aggregate(Sum("points")) return dictionary["points__sum"] or 0
def supply(request, page_name): """Supply the view_objects content for this widget, which is all the scoreboard data.""" user = request.user team = user.get_profile().team num_results = 10 if page_name != "status" else None round_standings = {} current_round = challenge_mgr.get_round_name() rounds = challenge_mgr.get_all_round_info()["rounds"] for key in rounds.keys(): if key == current_round or page_name == "status": round_standings[key] = { "team_standings": team_mgr.team_points_leaders(num_results, key), "profile_standings": player_mgr.points_leaders(num_results, key), "team_participation": team_mgr.team_active_participation(num_results, key) if \ page_name == "status" else None, "user_team_standings": team.points_leaders(num_results, key) if \ team and page_name != "status" else None, } count = len(rounds) return { "profile": user.get_profile(), "team": team, "current_round": current_round, "round_standings": round_standings, "no_carousel": page_name == "status", "range": count, "user": user, }
def group_active_participation(num_results=None, round_name=None): """Calculate active participation.""" if not round_name: round_name = challenge_mgr.get_round_name() participation = cache_mgr.get_cache('group_active_p-%s' % slugify(round_name)) if participation is None: active_participation = Group.objects.filter( team__profile__scoreboardentry__points__gte=score_mgr.active_threshold_points(), team__profile__scoreboardentry__round_name=round_name).annotate( user_count=Count('team__profile')).order_by('-user_count') if num_results: active_participation = active_participation[:num_results] participation = [] for g in active_participation: group_size = 0 for t in g.team_set.all(): group_size += t.profile_set.count() g.active_participation = (g.user_count * 100) / group_size participation.append(g) for g in Group.objects.all(): if len(participation) == num_results: break if not g in active_participation: g.active_participation = 0 participation.append(g) cache_mgr.set_cache('group_active_p-%s' % slugify(round_name), participation, 1800) return participation
def supply(request, page_name): """Supply view_objects content, which is the set of team members.""" _ = page_name # Get the team members. team = request.user.profile.team current_round = challenge_mgr.get_round_name() if team and current_round: members_with_points = ScoreboardEntry.objects.filter( round_name=current_round).select_related( 'profile').filter(profile__team=team) zero_point_members = team.profile_set.exclude( id__in=members_with_points.values_list( 'profile__id', flat=True)) # calculate and sort by rank members_with_points = sorted(list(members_with_points), key=lambda member: member.profile.overall_rank()) zero_point_members = sorted(list(zero_point_members), key=lambda member: member.overall_rank()) else: members_with_points = None zero_point_members = None return { "team_members": members_with_points, "zero_members": zero_point_members, }
def player_rank_in_team(profile, round_name=None): """Returns user's rank in his team.""" if not round_name: round_name = challenge_mgr.get_round_name() team = profile.team entry = None try: entry = ScoreboardEntry.objects.get(profile=profile, round_name=round_name) except ObjectDoesNotExist: pass if entry and 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__team=team, round_name=round_name, ).count() + 1 if entry: points = entry.points else: points = 0 return ScoreboardEntry.objects.filter( points__gt=points, profile__team=team, round_name=round_name, ).count() + 1
def team_active_participation(num_results=None, round_name=None): """Calculate active participation.""" if not round_name: round_name = challenge_mgr.get_round_name() participation = cache_mgr.get_cache('active_p-%s' % slugify(round_name)) if participation is None: active_participation = Team.objects.filter( profile__scoreboardentry__points__gte=score_mgr.active_threshold_points(), profile__scoreboardentry__round_name=round_name).annotate( user_count=Count('profile')).order_by('-user_count') if num_results: active_participation = active_participation[:num_results] participation = [] for t in active_participation: if t.size: t.active_participation = (t.user_count * 100) / t.size else: t.active_participation = (t.user_count * 100) / t.profile_set.count() participation.append(t) for t in Team.objects.all(): if len(participation) == num_results: break if not t in active_participation: t.active_participation = 0 participation.append(t) cache_mgr.set_cache('active_p-%s' % slugify(round_name), participation, 1800) return participation
def supply(request, page_name): """Supply view_objects content, which is the set of team members.""" _ = page_name # Get the team members. team = request.user.profile.team current_round = challenge_mgr.get_round_name() if team and current_round: members_with_points = ScoreboardEntry.objects.filter( round_name=current_round).select_related('profile').filter( profile__team=team) zero_point_members = team.profile_set.exclude( id__in=members_with_points.values_list('profile__id', flat=True)) # calculate and sort by rank members_with_points = sorted( list(members_with_points), key=lambda member: member.profile.overall_rank()) zero_point_members = sorted(list(zero_point_members), key=lambda member: member.overall_rank()) else: members_with_points = None zero_point_members = None return { "team_members": members_with_points, "zero_members": zero_point_members, }
def team_rank(team, round_name=None): """Returns the rank of the team across all groups.""" if not round_name: round_name = challenge_mgr.get_round_name() aggregate = ScoreboardEntry.objects.filter( profile__team=team, round_name=round_name).aggregate(points=Sum("points"), last=Max("last_awarded_submission")) points = aggregate["points"] or 0 last_awarded_submission = aggregate["last"] # Group by teams, filter out other rounds, and annotate. annotated_teams = ScoreboardEntry.objects.values("profile__team").filter( round_name=round_name).annotate( team_points=Sum("points"), last_awarded=Max("last_awarded_submission")) count = annotated_teams.filter(team_points__gt=points).count() # If there was a submission, tack that on to the count. if last_awarded_submission: count = count + annotated_teams.filter( team_points=points, last_awarded_submission__gt=last_awarded_submission).count() return count + 1
def player_rank(profile, round_name=None): """user round overall rank""" if not round_name: round_name = challenge_mgr.get_round_name() entry = None try: entry = ScoreboardEntry.objects.get(profile=profile, round_name=round_name) except ObjectDoesNotExist: pass # Check if the user has done anything. if entry and 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, ).count() + 1 if entry: points = entry.points else: points = 0 # Users who have not done anything yet are assumed to be last. return ScoreboardEntry.objects.filter( points__gt=points, round_name=round_name, ).count() + 1
def team_active_participation(num_results=None, round_name=None): """Calculate active participation.""" if not round_name: round_name = challenge_mgr.get_round_name() participation = cache_mgr.get_cache('active_p-%s' % slugify(round_name)) if participation is None: active_participation = Team.objects.filter( profile__scoreboardentry__points__gte=score_mgr. active_threshold_points(), profile__scoreboardentry__round_name=round_name).annotate( user_count=Count('profile')).order_by('-user_count') if num_results: active_participation = active_participation[:num_results] participation = [] for t in active_participation: if t.size: t.active_participation = (t.user_count * 100) / t.size else: t.active_participation = (t.user_count * 100) / t.profile_set.count() participation.append(t) for t in Team.objects.all(): if len(participation) == num_results: break if not t in active_participation: t.active_participation = 0 participation.append(t) cache_mgr.set_cache('active_p-%s' % slugify(round_name), participation, 1800) return participation
def player_rank_in_team(profile, round_name=None): """Returns user's rank in his team.""" if not round_name: round_name = challenge_mgr.get_round_name() team = profile.team entry = None try: entry = ScoreboardEntry.objects.get(profile=profile, round_name=round_name) except ObjectDoesNotExist: pass if entry and 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__team=team, round_name=round_name, ).count() + 1 if entry: points = entry.points else: points = 0 return ScoreboardEntry.objects.filter( points__gt=points, profile__team=team, round_name=round_name, ).count() + 1
def _update_scoreboard_entry(profile, points, transaction_date): """Update the scoreboard entry for the associated round.""" current_round = challenge_mgr.get_round_name(transaction_date) _update_round_scoreboard_entry(profile, current_round, points, transaction_date) # also update for the overall round _update_round_scoreboard_entry(profile, "Overall", points, transaction_date)
def team_points(team, round_name=None): """Returns the total number of points for the team. Optional parameter for a round.""" if not round_name: round_name = challenge_mgr.get_round_name() dictionary = ScoreboardEntry.objects.filter( profile__team=team, round_name=round_name).aggregate(Sum("points")) return dictionary["points__sum"] or 0
def player_points_leader(round_name=None): """Returns the points leader (the first place) out of all users, as a Profile object.""" if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter(round_name=round_name).order_by("-points", "-last_awarded_submission") if entries: return entries[0].profile else: return None
def _update_scoreboard_entry(profile, points, transaction_date): """Update the scoreboard entry for the associated round.""" current_round = challenge_mgr.get_round_name(transaction_date) _update_round_scoreboard_entry(profile, current_round, points, transaction_date) # also update for the overall round _update_round_scoreboard_entry(profile, "Overall", points, transaction_date)
def team_member_point_percent(user, points, percent): """Returns True if the user's team has at least [percent] members got at least [points].""" team = user.get_profile().team if team: current_round = challenge_mgr.get_round_name() point_count = ScoreboardEntry.objects.filter( profile__team=team, points__gte=points, round_name=current_round ).count() return point_count * 100 / team.profile_set.count() >= percent return False
def player_has_points(profile, points, round_name=None): """Returns True if the user has at least the requested number of points.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.filter(profile=profile, round_name=round_name) if entry: return entry[0].points >= points else: return False
def player_points(profile, round_name=None): """Returns the amount of points the user has in the round.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.filter(profile=profile, round_name=round_name) if entry: return entry[0].points else: return 0
def player_has_points(profile, points, round_name=None): """Returns True if the user has at least the requested number of points.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.filter(profile=profile, round_name=round_name) if entry: return entry[0].points >= points else: return False
def player_points_leader(round_name=None): """Returns the points leader (the first place) out of all users, as a Profile object.""" if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter(round_name=round_name, ).order_by( "-points", "-last_awarded_submission") if entries: return entries[0].profile else: return None
def player_points(profile, round_name=None): """Returns the amount of points the user has in the round.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.filter(profile=profile, round_name=round_name) if entry: return entry[0].points else: return 0
def team_member_point_percent(user, points, percent): """Returns True if the user's team has at least [percent] members got at least [points].""" team = user.get_profile().team if team: current_round = challenge_mgr.get_round_name() point_count = ScoreboardEntry.objects.filter(profile__team=team, points__gte=points, round_name=current_round, ).count() return point_count * 100 / team.profile_set.count() >= percent return False
def player_points_leaders_in_team(team, num_results=None, round_name=None): """Gets the individual points leaders for the team, as Profile objects""" if not round_name: round_name = challenge_mgr.get_round_name() results = team.profile_set.select_related('scoreboardentry').filter( scoreboardentry__round_name=round_name ).order_by("-scoreboardentry__points", "-scoreboardentry__last_awarded_submission", ) if num_results: results = results[:num_results] return results
def group_points(group, round_name=None): """Returns the total number of points for the team. Optional parameter for a round.""" if not round_name: round_name = challenge_mgr.get_round_name() group_total_points = 0 scores = ScoreboardEntry.objects.all().filter(round_name=round_name) for score in scores: if score.profile.team.group == group: group_total_points += score.points return group_total_points
def testCurrentRound(self): """Tests that the current round retrieval is correct.""" current_round = "Round 1" test_utils.set_competition_round() current = challenge_mgr.get_round_name() self.assertEqual(current, current_round, "Test that the current round is returned.") start = datetime.datetime.today() + datetime.timedelta(days=1) end = start + datetime.timedelta(days=7) rounds = RoundSetting.objects.get(name="Round 1") rounds.start = start rounds.end = end rounds.save() challenge_mgr.init() current_round = challenge_mgr.get_round_name() self.assertTrue(current_round is None, "Test that there is no current round.")
def testCurrentRound(self): """Tests that the current round retrieval is correct.""" current_round = "Round 1" test_utils.set_competition_round() current = challenge_mgr.get_round_name() self.assertEqual(current, current_round, "Test that the current round is returned.") start = datetime.datetime.today() + datetime.timedelta(days=1) end = start + datetime.timedelta(days=7) rounds = RoundSetting.objects.get(name="Round 1") rounds.start = start rounds.end = end rounds.save() challenge_mgr.init() current_round = challenge_mgr.get_round_name() self.assertTrue(current_round is None, "Test that there is no current round.")
def supply(request, page_name): """Supply the view_objects content for this widget, which is all the scoreboard data.""" user = request.user team = user.profile.team num_results = 10 if page_name != "status" else None round_standings = {} current_round = challenge_mgr.get_round_name() today = datetime.datetime.today() rounds = challenge_mgr.get_all_round_info()["rounds"] for key in rounds.keys(): # 1. always display current round # 2. if not future round # a. display the round with the "display_scoreboard" flag # b. display in the status page if rounds[key]["start"] <= today and \ (rounds[key]["display_scoreboard"] or page_name == "status"): round_standings[key] = { #"group_standings": team_mgr.group_points_leaders(num_results, key), "team_standings": team_mgr.team_points_leaders(num_results, key), "profile_standings": player_mgr.points_leaders(num_results, key), #"group_participation": team_mgr.group_active_participation(num_results, key) if \ # page_name == "status" else None, "team_participation": team_mgr.team_active_participation(num_results, key) if \ page_name == "status" else None, "user_team_standings": team.points_leaders(num_results, key) if \ team and page_name != "status" else None, } """ # add an overall scoreboard round_standings["Overall"] = { #"group_standings": team_mgr.group_points_leaders(num_results, "Overall"), "team_standings": team_mgr.team_points_leaders(num_results, "Overall"), "profile_standings": player_mgr.points_leaders(num_results, "Overall"), #"group_participation": team_mgr.group_active_participation(num_results, "Overall") if\ # page_name == "status" else None, "team_participation": team_mgr.team_active_participation(num_results, "Overall") if \ page_name == "status" else None, } """ count = len(rounds) return { "profile": user.profile, "team": team, "current_round": current_round, "round_standings": round_standings, "no_carousel": page_name == "status", "range": count, "user": user, }
def team_points_leader(round_name=None): """Returns the team points leader (the first place) across all groups, as a Team ID.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.values("profile__team").filter(round_name=round_name).annotate( points=Sum("points"), last=Max("last_awarded_submission")).order_by("-points", "-last") if entry: return entry[0]["profile__team"] else: return None
def supply(request, page_name): """Supply the view_objects content for this widget, which is all the scoreboard data.""" user = request.user team = user.get_profile().team num_results = 10 if page_name != "status" else None round_standings = {} current_round = challenge_mgr.get_round_name() today = datetime.datetime.today() rounds = challenge_mgr.get_all_round_info()["rounds"] for key in rounds.keys(): # 1. always display current round # 2. if not future round # a. display the round with the "display_scoreboard" flag # b. display in the status page if rounds[key]["start"] <= today and \ (rounds[key]["display_scoreboard"] or page_name == "status"): round_standings[key] = { "group_standings": team_mgr.group_points_leaders(num_results, key), "team_standings": team_mgr.team_points_leaders(num_results, key), "profile_standings": player_mgr.points_leaders(num_results, key), "group_participation": team_mgr.group_active_participation(num_results, key) if \ page_name == "status" else None, "team_participation": team_mgr.team_active_participation(num_results, key) if \ page_name == "status" else None, "user_team_standings": team.points_leaders(num_results, key) if \ team and page_name != "status" else None, } # add an overall scoreboard round_standings["Overall"] = { "group_standings": team_mgr.group_points_leaders(num_results, "Overall"), "team_standings": team_mgr.team_points_leaders(num_results, "Overall"), "profile_standings": player_mgr.points_leaders(num_results, "Overall"), "group_participation": team_mgr.group_active_participation(num_results, "Overall") if\ page_name == "status" else None, "team_participation": team_mgr.team_active_participation(num_results, "Overall") if \ page_name == "status" else None, } count = len(rounds) return { "profile": user.get_profile(), "team": team, "current_round": current_round, "round_standings": round_standings, "no_carousel": page_name == "status", "range": count, "user": user, }
def group_points_leader(round_name=None): """Returns the group points leader (the first place) across all groups, as a Group ID.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ScoreboardEntry.objects.values("profile__team__group").filter( round_name=round_name).annotate( points=Sum("points"), last=Max("last_awarded_submission")).order_by("-points", "-last") if entry: return entry[0]["profile__team__group"] else: return None
def team_points_leaders_in_group(group, num_results=None, round_name=None): """Returns the top points leaders for the given group.""" if not round_name: round_name = challenge_mgr.get_round_name() results = group.team_set.filter( profile__scoreboardentry__round_name=round_name).annotate( points=Sum("profile__scoreboardentry__points"), last=Max("profile__scoreboardentry__last_awarded_submission")).order_by( "-points", "-last") if num_results: results = results[:num_results] return results
def group_awarded(group, round_name=None): """Returns the group members""" if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter( round_name=round_name, group=group, profile__team__group__isnull=False).values( "profile__team__group__name") total = 0 for entry in entries: total = total + 1 return total
def team_points_leader(round_name=None): """Returns the team points leader (the first place) across all groups, as a Team object.""" if not round_name: round_name = challenge_mgr.get_round_name() team_id = score_mgr.team_points_leader(round_name=round_name) if team_id: return Team.objects.get(id=team_id) else: teams = Team.objects.all() if teams: return teams[0] else: return None
def update_resource_usage(resource, date): """Update the latest resource usage from Storage server.""" session = requests.session() for team in Team.objects.all(): goal_settings = team_goal_settings(team, resource) if not goal_settings.manual_entry: storage = get_resource_storage(goal_settings.data_storage) resource_mgr.update_team_resource_usage(resource, session, date, team, storage) # clear the cache for energy ranking, and RIB where it displays round_name = challenge_mgr.get_round_name(date) cache_mgr.delete("%s_ranks-%s" % (resource, slugify(round_name)))
def notify_winner(request): """Sends an email to the user notifying them that they are a winner.""" _ = request round_name = challenge_mgr.get_round_name() prizes = RafflePrize.objects.filter(round__name=round_name) for prize in prizes: if prize.winner: # Notify winner using the template. template = NoticeTemplate.objects.get(notice_type='raffle-winner') message = template.render({'PRIZE': prize}) UserNotification.create_info_notification(prize.winner, message, True, prize) return HttpResponseRedirect("/admin/raffle/raffleprize/")
def group_points_leader(round_name=None, place=1): """Returns the team points leader (the place) across all groups, as a Team object.""" if not round_name: round_name = challenge_mgr.get_round_name() group_id = score_mgr.group_points_leader(round_name=round_name, place=place) if group_id: return Group.objects.get(id=group_id) else: groups = Group.objects.all() if len(groups) >= place: return groups[place - 1] else: return None
def team_points_leader(round_name=None, place=1): """Returns the team points leader (the first place) across all groups, as a Team object.""" if not round_name: round_name = challenge_mgr.get_round_name() team_id = score_mgr.team_points_leader(round_name=round_name, place=place) if team_id: return Team.objects.get(id=team_id) else: teams = Team.objects.all() if len(teams) >= place: return teams[place - 1] else: return None
def group_points_leader(round_name=None): """Returns the team points leader (the first place) across all groups, as a Team object.""" if not round_name: round_name = challenge_mgr.get_round_name() group_id = score_mgr.group_points_leader(round_name=round_name) if group_id: return Group.objects.get(id=group_id) else: groups = Group.objects.all() if groups: return groups[0] else: return None
def team_points_leaders_in_group(group, num_results=None, round_name=None): """Returns the top points leaders for the given group.""" if not round_name: round_name = challenge_mgr.get_round_name() results = group.team_set.filter( profile__scoreboardentry__round_name=round_name).annotate( points=Sum("profile__scoreboardentry__points"), last=Max( "profile__scoreboardentry__last_awarded_submission")).order_by( "-points", "-last") if num_results: results = results[:num_results] return results
def award_participation(): """award the participation rate for all team.""" if not challenge_mgr.is_game_enabled("Participation Game"): return p_setting, _ = ParticipationSetting.objects.get_or_create(pk=1) current_round = challenge_mgr.get_round_name() for team in team_mgr.team_active_participation(round_name=current_round): team_participation, _ = TeamParticipation.objects.get_or_create( team=team, round_name=current_round) # check if the participation rate change if team_participation.participation != team.active_participation: # save the new participation rate if team.active_participation == 100: if team_participation.awarded_percent != "100": team_participation.awarded_percent = "100" team_mgr.award_member_points(team, p_setting.points_100_percent, "Team 100% participation") elif team.active_participation >= 75: if team_participation.awarded_percent != "75": team_participation.awarded_percent = "75" team_mgr.award_member_points(team, p_setting.points_75_percent, "Team 75% participation") elif team.active_participation >= 50: if not team_participation.awarded_percent: team_participation.awarded_percent = "50" team_mgr.award_member_points(team, p_setting.points_50_percent, "Team 50% participation") team_participation.participation = team.active_participation team_participation.save() # store the overall participation rate for team in team_mgr.team_active_participation(round_name="Overall"): team_participation, _ = TeamParticipation.objects.get_or_create( team=team, round_name="Overall") # check if the participation rate change if team_participation.participation != team.active_participation: # save the new participation rate team_participation.participation = team.active_participation team_participation.save()
def award_participation(): """award the participation rate for all team.""" if not challenge_mgr.is_game_enabled("Participation Game"): return p_setting, _ = ParticipationSetting.objects.get_or_create(pk=1) current_round = challenge_mgr.get_round_name() for team in team_mgr.team_active_participation(round_name=current_round): team_participation, _ = TeamParticipation.objects.get_or_create( team=team, round_name=current_round) # check if the participation rate change if team_participation.participation != team.active_participation: # save the new participation rate if team.active_participation == 100: if team_participation.awarded_percent != "100": team_participation.awarded_percent = "100" team_mgr.award_member_points(team, p_setting.points_100_percent, "Team 100% participation") elif team.active_participation >= 75: if team_participation.awarded_percent != "75": team_participation.awarded_percent = "75" team_mgr.award_member_points(team, p_setting.points_75_percent, "Team 75% participation") elif team.active_participation >= 50: if not team_participation.awarded_percent: team_participation.awarded_percent = "50" team_mgr.award_member_points(team, p_setting.points_50_percent, "Team 50% participation") team_participation.participation = team.active_participation team_participation.save() # store the overall participation rate for team in team_mgr.team_active_participation(round_name="Overall"): team_participation, _ = TeamParticipation.objects.get_or_create( team=team, round_name="Overall") # check if the participation rate change if team_participation.participation != team.active_participation: # save the new participation rate team_participation.participation = team.active_participation team_participation.save()
def update_resource_usage(resource, date): """Update the latest resource usage from Storage server.""" session = requests.session() for team in Team.objects.all(): goal_settings = team_goal_settings(team, resource) if not goal_settings.manual_entry: storage = get_resource_storage(goal_settings.data_storage) resource_mgr.update_team_resource_usage(resource, session, date, team, storage) # clear the cache for energy ranking, and RIB where it displays round_name = challenge_mgr.get_round_name(date) cache_mgr.delete("%s_ranks-%s" % (resource, slugify(round_name)))
def pick_winner(request): """Picks the raffle game winners for the raffle deadline that has passed.""" _ = request round_name = challenge_mgr.get_round_name() prizes = RafflePrize.objects.filter(round__name=round_name) for prize in prizes: if not prize.winner: # Randomly order the tickets and then pick a random ticket. tickets = prize.raffleticket_set.order_by("?").all() if tickets.count(): ticket = random.randint(0, tickets.count() - 1) user = tickets[ticket].user prize.winner = user prize.save() return HttpResponseRedirect("/admin/raffle/raffleprize/")
def notify_winner(request): """Sends an email to the user notifying them that they are a winner.""" _ = request round_name = challenge_mgr.get_round_name() prizes = RafflePrize.objects.filter(round__name=round_name) for prize in prizes: if prize.winner: # Notify winner using the template. template = NoticeTemplate.objects.get(notice_type='raffle-winner') message = template.render({'PRIZE': prize}) UserNotification.create_info_notification(prize.winner, message, True, prize) return HttpResponseRedirect("/admin/raffle/raffleprize/")
def pick_winner(request): """Picks the raffle game winners for the raffle deadline that has passed.""" _ = request round_name = challenge_mgr.get_round_name() prizes = RafflePrize.objects.filter(round__name=round_name) for prize in prizes: if not prize.winner: # Randomly order the tickets and then pick a random ticket. tickets = prize.raffleticket_set.order_by("?").all() if tickets.count(): ticket = random.randint(0, tickets.count() - 1) user = tickets[ticket].user prize.winner = user prize.save() return HttpResponseRedirect("/admin/raffle/raffleprize/")
def group_points(num_results=None, round_name=None): """Returns the groups with their point totals""" if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter( round_name=round_name, profile__team__group__isnull=False).values( "profile__team__group__name").annotate( points=Sum("points"), last=Max("last_awarded_submission")).order_by("-points", "-last") if entries: if num_results: entries = entries[:num_results] return entries else: return None
def player_points_leaders(num_results=None, round_name=None): """Returns the points leaders out of all users, as a dictionary object with profile__name and points. """ if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter(round_name=round_name,).order_by( "-points", "-last_awarded_submission").values('profile', 'profile__name', 'points') if entries: if num_results: entries = entries[:num_results] return entries else: return None
def testRoundInfo(self): """Tests that round info is available for the page to process.""" challenge_mgr.init() test_utils.set_competition_round() current_round = challenge_mgr.get_round_name() User.objects.create_user("user", "*****@*****.**", password="******") self.client.login(username="******", password="******") challenge_mgr.register_page_widget("home", "home") response = self.client.get(reverse("home_index")) # Response context should have round info corresponding to the past days. self.assertEqual(response.context["CURRENT_ROUND_INFO"]["name"], current_round, "Expected %s but got %s" % ( current_round, response.context["CURRENT_ROUND_INFO"]["name"]))
def group_points_leader(round_name=None, place=1): """Returns the group points leader (the place) across all groups, as a Group ID.""" if not round_name: round_name = challenge_mgr.get_round_name() entry = ( ScoreboardEntry.objects.values("profile__team__group") .filter(round_name=round_name) .annotate(points=Sum("points"), last=Max("last_awarded_submission")) .order_by("-points", "-last") ) if len(entry) >= place: return entry[place - 1]["profile__team__group"] else: return None
def player_points_leaders_in_team(team, num_results=None, round_name=None): """Gets the individual points leaders for the team, as Profile objects and scoreboardentry_points""" if not round_name: round_name = challenge_mgr.get_round_name() results = team.profile_set.select_related('scoreboardentry').filter( scoreboardentry__round_name=round_name).order_by( "-scoreboardentry__points", "-scoreboardentry__last_awarded_submission", ).annotate(scoreboardentry_points=Sum("scoreboardentry__points")) if num_results: results = results[:num_results] return results
def testRoundInfo(self): """Tests that round info is available for the page to process.""" challenge_mgr.init() test_utils.set_competition_round() current_round = challenge_mgr.get_round_name() User.objects.create_user("user", "*****@*****.**", password="******") self.client.login(username="******", password="******") challenge_mgr.register_page_widget("home", "home") response = self.client.get(reverse("home_index")) # Response context should have round info corresponding to the past days. self.assertEqual( response.context["CURRENT_ROUND_INFO"]["name"], current_round, "Expected %s but got %s" % (current_round, response.context["CURRENT_ROUND_INFO"]["name"]))
def team_points_leaders(num_results=None, round_name=None): """Returns the team points leaders across all groups, as a dictionary profile__team__name and points. """ if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter( round_name=round_name, profile__team__isnull=False).values("profile__team__name").annotate( points=Sum("points"), last=Max("last_awarded_submission")).order_by("-points", "-last") if entries: if num_results: entries = entries[:num_results] return entries else: return None
def player_points_leaders(num_results=None, round_name=None): """Returns the points leaders out of all users, as a dictionary object with profile__name and points. """ if not round_name: round_name = challenge_mgr.get_round_name() entries = ScoreboardEntry.objects.filter( round_name=round_name, ).select_related( 'profile', 'user__is_staff').filter(profile__user__is_staff=False).order_by( "-points", "-last_awarded_submission").values('profile', 'profile__name', 'points') if entries: if num_results: entries = entries[:num_results] return entries else: return None
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 current_round_overall_rank(self): """Returns the overall rank of the user for the current round.""" current_round = challenge_mgr.get_round_name() return score_mgr.player_rank(self, round_name=current_round)
def current_round_team_rank(self): """Returns the rank of the user for the current round in their own team.""" current_round = challenge_mgr.get_round_name() return score_mgr.player_rank_in_team(self, round_name=current_round)
def current_round_points(self): """Returns the total number of points for the user. Optional parameter for a round.""" current_round = challenge_mgr.get_round_name() return score_mgr.player_points(self, round_name=current_round)