diff --git a/vrobbler/apps/podcasts/views.py b/vrobbler/apps/podcasts/views.py index 6f7c7e4..26bedc6 100644 --- a/vrobbler/apps/podcasts/views.py +++ b/vrobbler/apps/podcasts/views.py @@ -1,6 +1,9 @@ +import datetime +from django.utils import timezone from django.views import generic from podcasts.models import Podcast +from scrobbles.models import Scrobble from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView @@ -8,6 +11,60 @@ class PodcastListView(generic.ListView): model = Podcast paginate_by = 20 + def get_context_data(self, **kwargs): + context_data = super().get_context_data(**kwargs) + user = self.request.user + now = timezone.now() + start_day_of_week = now - datetime.timedelta(days=now.weekday()) + start_day_of_month = now.replace(day=1) + + scrobbles_this_week = Scrobble.objects.filter( + user=user, + podcast_episode__isnull=False, + timestamp__gte=start_day_of_week, + ).select_related("podcast_episode", "podcast_episode__podcast") + + scrobbles_this_month = Scrobble.objects.filter( + user=user, + podcast_episode__isnull=False, + timestamp__gte=start_day_of_month, + ).select_related("podcast_episode", "podcast_episode__podcast") + + podcasts_this_week = {} + for scrobble in scrobbles_this_week: + podcast = scrobble.podcast_episode.podcast + if podcast: + podcasts_this_week[podcast.id] = { + "podcast": podcast, + "count": podcasts_this_week.get(podcast.id, {}).get( + "count", 0 + ) + + 1, + } + + podcasts_this_month = {} + for scrobble in scrobbles_this_month: + podcast = scrobble.podcast_episode.podcast + if podcast: + podcasts_this_month[podcast.id] = { + "podcast": podcast, + "count": podcasts_this_month.get(podcast.id, {}).get( + "count", 0 + ) + + 1, + } + + context_data["podcasts_this_week"] = sorted( + podcasts_this_week.values(), key=lambda x: x["count"], reverse=True + ) + context_data["podcasts_this_month"] = sorted( + podcasts_this_month.values(), + key=lambda x: x["count"], + reverse=True, + ) + + return context_data + class PodcastDetailView(generic.DetailView): model = Podcast diff --git a/vrobbler/templates/podcasts/podcast_list.html b/vrobbler/templates/podcasts/podcast_list.html index 3f63b14..aec49c4 100644 --- a/vrobbler/templates/podcasts/podcast_list.html +++ b/vrobbler/templates/podcasts/podcast_list.html @@ -1,7 +1,54 @@ {% extends "base_list.html" %} {% block title %}Podcasts{% endblock %} + {% block lists %} +{% if podcasts_this_week or podcasts_this_month %} +
+ {% if podcasts_this_week or podcasts_this_month %} +
+

Podcasts Listened

+ + {% if podcasts_this_week %} +

This Week

+
+ + + {% for item in podcasts_this_week %} + + + + + {% empty %} + + {% endfor %} + +
{{item.podcast}}{{item.count}} episodes
No podcasts listened this week
+
+ {% endif %} + + {% if podcasts_this_month %} +

This Month

+
+ + + {% for item in podcasts_this_month %} + + + + + {% empty %} + + {% endfor %} + +
{{item.podcast}}{{item.count}} episodes
No podcasts listened this month
+
+ {% endif %} +
+ {% endif %} +
+{% endif %} +