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
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()
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
)
51 subscription_meta
= 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', {
62 'subscription_meta': subscription_meta
,
63 'is_favorite': is_fav
,
64 'played_parts': played_parts
,
66 }, context_instance
=RequestContext(request
))
71 def add_chapter(request
, id):
72 episode
= get_object_or_404(Episode
, pk
=id)
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'))
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
)
89 return HttpResponseRedirect('/episode/%s' % id)
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)
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
)
108 return HttpResponseRedirect('/episode/%s' % id)
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
]))
124 podcast
= Podcast
.objects
.get(url
=feed_url
)
125 except Podcast
.DoesNotExist
:
128 if 'public_feed' in request
.GET
:
132 elif 'private_feed' in request
.GET
:
133 token
.random_token(length
=8)
137 return render_to_response('favorites.html', {
138 'episodes': episodes
,
142 }, context_instance
=RequestContext(request
))