[charts] Bird charts didn't use date filters
This commit is contained in:
@ -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"],
|
||||
|
||||
Reference in New Issue
Block a user