|
1 |
| -from django.shortcuts import render |
| 1 | +import json |
2 | 2 |
|
3 |
| -from gamification.models import Participant |
| 3 | +from django.views.generic import TemplateView |
4 | 4 |
|
| 5 | +from community.views import get_header_and_footer |
| 6 | +from gamification.models import Participant, Level, Badge |
5 | 7 |
|
6 |
| -def index(request): |
7 |
| - Participant.objects.filter(username__startswith='testuser').delete() |
| 8 | + |
| 9 | +class GamificationResults(TemplateView): |
| 10 | + template_name = 'gamification.html' |
8 | 11 | participants = Participant.objects.all()
|
9 |
| - args = {'participants': participants} |
10 |
| - return render(request, 'gamification.html', args) |
| 12 | + |
| 13 | + def get_users_username(self, users): |
| 14 | + """ |
| 15 | + :param users: A Queryset, with a field username |
| 16 | + :return: A list of usernames |
| 17 | + """ |
| 18 | + usernames = list() |
| 19 | + for user in users: |
| 20 | + usernames.append(user.username) |
| 21 | + return usernames |
| 22 | + |
| 23 | + def group_participants_by_score(self): |
| 24 | + """ |
| 25 | + Divide the participants according to their scores. For example, if |
| 26 | + there are 10 contributors who have different scores and there are |
| 27 | + possibly 4 ranges i.e. score_gt 80, score_between (70,80), |
| 28 | + score_between (60,70) and score_lt 60. So, divide them and put them |
| 29 | + in their respective lists. |
| 30 | + :return: A Dict, with key as score_range and value a list of |
| 31 | + contributors username |
| 32 | + """ |
| 33 | + scores = set() |
| 34 | + for contrib in self.participants: |
| 35 | + scores.add(contrib.score) |
| 36 | + |
| 37 | + scores = list(scores) |
| 38 | + scores.sort() |
| 39 | + |
| 40 | + try: |
| 41 | + min_score, max_score = scores[0], scores[-1] |
| 42 | + except IndexError: |
| 43 | + return dict() |
| 44 | + |
| 45 | + difference_bw_groups_score = int(max_score/5) |
| 46 | + score_ranges = [ |
| 47 | + min_score + i * difference_bw_groups_score for i in range(6) |
| 48 | + ] |
| 49 | + score_ranges[-1] += max_score % 5 |
| 50 | + |
| 51 | + grouped_participants = dict() |
| 52 | + for index, score in enumerate(score_ranges[1:]): |
| 53 | + begin_score, end_score = score_ranges[index], score |
| 54 | + |
| 55 | + filtered_participants = self.participants.filter( |
| 56 | + score__range=[begin_score, end_score] |
| 57 | + ) |
| 58 | + |
| 59 | + if begin_score == min_score: |
| 60 | + grp_lvl = f'<{end_score}' |
| 61 | + elif end_score < max_score: |
| 62 | + grp_lvl = f'>={begin_score} and <{end_score}' |
| 63 | + else: |
| 64 | + grp_lvl = f'>={begin_score}' |
| 65 | + |
| 66 | + grouped_participants[grp_lvl] = json.dumps( |
| 67 | + self.get_users_username(filtered_participants) |
| 68 | + ) |
| 69 | + return grouped_participants |
| 70 | + |
| 71 | + def get_context_data(self, **kwargs): |
| 72 | + context = super().get_context_data(**kwargs) |
| 73 | + context = get_header_and_footer(context) |
| 74 | + context['gamification_results'] = self.participants |
| 75 | + context['levels'] = Level.objects.all() |
| 76 | + context['badges'] = Badge.objects.all() |
| 77 | + context['grouped_participants'] = self.group_participants_by_score() |
| 78 | + |
| 79 | + return context |
0 commit comments