-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmixins.py
71 lines (60 loc) · 2.19 KB
/
mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from datetime import timedelta
from dateutil.parser import parse
from django.contrib.auth.models import User
from django.db.models import Count, F
from django.utils import timezone
from ..models import LoggedAction
class ContributorsMixin(object):
def get_leaderboards(self, all_time=True):
result = []
boards = [
(
"In the last week",
timezone.now() - timedelta(days=7),
timezone.now(),
),
# (
# "May 2024 Elections",
# timezone.make_aware(parse("2024-03-01")),
# timezone.make_aware(parse("2024-05-31")),
# ),
(
"Since the 4 July 2024 General Election Announcement",
timezone.make_aware(parse("2024-05-22")),
timezone.now(),
),
]
if all_time:
boards.insert(0, ("All Time", None, None))
for title, since, until in boards:
interesting_actions = LoggedAction.objects.exclude(
action_type="set-candidate-not-elected"
)
if since:
qs = interesting_actions.filter(created__gt=since)
else:
qs = interesting_actions
if until:
qs = qs.filter(created__lt=until)
rows = (
qs.annotate(username=F("user__username"))
.values("username")
.annotate(edit_count=Count("user"))
.order_by("-edit_count")
)
leaderboard = {"title": title, "rows": rows[:25]}
result.append(leaderboard)
return result
def get_recent_changes_queryset(self):
return (
LoggedAction.objects.exclude(
action_type="set-candidate-not-elected"
)
.filter(user__isnull=False)
.select_related("user", "person", "ballot", "ballot__post")
.order_by("-created")
)
def get_num_new_users(self):
"""Return the number of new users
created since the GE 2024 announcement."""
return User.objects.filter(date_joined__gt=parse("2024-05-22")).count() # noqa