@allowed_methods to check for correct HTTP method
[mygpo.git] / mygpo / web / views / episode.py
blob4e5aa335be758b5dd1b0945c4d3c55caa3e44b6c
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 django.shortcuts import render_to_response
19 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseForbidden
20 from django.template import RequestContext
21 from mygpo.api.models import Podcast, Episode, Device, EpisodeAction, Subscription
22 from mygpo.api.models.episodes import Chapter
23 from mygpo.api.models.users import EpisodeFavorite
24 from mygpo.web.models import SecurityToken
25 from mygpo.web.utils import get_played_parts
26 from mygpo.decorators import manual_gc
27 from mygpo.utils import parse_time
28 from django.utils.translation import ugettext as _
29 from django.contrib.auth.decorators import login_required
30 from django.shortcuts import get_object_or_404
31 from datetime import datetime, date, timedelta
32 from django.contrib.sites.models import Site
33 import random
34 import string
36 @manual_gc
37 def episode(request, id):
38 episode = get_object_or_404(Episode, pk=id)
40 if request.user.is_authenticated():
41 history = EpisodeAction.objects.filter(user=request.user, episode=episode).order_by('-timestamp')
42 subscription_tmp = Subscription.objects.filter(podcast=episode.podcast, user=request.user)
43 if subscription_tmp.exists():
44 subscription_meta = subscription_tmp[0].get_meta()
45 else:
46 subscription_meta = None
47 is_fav = EpisodeFavorite.objects.filter(user=request.user, episode=episode).exists()
49 played_parts, duration = get_played_parts(request.user, episode)
51 else:
52 history = []
53 subscription_meta = None
54 is_fav = False
55 played_parts = None
56 duration = episode.duration
58 chapters = [c for c in Chapter.objects.filter(episode=episode).order_by('start') if c.is_public() or c.user == request.user]
60 return render_to_response('episode.html', {
61 'episode': episode,
62 'history': history,
63 'chapters': chapters,
64 'subscription_meta': subscription_meta,
65 'is_favorite': is_fav,
66 'played_parts': played_parts,
67 'duration': duration
68 }, context_instance=RequestContext(request))
71 @manual_gc
72 @login_required
73 def add_chapter(request, id):
74 episode = get_object_or_404(Episode, pk=id)
76 try:
77 start = parse_time(request.POST.get('start', '0'))
79 if request.POST.get('end', '0'):
80 end = parse_time(request.POST.get('end', '0'))
81 else:
82 end = start
84 adv = 'advertisement' in request.POST
85 label = request.POST.get('label')
87 Chapter.objects.create(user=request.user, episode=episode, start=start, end=end, advertisement=adv, label=label)
88 except:
89 pass
91 return HttpResponseRedirect('/episode/%s' % id)
94 @manual_gc
95 @login_required
96 def remove_chapter(request, id, chapter_id):
97 Chapter.objects.filter(user=request.user, id=chapter_id).delete()
99 return HttpResponseRedirect('/episode/%s' % id)
102 @manual_gc
103 @login_required
104 def toggle_favorite(request, id):
105 episode = get_object_or_404(Episode, id=id)
106 fav, c = EpisodeFavorite.objects.get_or_create(user=request.user, episode=episode)
107 if not c:
108 fav.delete()
110 return HttpResponseRedirect('/episode/%s' % id)
113 @manual_gc
114 @login_required
115 def list_favorites(request):
116 site = Site.objects.get_current()
117 episodes = [x.episode for x in EpisodeFavorite.objects.filter(user=request.user).order_by('-created')]
119 token, c = SecurityToken.objects.get_or_create(user=request.user, object='fav-feed', action='r', \
120 defaults={'token': "".join(random.sample(string.letters+string.digits, 8))})
122 from django.core.urlresolvers import reverse
123 feed_url = 'http://%s/%s' % (site.domain, reverse('favorites-feed', args=[request.user.username]))
125 try:
126 podcast = Podcast.objects.get(url=feed_url)
127 except Podcast.DoesNotExist:
128 podcast = None
130 if 'public_feed' in request.GET:
131 token.token = ''
132 token.save()
134 elif 'private_feed' in request.GET:
135 token.random_token(length=8)
136 token.save()
139 return render_to_response('favorites.html', {
140 'episodes': episodes,
141 'feed_token': token,
142 'site': site,
143 'podcast': podcast,
144 }, context_instance=RequestContext(request))