Merge pull request #799 from maxthomas/fix-openapi-comment
[mygpo.git] / mygpo / share / userpage.py
blobf1d16ee7efd0abeafdcbe65f6fddd2d9169dd203
1 from datetime import datetime, timedelta
3 from django.shortcuts import render
4 from django.views import View
5 from django.utils.decorators import method_decorator
6 from django.contrib.sites.requests import RequestSite
7 from django.contrib.auth import get_user_model
9 from mygpo.podcasts.models import Episode
10 from mygpo.users.models import HistoryEntry
11 from mygpo.subscriptions import get_subscribed_podcasts
12 from mygpo.decorators import requires_token
13 from mygpo.podcastlists.models import PodcastList
14 from mygpo.users.subscriptions import PodcastPercentageListenedSorter
15 from mygpo.history.stats import (
16 num_played_episodes,
17 last_played_episodes,
18 seconds_played,
20 from mygpo.favorites.models import FavoriteEpisode
23 class UserpageView(View):
24 """Shows the profile page for a user"""
26 @method_decorator(
27 requires_token(
28 token_name="userpage_token", denied_template="userpage-denied.html"
31 def get(self, request, username):
33 User = get_user_model()
34 user = User.objects.get(username=username)
35 month_ago = datetime.today() - timedelta(days=31)
36 site = RequestSite(request)
38 context = {
39 "page_user": user,
40 "site": site.domain,
41 "subscriptions_token": user.profile.get_token("subscriptions_token"),
42 "favorite_feeds_token": user.profile.get_token("favorite_feeds_token"),
43 "lists": self.get_podcast_lists(user),
44 "subscriptions": self.get_subscriptions(user),
45 "recent_episodes": last_played_episodes(user),
46 "seconds_played_total": seconds_played(user),
47 "seconds_played_month": seconds_played(user, month_ago),
48 "favorite_episodes": FavoriteEpisode.episodes_for_user(user),
49 "num_played_episodes_total": num_played_episodes(user),
50 "num_played_episodes_month": num_played_episodes(user, month_ago),
53 return render(request, "userpage.html", context)
55 def get_podcast_lists(self, user):
56 return PodcastList.objects.filter(user=user)
58 def get_subscriptions(self, user):
59 subscriptions = [sp.podcast for sp in get_subscribed_podcasts(user)]
60 return PodcastPercentageListenedSorter(subscriptions, user)