[charts] Big revamp of the charts app
This commit is contained in:
417
vrobbler/apps/charts/views.py
Normal file
417
vrobbler/apps/charts/views.py
Normal file
@ -0,0 +1,417 @@
|
||||
import calendar
|
||||
from datetime import timedelta
|
||||
|
||||
from charts.models import ChartRecord
|
||||
from django.db.models import Q
|
||||
from django.utils import timezone
|
||||
from django.views.generic import TemplateView
|
||||
from profiles.utils import now_user_timezone
|
||||
|
||||
MEDIA_TYPE_FILTERS = {
|
||||
"artist": Q(artist__isnull=False),
|
||||
"album": Q(album__isnull=False),
|
||||
"track": Q(track__isnull=False),
|
||||
"tv_series": Q(tv_series__isnull=False),
|
||||
"video": Q(video__isnull=False),
|
||||
"podcast": Q(podcast__isnull=False),
|
||||
"podcast_episode": Q(podcast_episode__isnull=False),
|
||||
"board_game": Q(board_game__isnull=False),
|
||||
"trail": Q(trail__isnull=False),
|
||||
"geo_location": Q(geo_location__isnull=False),
|
||||
"food": Q(food__isnull=False),
|
||||
"book": Q(book__isnull=False),
|
||||
}
|
||||
|
||||
|
||||
class ChartRecordView(TemplateView):
|
||||
template_name = "charts/chart_index.html"
|
||||
|
||||
def get_charts(
|
||||
self,
|
||||
user,
|
||||
media_type: str,
|
||||
year=None,
|
||||
month=None,
|
||||
week=None,
|
||||
day=None,
|
||||
limit=20,
|
||||
):
|
||||
params = {"user": user}
|
||||
if year is not None:
|
||||
params["year"] = year
|
||||
if month is not None:
|
||||
params["month"] = month
|
||||
if week is not None:
|
||||
params["week"] = week
|
||||
if day is not None:
|
||||
params["day"] = day
|
||||
|
||||
filter_key = media_type.lower()
|
||||
media_filter = MEDIA_TYPE_FILTERS.get(filter_key, Q())
|
||||
|
||||
return ChartRecord.objects.filter(media_filter, **params).order_by("rank")[
|
||||
:limit
|
||||
]
|
||||
|
||||
def get_charts_for_period(
|
||||
self,
|
||||
user,
|
||||
media_type: str,
|
||||
year=None,
|
||||
month=None,
|
||||
week=None,
|
||||
day=None,
|
||||
limit=20,
|
||||
):
|
||||
"""Get charts for a specific period, properly filtering by period type."""
|
||||
params = {"user": user}
|
||||
if year is not None:
|
||||
params["year"] = year
|
||||
if month is not None:
|
||||
params["month"] = month
|
||||
if week is not None:
|
||||
params["week"] = week
|
||||
if day is not None:
|
||||
params["day"] = day
|
||||
|
||||
filter_key = media_type.lower()
|
||||
media_filter = MEDIA_TYPE_FILTERS.get(filter_key, Q())
|
||||
|
||||
qs = ChartRecord.objects.filter(media_filter, **params)
|
||||
|
||||
if day is not None:
|
||||
qs = qs.filter(day__isnull=False)
|
||||
elif week is not None:
|
||||
qs = qs.filter(week__isnull=False, day__isnull=True)
|
||||
elif month is not None:
|
||||
qs = qs.filter(month__isnull=False, week__isnull=True, day__isnull=True)
|
||||
else:
|
||||
qs = qs.filter(month__isnull=True, week__isnull=True, day__isnull=True)
|
||||
|
||||
return qs.order_by("rank")[:limit]
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
user = self.request.user
|
||||
chart_type = self.request.GET.get("chart_type", "standard")
|
||||
context["chart_type"] = chart_type
|
||||
|
||||
date_param = self.request.GET.get("date")
|
||||
|
||||
now = timezone.now()
|
||||
if user.is_authenticated:
|
||||
now = now_user_timezone(user.profile)
|
||||
today = now.date()
|
||||
current_year = today.year
|
||||
current_month = today.month
|
||||
current_week = today.isocalendar()[1]
|
||||
current_day = today.day
|
||||
|
||||
context["current_year"] = current_year
|
||||
context["current_month"] = current_month
|
||||
context["current_week"] = current_week
|
||||
context["current_day"] = current_day
|
||||
|
||||
if chart_type == "maloja":
|
||||
context["chart_keys"] = {
|
||||
"today": "Today",
|
||||
"week": "This Week",
|
||||
"month": "This Month",
|
||||
"year": "This Year",
|
||||
"all": "All Time",
|
||||
}
|
||||
|
||||
context["maloja_charts"] = {
|
||||
"artist": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
week=current_week,
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "artist", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "artist")),
|
||||
},
|
||||
"track": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user, "track", year=current_year, week=current_week
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "track", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "track")),
|
||||
},
|
||||
"tv_series": {
|
||||
"today": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
day=current_day,
|
||||
)
|
||||
),
|
||||
"week": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
week=current_week,
|
||||
)
|
||||
),
|
||||
"month": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=current_year,
|
||||
month=current_month,
|
||||
)
|
||||
),
|
||||
"year": list(
|
||||
self.get_charts_for_period(user, "tv_series", year=current_year)
|
||||
),
|
||||
"all": list(self.get_charts_for_period(user, "tv_series")),
|
||||
},
|
||||
}
|
||||
return context
|
||||
|
||||
if not date_param:
|
||||
context["period"] = "current"
|
||||
context["year"] = current_year
|
||||
context["month"] = current_month
|
||||
context["week"] = current_week
|
||||
context["day"] = current_day
|
||||
|
||||
context["charts"] = {
|
||||
"artist": list(
|
||||
self.get_charts_for_period(
|
||||
user, "artist", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"album": list(
|
||||
self.get_charts_for_period(
|
||||
user, "album", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"track": list(
|
||||
self.get_charts_for_period(
|
||||
user, "track", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"tv_series": list(
|
||||
self.get_charts_for_period(
|
||||
user, "tv_series", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"video": list(
|
||||
self.get_charts_for_period(
|
||||
user, "video", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"board_game": list(
|
||||
self.get_charts_for_period(
|
||||
user, "board_game", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"book": list(
|
||||
self.get_charts_for_period(
|
||||
user, "book", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"food": list(
|
||||
self.get_charts_for_period(
|
||||
user, "food", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"podcast": list(
|
||||
self.get_charts_for_period(
|
||||
user, "podcast", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
"trail": list(
|
||||
self.get_charts_for_period(
|
||||
user, "trail", year=current_year, limit=20
|
||||
)
|
||||
),
|
||||
}
|
||||
else:
|
||||
parts = date_param.split("-")
|
||||
year = int(parts[0])
|
||||
|
||||
week = None
|
||||
month = None
|
||||
day = None
|
||||
|
||||
if len(parts) >= 2 and parts[1].startswith("W"):
|
||||
week = int(parts[1].lstrip("W"))
|
||||
elif len(parts) >= 2 and parts[1]:
|
||||
try:
|
||||
month = int(parts[1])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if len(parts) >= 3:
|
||||
if parts[2].startswith("W"):
|
||||
week = int(parts[2].lstrip("W"))
|
||||
elif not parts[2].startswith("W"):
|
||||
day = int(parts[2])
|
||||
|
||||
context["period"] = "historical"
|
||||
context["year"] = year
|
||||
context["month"] = month
|
||||
context["week"] = week
|
||||
context["day"] = day
|
||||
|
||||
period_str = str(year)
|
||||
if month:
|
||||
period_str = f"{calendar.month_name[month]} {period_str}"
|
||||
if week:
|
||||
period_str = f"Week {week}, {period_str}"
|
||||
if day:
|
||||
period_str = f"{calendar.month_name[month]} {day}, {year}"
|
||||
context["period_str"] = period_str
|
||||
|
||||
context["charts"] = {
|
||||
"artist": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"artist",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"album": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"album",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"track": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"track",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"tv_series": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"tv_series",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"video": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"video",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"board_game": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"board_game",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"book": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"book",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"food": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"food",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"podcast": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"podcast",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
"trail": list(
|
||||
self.get_charts_for_period(
|
||||
user,
|
||||
"trail",
|
||||
year=year,
|
||||
month=month,
|
||||
week=week,
|
||||
day=day,
|
||||
)
|
||||
),
|
||||
}
|
||||
|
||||
return context
|
||||
Reference in New Issue
Block a user