from datetime import timedelta

from dateutil.parser import parse
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(),
            ),
            (
                "2019 General election",
                timezone.make_aware(parse("2019-10-28")),
                timezone.make_aware(parse("2019-12-15")),
            ),
        ]
        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"
            )
            .select_related("user", "person", "post")
            .order_by("-created")
        )

@@ -471,29 +471,26 @@
    %22
-2019 General e
+May 2021 E
 lection
+s
 %22,%0A 
@@ -537,16 +537,16 @@
 (%2220
-19-10-28
+21-03-01
 %22)),
@@ -595,16 +595,16 @@
 (%2220
-19-12-15
+21-05-09
 %22)),

Enable leaderboard for May 2021 elections