|
1 | | -from challenges.models import Challenge |
| 1 | +from datetime import timedelta |
| 2 | + |
| 3 | +from challenges.models import Challenge, ChallengePhase |
2 | 4 | from django.contrib.admin import SimpleListFilter |
| 5 | +from django.db.models import Count, Exists, OuterRef, Q |
3 | 6 | from django.utils import timezone |
4 | 7 |
|
| 8 | +from .models import Submission |
| 9 | + |
| 10 | + |
| 11 | +def _get_top_challenge_ids(): |
| 12 | + """Return IDs of top 10 challenges by submission count in last 30 days.""" |
| 13 | + thirty_days_ago = timezone.now() - timedelta(days=30) |
| 14 | + recent_submissions = Submission.objects.filter( |
| 15 | + challenge_phase__challenge_id=OuterRef("pk"), |
| 16 | + submitted_at__gte=thirty_days_ago, |
| 17 | + ) |
| 18 | + top_challenges = ( |
| 19 | + Challenge.objects.filter( |
| 20 | + published=True, |
| 21 | + approved_by_admin=True, |
| 22 | + is_disabled=False, |
| 23 | + ) |
| 24 | + .annotate(has_recent_submission=Exists(recent_submissions)) |
| 25 | + .filter(has_recent_submission=True) |
| 26 | + .annotate( |
| 27 | + submission_count=Count( |
| 28 | + "challengephase__submissions", |
| 29 | + filter=Q( |
| 30 | + challengephase__submissions__submitted_at__gte=thirty_days_ago |
| 31 | + ), |
| 32 | + ) |
| 33 | + ) |
| 34 | + .order_by("-submission_count")[:10] |
| 35 | + ) |
| 36 | + return list(top_challenges.values_list("id", flat=True)) |
5 | 37 |
|
6 | | -class OngoingChallengesFilter(SimpleListFilter): |
| 38 | + |
| 39 | +class TopActiveChallengesFilter(SimpleListFilter): |
7 | 40 | """ |
8 | | - List filter that shows only ongoing challenges in the dropdown, |
9 | | - reducing clutter when filtering submissions by challenge. |
| 41 | + List filter that shows top 10 challenges by submission count |
| 42 | + that have received submissions in the last 30 days. |
10 | 43 | """ |
11 | 44 |
|
12 | | - title = "By challenge" |
| 45 | + title = "Top active challenges (last 30 days)" |
13 | 46 | parameter_name = "challenge" |
14 | 47 |
|
15 | 48 | def lookups(self, request, model_admin): |
16 | | - now = timezone.now() |
17 | | - ongoing = Challenge.objects.filter( |
18 | | - published=True, |
19 | | - approved_by_admin=True, |
20 | | - is_disabled=False, |
21 | | - start_date__lte=now, |
22 | | - end_date__gte=now, |
23 | | - ).order_by("title") |
24 | | - return [(c.id, c.title) for c in ongoing] |
| 49 | + thirty_days_ago = timezone.now() - timedelta(days=30) |
| 50 | + recent_submissions = Submission.objects.filter( |
| 51 | + challenge_phase__challenge_id=OuterRef("pk"), |
| 52 | + submitted_at__gte=thirty_days_ago, |
| 53 | + ) |
| 54 | + top_challenges = ( |
| 55 | + Challenge.objects.filter( |
| 56 | + published=True, |
| 57 | + approved_by_admin=True, |
| 58 | + is_disabled=False, |
| 59 | + ) |
| 60 | + .annotate(has_recent_submission=Exists(recent_submissions)) |
| 61 | + .filter(has_recent_submission=True) |
| 62 | + .annotate( |
| 63 | + submission_count=Count( |
| 64 | + "challengephase__submissions", |
| 65 | + filter=Q( |
| 66 | + challengephase__submissions__submitted_at__gte=thirty_days_ago |
| 67 | + ), |
| 68 | + ) |
| 69 | + ) |
| 70 | + .order_by("-submission_count")[:10] |
| 71 | + ) |
| 72 | + return [ |
| 73 | + (c.id, f"{c.title} ({c.submission_count})") for c in top_challenges |
| 74 | + ] |
25 | 75 |
|
26 | 76 | def queryset(self, request, queryset): |
27 | 77 | if self.value(): |
28 | 78 | return queryset.filter(challenge_phase__challenge_id=self.value()) |
29 | 79 | return queryset |
| 80 | + |
| 81 | + |
| 82 | +class ActiveChallengePhaseFilter(SimpleListFilter): |
| 83 | + """ |
| 84 | + List filter for challenge phase. When a challenge is selected, only phases |
| 85 | + for that challenge are shown. Otherwise, only phases from top active |
| 86 | + challenges (last 30 days) are shown. |
| 87 | + """ |
| 88 | + |
| 89 | + title = "Phase (top active challenges)" |
| 90 | + parameter_name = "challenge_phase" |
| 91 | + |
| 92 | + def lookups(self, request, model_admin): |
| 93 | + challenge_id = request.GET.get("challenge") |
| 94 | + if challenge_id: |
| 95 | + phases = ChallengePhase.objects.filter( |
| 96 | + challenge_id=challenge_id |
| 97 | + ).order_by("name") |
| 98 | + return [(p.id, p.name) for p in phases] |
| 99 | + # When no challenge selected, show only phases from top active |
| 100 | + # challenges |
| 101 | + top_challenge_ids = _get_top_challenge_ids() |
| 102 | + if not top_challenge_ids: |
| 103 | + return [] |
| 104 | + phases = ( |
| 105 | + ChallengePhase.objects.filter(challenge_id__in=top_challenge_ids) |
| 106 | + .select_related("challenge") |
| 107 | + .order_by("challenge__title", "name") |
| 108 | + ) |
| 109 | + return [(p.id, f"{p.challenge.title} — {p.name}") for p in phases] |
| 110 | + |
| 111 | + def queryset(self, request, queryset): |
| 112 | + if self.value(): |
| 113 | + return queryset.filter(challenge_phase_id=self.value()) |
| 114 | + return queryset |
0 commit comments