From 7b487f84940d672ca3104a41f45a76f32b4c2ac3 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Fri, 22 May 2026 11:43:31 -0400 Subject: [PATCH] [charts] Bird charts didn't use date filters --- vrobbler/apps/charts/views.py | 60 +++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 7 deletions(-) diff --git a/vrobbler/apps/charts/views.py b/vrobbler/apps/charts/views.py index f8b3410..e5743ea 100644 --- a/vrobbler/apps/charts/views.py +++ b/vrobbler/apps/charts/views.py @@ -420,6 +420,8 @@ class ChartRecordView(TemplateView): user, year=context.get("year"), month=context.get("month"), + week=context.get("week"), + day=context.get("day"), ) context["birds_chart"] = bird_data["top_birds"] context["bird_stats"] = { @@ -571,7 +573,9 @@ class ChartRecordView(TemplateView): return f"/charts/?date={year + 1}" return None - def get_bird_chart_data(self, user, year=None, month=None, limit=20): + def get_bird_chart_data( + self, user, year=None, month=None, week=None, day=None, limit=20 + ): from birds.models import Bird filters = { @@ -582,6 +586,10 @@ class ChartRecordView(TemplateView): filters["timestamp__year"] = year if month: filters["timestamp__month"] = month + if week: + filters["timestamp__week"] = week + if day: + filters["timestamp__day"] = day scrobbles = Scrobble.objects.filter(**filters) @@ -755,13 +763,23 @@ class SpotifyTracksView(TemplateView): class BirdsChartView(TemplateView): template_name = "charts/birds_chart.html" - def get_bird_data(self, user, limit=50): + def get_bird_data(self, user, year=None, month=None, week=None, day=None, limit=50): from birds.models import Bird - scrobbles = Scrobble.objects.filter( - user=user, - media_type=Scrobble.MediaType.BIRDING_LOCATION, - ) + filters = { + "user": user, + "media_type": Scrobble.MediaType.BIRDING_LOCATION, + } + if year: + filters["timestamp__year"] = year + if month: + filters["timestamp__month"] = month + if week: + filters["timestamp__week"] = week + if day: + filters["timestamp__day"] = day + + scrobbles = Scrobble.objects.filter(**filters) bird_counts = {} outings = 0 @@ -798,7 +816,35 @@ class BirdsChartView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) user = self.request.user - bird_data = self.get_bird_data(user) + + date_param = self.request.GET.get("date") + now = timezone.now() + if user.is_authenticated: + now = now_user_timezone(user.profile) + today = now.date() + + year = None + month = None + week = None + day = None + + if date_param: + parts = date_param.split("-") + year = int(parts[0]) + if len(parts) >= 2 and parts[1].startswith("W"): + week = int(parts[1].lstrip("W")) + elif len(parts) >= 2: + try: + month = int(parts[1]) + except ValueError: + pass + if len(parts) >= 3: + if parts[2].startswith("W"): + week = int(parts[2].lstrip("W")) + else: + day = int(parts[2]) + + bird_data = self.get_bird_data(user, year=year, month=month, week=week, day=day) context["birds_chart"] = bird_data["top_birds"] context["bird_stats"] = { "total_species": bird_data["total_species"],