1 from functools
import wraps
4 from django
.shortcuts
import render
5 from django
.http
import HttpResponse
, HttpResponseRedirect
, \
6 HttpResponseForbidden
, Http404
7 from django
.views
.decorators
.cache
import never_cache
, cache_control
8 from django
.views
.decorators
.vary
import vary_on_cookie
9 from django
.core
.urlresolvers
import reverse
10 from django
.utils
.translation
import ugettext
as _
11 from django
.contrib
import messages
13 from mygpo
.core
.proxy
import proxy_object
14 from mygpo
.publisher
.auth
import require_publisher
, is_publisher
15 from mygpo
.publisher
.forms
import SearchPodcastForm
16 from mygpo
.publisher
.utils
import listener_data
, episode_listener_data
, \
17 check_publisher_permission
, subscriber_data
18 from mygpo
.web
.heatmap
import EpisodeHeatmap
19 from mygpo
.web
.views
.episode
import oldid_decorator
, slug_id_decorator
20 from mygpo
.web
.views
.podcast
import \
21 slug_id_decorator
as podcast_slug_id_decorator
, \
22 oldid_decorator
as podcast_oldid_decorator
23 from mygpo
.web
.utils
import get_podcast_link_target
, normalize_twitter
24 from django
.contrib
.sites
.models
import RequestSite
25 from mygpo
.data
.feeddownloader
import PodcastUpdater
26 from mygpo
.decorators
import requires_token
, allowed_methods
27 from mygpo
.users
.models
import User
28 from mygpo
.db
.couchdb
.episode
import episodes_for_podcast
29 from mygpo
.db
.couchdb
.podcast
import podcast_by_id
, podcasts_by_id
, \
30 podcast_for_url
, podcastgroup_for_slug_id
, podcastgroup_for_oldid
, \
31 podcastgroup_by_id
, update_additional_data
32 from mygpo
.db
.couchdb
.episode_state
import episode_listener_counts
36 @cache_control(private
=True)
38 if is_publisher(request
.user
):
39 podcasts
= podcasts_by_id(request
.user
.published_objects
)
40 site
= RequestSite(request
)
41 update_token
= request
.user
.get_token('publisher_update_token')
42 form
= SearchPodcastForm()
43 return render(request
, 'publisher/home.html', {
44 'update_token': update_token
,
51 site
= RequestSite(request
)
52 return render(request
, 'publisher/info.html', {
58 @cache_control(private
=True)
60 def search_podcast(request
):
61 form
= SearchPodcastForm(request
.POST
)
63 url
= form
.cleaned_data
['url']
65 podcast
= podcast_for_url(url
)
69 url
= get_podcast_link_target(podcast
, 'podcast-publisher-detail')
71 url
= reverse('publisher')
73 return HttpResponseRedirect(url
)
77 @cache_control(private
=True)
79 @allowed_methods(['GET', 'POST'])
80 def podcast(request
, podcast
):
82 if not check_publisher_permission(request
.user
, podcast
):
83 return HttpResponseForbidden()
85 timeline_data
= listener_data([podcast
])
86 subscription_data
= subscriber_data([podcast
])[-20:]
89 group
= podcastgroup_by_id(podcast
.group
)
93 update_token
= request
.user
.publisher_update_token
95 heatmap
= EpisodeHeatmap(podcast
.get_id())
97 site
= RequestSite(request
)
98 feedurl_quoted
= urllib
.quote(podcast
.url
)
100 return render(request
, 'publisher/podcast.html', {
105 'timeline_data': timeline_data
,
106 'subscriber_data': subscription_data
,
107 'update_token': update_token
,
109 'feedurl_quoted': feedurl_quoted
,
114 @cache_control(private
=True)
116 def group(request
, group
):
118 podcasts
= group
.podcasts
120 # users need to have publisher access for at least one of the group's podcasts
121 if not any([check_publisher_permission(request
.user
, p
) for p
in podcasts
]):
122 return HttpResponseForbidden()
124 timeline_data
= listener_data(podcasts
)
125 subscription_data
= list(subscriber_data(podcasts
))[-20:]
127 return render(request
, 'publisher/group.html', {
129 'timeline_data': timeline_data
,
130 'subscriber_data': subscription_data
,
135 @cache_control(private
=True)
137 def update_podcast(request
, podcast
):
139 if not check_publisher_permission(request
.user
, podcast
):
140 return HttpResponseForbidden()
142 updater
= PodcastUpdater( [podcast
.url
] )
145 url
= get_podcast_link_target(podcast
, 'podcast-publisher-detail')
146 return HttpResponseRedirect(url
)
150 @cache_control(private
=True)
152 def save_podcast(request
, podcast
):
153 twitter
= normalize_twitter(request
.POST
.get('twitter', ''))
154 update_additional_data(podcast
, twitter
)
155 messages
.success(request
, _('Data updated'))
156 url
= get_podcast_link_target(podcast
, 'podcast-publisher-detail')
157 return HttpResponseRedirect(url
)
163 def new_update_token(request
, username
):
164 request
.user
.create_new_token('publisher_update_token')
166 messages
.success(request
, _('Publisher token updated'))
167 return HttpResponseRedirect(reverse('publisher'))
171 @requires_token(token_name
='publisher_update_token')
172 def update_published_podcasts(request
, username
):
173 user
= User
.get_user(username
)
177 published_podcasts
= podcasts_by_id(user
.published_objects
)
178 updater
= PodcastUpdater(podcast
.url
for podcast
in published_podcasts
)
181 return HttpResponse('Updated:\n' + '\n'.join([p
.url
for p
in published_podcasts
]), mimetype
='text/plain')
185 @cache_control(private
=True)
187 def episodes(request
, podcast
):
189 if not check_publisher_permission(request
.user
, podcast
):
190 return HttpResponseForbidden()
192 episodes
= episodes_for_podcast(podcast
, descending
=True)
193 listeners
= dict(episode_listener_counts(podcast
))
195 max_listeners
= max(listeners
.values() + [0])
197 def annotate_episode(episode
):
198 listener_count
= listeners
.get(episode
._id
, None)
199 return proxy_object(episode
, listeners
=listener_count
)
201 episodes
= map(annotate_episode
, episodes
)
203 return render(request
, 'publisher/episodes.html', {
205 'episodes': episodes
,
206 'max_listeners': max_listeners
212 @cache_control(private
=True)
213 @allowed_methods(['GET', 'POST'])
214 def episode(request
, episode
):
216 podcast
= podcast_by_id(episode
.podcast
)
218 if not check_publisher_permission(request
.user
, podcast
):
219 return HttpResponseForbidden()
221 if request
.method
== 'POST':
222 form
= None #EpisodeForm(request.POST, instance=e)
226 elif request
.method
== 'GET':
227 form
= None #EpisodeForm(instance=e)
229 timeline_data
= list(episode_listener_data(episode
))
231 heatmap
= EpisodeHeatmap(episode
.podcast
, episode
._id
,
232 duration
=episode
.duration
)
234 return render(request
, 'publisher/episode.html', {
238 'timeline_data': timeline_data
,
244 @cache_control(private
=True)
246 current_site
= RequestSite(request
)
247 return render(request
, 'link.html', {
253 @cache_control(private
=True)
254 def advertise(request
):
255 site
= RequestSite(request
)
256 return render(request
, 'publisher/advertise.html', {
261 def group_slug_id_decorator(f
):
263 def _decorator(request
, slug_id
, *args
, **kwargs
):
264 group
= podcastgroup_for_slug_id(slug_id
)
269 return f(request
, group
, *args
, **kwargs
)
274 def group_oldid_decorator(f
):
276 def _decorator(request
, pid
, *args
, **kwargs
):
279 except (TypeError, ValueError):
282 group
= podcastgroup_for_oldid(pid
)
287 return f(request
, group
, *args
, **kwargs
)
293 episode_oldid
= oldid_decorator(episode
)
294 podcast_oldid
= podcast_oldid_decorator(podcast
)
295 update_podcast_oldid
= podcast_oldid_decorator(update_podcast
)
296 save_podcast_oldid
= podcast_oldid_decorator(save_podcast
)
297 episodes_oldid
= podcast_oldid_decorator(episodes
)
298 group_oldid
= group_oldid_decorator(group
)
300 episode_slug_id
= slug_id_decorator(episode
)
301 podcast_slug_id
= podcast_slug_id_decorator(podcast
)
302 episodes_slug_id
= podcast_slug_id_decorator(episodes
)
303 update_podcast_slug_id
= podcast_slug_id_decorator(update_podcast
)
304 save_podcast_slug_id
= podcast_slug_id_decorator(save_podcast
)
305 group_slug_id
= group_slug_id_decorator(group
)