2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from datetime
import datetime
19 from functools
import wraps
21 import dateutil
.parser
23 from django
.shortcuts
import render
24 from django
.http
import HttpResponseRedirect
, Http404
25 from django
.contrib
.auth
.decorators
import login_required
26 from django
.contrib
.sites
.requests
import RequestSite
27 from django
.contrib
.contenttypes
.models
import ContentType
28 from django
.views
.decorators
.vary
import vary_on_cookie
29 from django
.views
.decorators
.cache
import never_cache
, cache_control
30 from django
.contrib
import messages
31 from django
.utils
.translation
import ugettext
as _
33 from mygpo
.podcasts
.models
import Podcast
, Episode
34 from mygpo
.api
.constants
import EPISODE_ACTION_TYPES
35 from mygpo
.core
.tasks
import flattr_thing
36 from mygpo
.utils
import parse_time
, get_timestamp
37 from mygpo
.users
.settings
import FLATTR_TOKEN
38 from mygpo
.history
.stats
import last_played_episodes
39 from mygpo
.publisher
.utils
import check_publisher_permission
40 from mygpo
.web
.utils
import get_episode_link_target
, check_restrictions
41 from mygpo
.history
.models
import EpisodeHistoryEntry
42 from mygpo
.favorites
.models
import FavoriteEpisode
43 from mygpo
.userfeeds
.feeds
import FavoriteFeed
47 @cache_control(private
=True)
48 def episode(request
, episode
):
50 podcast
= episode
.podcast
52 podcast
= check_restrictions(podcast
)
59 if user
.is_authenticated():
61 is_fav
= FavoriteEpisode
.objects
.filter(user
=user
, episode
=episode
)\
64 # pre-populate data for fetch_data
65 podcasts_dict
= {podcast
.get_id(): podcast
}
66 episodes_dict
= {episode
.id.hex: episode
}
68 has_history
= EpisodeHistoryEntry
.objects
.filter(user
=user
,
72 devices
= {c
.id.hex: c
for c
in user
.client_set
.all()}
73 can_flattr
= user
.profile
.settings
.get_wksetting(FLATTR_TOKEN
) and episode
.flattr_url
81 is_publisher
= check_publisher_permission(user
, podcast
)
83 prev
= None #podcast.get_episode_before(episode)
84 next
= None #podcast.get_episode_after(episode)
86 return render(request
, 'episode.html', {
91 'has_history': has_history
,
92 'is_favorite': is_fav
,
93 'actions': EPISODE_ACTION_TYPES
,
95 'can_flattr': can_flattr
,
96 'is_publisher': is_publisher
,
103 @cache_control(private
=True)
104 def history(request
, episode
):
105 """ shows the history of the episode """
108 podcast
= episode
.podcast
110 history
= EpisodeHistoryEntry
.objects
.filter(user
=user
,
112 .order_by('-timestamp')\
113 .prefetch_related('episode',
116 'episode__podcast__slugs',
119 clients
= user
.client_set
.all()
121 return render(request
, 'episode-history.html', {
125 'actions': EPISODE_ACTION_TYPES
,
132 def toggle_favorite(request
, episode
):
135 fav
, created
= FavoriteEpisode
.objects
.get_or_create(
140 # if the episode was already a favorite, remove it
144 podcast
= episode
.podcast
145 return HttpResponseRedirect(get_episode_link_target(episode
, podcast
))
150 @cache_control(private
=True)
152 def list_favorites(request
):
154 site
= RequestSite(request
)
156 favorites
= FavoriteEpisode
.episodes_for_user(user
)
158 recently_listened
= last_played_episodes(user
)
160 favfeed
= FavoriteFeed(user
)
161 feed_url
= favfeed
.get_public_url(site
.domain
)
163 podcast
= Podcast
.objects
.filter(urls__url
=feed_url
).first()
165 token
= request
.user
.profile
.favorite_feeds_token
167 return render(request
, 'favorites.html', {
168 'episodes': favorites
,
172 'recently_listened': recently_listened
,
177 def add_action(request
, episode
):
180 client
= user
.client_set
.get(id=request
.POST
.get('device'))
182 action_str
= request
.POST
.get('action')
183 timestamp
= request
.POST
.get('timestamp', '')
187 timestamp
= dateutil
.parser
.parse(timestamp
)
188 except (ValueError, AttributeError, TypeError):
189 timestamp
= datetime
.utcnow()
191 timestamp
= datetime
.utcnow()
193 EpisodeHistoryEntry
.create_entry(user
, episode
, action_str
, client
,
195 podcast
= episode
.podcast
196 return HttpResponseRedirect(get_episode_link_target(episode
, podcast
))
201 def flattr_episode(request
, episode
):
202 """ Flattrs an episode, records an event and redirects to the episode """
205 site
= RequestSite(request
)
207 # Flattr via the tasks queue, but wait for the result
208 task
= flattr_thing
.delay(user
.pk
, episode
._id
, site
.domain
,
209 request
.is_secure(), 'Episode')
210 success
, msg
= task
.get()
213 messages
.success(request
, _("Flattr\'d"))
216 messages
.error(request
, msg
)
218 podcast
= episode
.podcast
219 return HttpResponseRedirect(get_episode_link_target(episode
, podcast
))
222 # To make all view accessible via either IDs or Slugs
223 # a decorator queries the episode and passes the Id on to the
226 def slug_decorator(f
):
228 def _decorator(request
, p_slug
, e_slug
, *args
, **kwargs
):
230 pquery
= Podcast
.objects
.filter(
236 podcast
= pquery
.prefetch_related('slugs').get()
237 except Podcast
.DoesNotExist
:
240 equery
= Episode
.objects
.filter(
243 slugs__scope
=podcast
.id.hex,
247 episode
= equery
.prefetch_related('urls', 'slugs').get()
249 # set previously fetched podcast, to avoid additional query
250 episode
.podcast
= podcast
252 except Episode
.DoesNotExist
:
255 # redirect when Id or a merged (non-cannonical) slug is used
256 if episode
.slug
and episode
.slug
!= e_slug
:
257 return HttpResponseRedirect(
258 get_episode_link_target(episode
, podcast
))
260 return f(request
, episode
, *args
, **kwargs
)
267 def _decorator(request
, p_id
, e_id
, *args
, **kwargs
):
270 query
= Episode
.objects
.filter(id=e_id
,
272 episode
= query
.select_related('podcast').get()
274 except Episode
.DoesNotExist
:
277 # redirect when Id or a merged (non-cannonical) slug is used
278 if episode
.slug
and episode
.slug
!= e_id
:
279 podcast
= episode
.podcast
280 return HttpResponseRedirect(
281 get_episode_link_target(episode
, podcast
))
283 return f(request
, episode
, *args
, **kwargs
)
289 show_slug
= slug_decorator(episode
)
290 toggle_favorite_slug
= slug_decorator(toggle_favorite
)
291 add_action_slug
= slug_decorator(add_action
)
292 flattr_episode_slug
= slug_decorator(flattr_episode
)
293 episode_history_slug
= slug_decorator(history
)
295 show_id
= id_decorator(episode
)
296 toggle_favorite_id
= id_decorator(toggle_favorite
)
297 add_action_id
= id_decorator(add_action
)
298 flattr_episode_id
= id_decorator(flattr_episode
)
299 episode_history_id
= id_decorator(history
)