remove unnecessary imports
[mygpo.git] / mygpo / web / views / episode.py
blobb944849a6e87ae52e62cf8c229d3c9e86a9ba470
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
20 from django.template import RequestContext
21 from mygpo.api.models import Podcast, Episode, 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.contrib.auth.decorators import login_required
29 from django.shortcuts import get_object_or_404
30 from django.contrib.sites.models import Site
31 import random
32 import string
34 @manual_gc
35 def episode(request, id):
36 episode = get_object_or_404(Episode, pk=id)
38 if request.user.is_authenticated():
39 history = EpisodeAction.objects.filter(user=request.user, episode=episode).order_by('-timestamp')
40 subscription_tmp = Subscription.objects.filter(podcast=episode.podcast, user=request.user)
41 if subscription_tmp.exists():
42 subscription_meta = subscription_tmp[0].get_meta()
43 else:
44 subscription_meta = None
45 is_fav = EpisodeFavorite.objects.filter(user=request.user, episode=episode).exists()
47 played_parts, duration = get_played_parts(request.user, episode)
49 else:
50 history = []
51 subscription_meta = None
52 is_fav = False
53 played_parts = None
54 duration = episode.duration
56 chapters = [c for c in Chapter.objects.filter(episode=episode).order_by('start') if c.is_public() or c.user == request.user]
58 return render_to_response('episode.html', {
59 'episode': episode,
60 'history': history,
61 'chapters': chapters,
62 'subscription_meta': subscription_meta,
63 'is_favorite': is_fav,
64 'played_parts': played_parts,
65 'duration': duration
66 }, context_instance=RequestContext(request))
69 @manual_gc
70 @login_required
71 def add_chapter(request, id):
72 episode = get_object_or_404(Episode, pk=id)
74 try:
75 start = parse_time(request.POST.get('start', '0'))
77 if request.POST.get('end', '0'):
78 end = parse_time(request.POST.get('end', '0'))
79 else:
80 end = start
82 adv = 'advertisement' in request.POST
83 label = request.POST.get('label')
85 Chapter.objects.create(user=request.user, episode=episode, start=start, end=end, advertisement=adv, label=label)
86 except:
87 pass
89 return HttpResponseRedirect('/episode/%s' % id)
92 @manual_gc
93 @login_required
94 def remove_chapter(request, id, chapter_id):
95 Chapter.objects.filter(user=request.user, id=chapter_id).delete()
97 return HttpResponseRedirect('/episode/%s' % id)
100 @manual_gc
101 @login_required
102 def toggle_favorite(request, id):
103 episode = get_object_or_404(Episode, id=id)
104 fav, c = EpisodeFavorite.objects.get_or_create(user=request.user, episode=episode)
105 if not c:
106 fav.delete()
108 return HttpResponseRedirect('/episode/%s' % id)
111 @manual_gc
112 @login_required
113 def list_favorites(request):
114 site = Site.objects.get_current()
115 episodes = [x.episode for x in EpisodeFavorite.objects.filter(user=request.user).order_by('-created')]
117 token, c = SecurityToken.objects.get_or_create(user=request.user, object='fav-feed', action='r', \
118 defaults={'token': "".join(random.sample(string.letters+string.digits, 8))})
120 from django.core.urlresolvers import reverse
121 feed_url = 'http://%s/%s' % (site.domain, reverse('favorites-feed', args=[request.user.username]))
123 try:
124 podcast = Podcast.objects.get(url=feed_url)
125 except Podcast.DoesNotExist:
126 podcast = None
128 if 'public_feed' in request.GET:
129 token.token = ''
130 token.save()
132 elif 'private_feed' in request.GET:
133 token.random_token(length=8)
134 token.save()
137 return render_to_response('favorites.html', {
138 'episodes': episodes,
139 'feed_token': token,
140 'site': site,
141 'podcast': podcast,
142 }, context_instance=RequestContext(request))