This commit is contained in:
89
vrobbler/apps/trends/views.py
Normal file
89
vrobbler/apps/trends/views.py
Normal file
@ -0,0 +1,89 @@
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.views.generic import TemplateView
|
||||
|
||||
from trends.models import TrendResult
|
||||
from trends.trends import TREND_REGISTRY
|
||||
|
||||
TREND_METADATA = {
|
||||
"concurrent-listening": {
|
||||
"title": "Concurrent Listening",
|
||||
"description": "What music were you listening to while on trails or at locations?",
|
||||
"icon": "🎧",
|
||||
},
|
||||
"concurrent-reading": {
|
||||
"title": "Concurrent Reading",
|
||||
"description": "What music did you listen to while reading books?",
|
||||
"icon": "📖",
|
||||
},
|
||||
"reading-pace-vs-activity": {
|
||||
"title": "Reading Pace vs Music",
|
||||
"description": "Compare how long you read per session with and without concurrent music.",
|
||||
"icon": "📊",
|
||||
},
|
||||
"trending-up": {
|
||||
"title": "Trending Media Types",
|
||||
"description": "Which media types have you been consuming more or less of recently?",
|
||||
"icon": "📈",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
class TrendListView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "trends/trend_list.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
results = {
|
||||
r.trend_slug: r
|
||||
for r in TrendResult.objects.filter(
|
||||
user=self.request.user
|
||||
)
|
||||
}
|
||||
trends = []
|
||||
for slug in TREND_REGISTRY:
|
||||
meta = TREND_METADATA.get(slug, {})
|
||||
result = results.get(slug)
|
||||
trends.append({
|
||||
"slug": slug,
|
||||
"title": meta.get("title", slug),
|
||||
"description": meta.get("description", ""),
|
||||
"icon": meta.get("icon", ""),
|
||||
"computed_at": result.computed_at if result else None,
|
||||
"has_data": result is not None,
|
||||
})
|
||||
ctx["trends"] = trends
|
||||
return ctx
|
||||
|
||||
|
||||
class TrendDetailView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "trends/trend_detail.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
slug = kwargs["trend_slug"]
|
||||
|
||||
if slug not in TREND_REGISTRY:
|
||||
ctx["trend_not_found"] = True
|
||||
return ctx
|
||||
|
||||
meta = TREND_METADATA.get(slug, {})
|
||||
ctx["trend"] = {
|
||||
"slug": slug,
|
||||
"title": meta.get("title", slug),
|
||||
"description": meta.get("description", ""),
|
||||
"icon": meta.get("icon", ""),
|
||||
}
|
||||
|
||||
result = TrendResult.objects.filter(
|
||||
user=self.request.user,
|
||||
trend_slug=slug,
|
||||
).first()
|
||||
|
||||
if result:
|
||||
ctx["computed_at"] = result.computed_at
|
||||
ctx["data"] = result.data
|
||||
else:
|
||||
ctx["computed_at"] = None
|
||||
ctx["data"] = None
|
||||
|
||||
return ctx
|
||||
Reference in New Issue
Block a user