1 from datetime
import datetime
, timedelta
3 from functools
import partial
5 from django
.shortcuts
import render
6 from django
.views
.generic
.base
import View
7 from django
.utils
.decorators
import method_decorator
8 from django
.contrib
.sites
.models
import RequestSite
10 from mygpo
.users
.models
import User
11 from mygpo
.users
.models
import HistoryEntry
12 from mygpo
.users
.settings
import FLATTR_USERNAME
13 from mygpo
.decorators
import requires_token
14 from mygpo
.web
.utils
import fetch_episode_data
15 from mygpo
.users
.subscriptions
import PodcastPercentageListenedSorter
16 from mygpo
.web
.views
import GeventView
17 from mygpo
.db
.couchdb
.episode
import favorite_episodes_for_user
18 from mygpo
.db
.couchdb
.user
import get_latest_episodes
, \
19 get_num_played_episodes
, get_seconds_played
20 from mygpo
.db
.couchdb
.podcastlist
import podcastlists_for_user
24 class UserpageView(GeventView
):
25 """ Shows the profile page for a user """
27 @method_decorator(requires_token(token_name
='userpage_token',
28 denied_template
='userpage-denied.html'))
29 def get(self
, request
, username
):
31 user
= User
.get_user(username
)
32 month_ago
= datetime
.today() - timedelta(days
=31)
33 site
= RequestSite(request
)
36 'lists': partial(self
.get_podcast_lists
, user
),
37 'subscriptions': partial(self
.get_subscriptions
, user
),
38 'recent_episodes': partial(self
.get_recent_episodes
, user
),
39 'seconds_played_total': partial(self
.get_seconds_played_total
, user
),
40 'seconds_played_month': partial(self
.get_seconds_played_since
, user
, month_ago
),
41 'favorite_episodes': partial(self
.get_favorite_episodes
, user
),
42 'num_played_episodes_total': partial(self
.get_played_episodes_total
, user
),
43 'num_played_episodes_month': partial(self
.get_played_episodes_since
, user
, month_ago
),
48 'flattr_username': user
.get_wksetting(FLATTR_USERNAME
),
50 'subscriptions_token': user
.get_token('subscriptions_token'),
51 'favorite_feeds_token': user
.get_token('favorite_feeds_token'),
53 context
.update(self
.get_context(context_funs
))
55 return render(request
, 'userpage.html', context
)
58 def get_podcast_lists(self
, user
):
59 return podcastlists_for_user(user
._id
)
62 def get_subscriptions(self
, user
):
63 subscriptions
= user
.get_subscribed_podcasts()
64 return PodcastPercentageListenedSorter(subscriptions
, user
)
67 def get_recent_episodes(self
, user
):
68 recent_episodes
= get_latest_episodes(user
)
69 return fetch_episode_data(recent_episodes
)
72 def get_seconds_played_total(self
, user
):
73 return get_seconds_played(user
)
76 def get_seconds_played_since(self
, user
, since
):
77 return get_seconds_played(user
, since
=since
)
80 def get_favorite_episodes(self
, user
):
81 favorite_episodes
= favorite_episodes_for_user(user
)
82 return fetch_episode_data(favorite_episodes
)
85 def get_played_episodes_total(self
, user
):
86 return get_num_played_episodes(user
)
89 def get_played_episodes_since(self
, user
, since
):
90 return get_num_played_episodes(user
, since
=since
)