Merge branch 'master' into upload-timestamps
[mygpo.git] / mygpo / publisher / views.py
blob5008c8ace63409695179fe107c2fe4512224086f
1 from functools import wraps
2 import urllib
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
35 @vary_on_cookie
36 @cache_control(private=True)
37 def home(request):
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,
45 'podcasts': podcasts,
46 'form': form,
47 'site': site,
50 else:
51 site = RequestSite(request)
52 return render(request, 'publisher/info.html', {
53 'site': site
57 @vary_on_cookie
58 @cache_control(private=True)
59 @require_publisher
60 def search_podcast(request):
61 form = SearchPodcastForm(request.POST)
62 if form.is_valid():
63 url = form.cleaned_data['url']
65 podcast = podcast_for_url(url)
66 if not podcast:
67 raise Http404
69 url = get_podcast_link_target(podcast, 'podcast-publisher-detail')
70 else:
71 url = reverse('publisher')
73 return HttpResponseRedirect(url)
76 @vary_on_cookie
77 @cache_control(private=True)
78 @require_publisher
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:]
88 if podcast.group:
89 group = podcastgroup_by_id(podcast.group)
90 else:
91 group = None
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', {
101 'site': site,
102 'podcast': podcast,
103 'group': group,
104 'form': None,
105 'timeline_data': timeline_data,
106 'subscriber_data': subscription_data,
107 'update_token': update_token,
108 'heatmap': heatmap,
109 'feedurl_quoted': feedurl_quoted,
113 @vary_on_cookie
114 @cache_control(private=True)
115 @require_publisher
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', {
128 'group': group,
129 'timeline_data': timeline_data,
130 'subscriber_data': subscription_data,
134 @vary_on_cookie
135 @cache_control(private=True)
136 @require_publisher
137 def update_podcast(request, podcast):
139 if not check_publisher_permission(request.user, podcast):
140 return HttpResponseForbidden()
142 updater = PodcastUpdater( [podcast.url] )
143 updater.update()
145 url = get_podcast_link_target(podcast, 'podcast-publisher-detail')
146 return HttpResponseRedirect(url)
149 @vary_on_cookie
150 @cache_control(private=True)
151 @require_publisher
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)
161 @never_cache
162 @require_publisher
163 def new_update_token(request, username):
164 request.user.create_new_token('publisher_update_token')
165 request.user.save()
166 messages.success(request, _('Publisher token updated'))
167 return HttpResponseRedirect(reverse('publisher'))
170 @never_cache
171 @requires_token(token_name='publisher_update_token')
172 def update_published_podcasts(request, username):
173 user = User.get_user(username)
174 if not user:
175 raise Http404
177 published_podcasts = podcasts_by_id(user.published_objects)
178 updater = PodcastUpdater(podcast.url for podcast in published_podcasts)
179 updater.update()
181 return HttpResponse('Updated:\n' + '\n'.join([p.url for p in published_podcasts]), mimetype='text/plain')
184 @vary_on_cookie
185 @cache_control(private=True)
186 @require_publisher
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', {
204 'podcast': podcast,
205 'episodes': episodes,
206 'max_listeners': max_listeners
210 @require_publisher
211 @vary_on_cookie
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)
223 #if form.is_valid():
224 # form.save()
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', {
235 'episode': episode,
236 'podcast': podcast,
237 'form': form,
238 'timeline_data': timeline_data,
239 'heatmap': heatmap,
243 @vary_on_cookie
244 @cache_control(private=True)
245 def link(request):
246 current_site = RequestSite(request)
247 return render(request, 'link.html', {
248 'url': current_site
252 @vary_on_cookie
253 @cache_control(private=True)
254 def advertise(request):
255 site = RequestSite(request)
256 return render(request, 'publisher/advertise.html', {
257 'site': site
261 def group_slug_id_decorator(f):
262 @wraps(f)
263 def _decorator(request, slug_id, *args, **kwargs):
264 group = podcastgroup_for_slug_id(slug_id)
266 if podcast is None:
267 raise Http404
269 return f(request, group, *args, **kwargs)
271 return _decorator
274 def group_oldid_decorator(f):
275 @wraps(f)
276 def _decorator(request, pid, *args, **kwargs):
277 try:
278 pid = int(pid)
279 except (TypeError, ValueError):
280 raise Http404
282 group = podcastgroup_for_oldid(pid)
284 if not podcast:
285 raise Http404
287 return f(request, group, *args, **kwargs)
289 return _decorator
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)