[podcasts] Add podcast aggregation widget
This commit is contained in:
@ -1,6 +1,9 @@
|
|||||||
|
import datetime
|
||||||
|
from django.utils import timezone
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from podcasts.models import Podcast
|
from podcasts.models import Podcast
|
||||||
|
|
||||||
|
from scrobbles.models import Scrobble
|
||||||
from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView
|
from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView
|
||||||
|
|
||||||
|
|
||||||
@ -8,6 +11,60 @@ class PodcastListView(generic.ListView):
|
|||||||
model = Podcast
|
model = Podcast
|
||||||
paginate_by = 20
|
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):
|
class PodcastDetailView(generic.DetailView):
|
||||||
model = Podcast
|
model = Podcast
|
||||||
|
|||||||
@ -1,7 +1,54 @@
|
|||||||
{% extends "base_list.html" %}
|
{% extends "base_list.html" %}
|
||||||
|
|
||||||
{% block title %}Podcasts{% endblock %}
|
{% block title %}Podcasts{% endblock %}
|
||||||
|
|
||||||
{% block lists %}
|
{% block lists %}
|
||||||
|
{% if podcasts_this_week or podcasts_this_month %}
|
||||||
|
<div class="row">
|
||||||
|
{% if podcasts_this_week or podcasts_this_month %}
|
||||||
|
<div class="col-md">
|
||||||
|
<h2>Podcasts Listened</h2>
|
||||||
|
|
||||||
|
{% if podcasts_this_week %}
|
||||||
|
<h3>This Week</h3>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-sm">
|
||||||
|
<tbody>
|
||||||
|
{% for item in podcasts_this_week %}
|
||||||
|
<tr>
|
||||||
|
<td>{{item.podcast}}</td>
|
||||||
|
<td>{{item.count}} episodes</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr><td>No podcasts listened this week</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
{% if podcasts_this_month %}
|
||||||
|
<h3>This Month</h3>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-striped table-sm">
|
||||||
|
<tbody>
|
||||||
|
{% for item in podcasts_this_month %}
|
||||||
|
<tr>
|
||||||
|
<td>{{item.podcast}}</td>
|
||||||
|
<td>{{item.count}} episodes</td>
|
||||||
|
</tr>
|
||||||
|
{% empty %}
|
||||||
|
<tr><td>No podcasts listened this month</td></tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md">
|
<div class="col-md">
|
||||||
<div class="table-responsive">
|
<div class="table-responsive">
|
||||||
|
|||||||
Reference in New Issue
Block a user