Use Django's new UUIDField
[mygpo.git] / mygpo / share / views.py
blob0013cd218f3c5169ebfb6863d52c63a5054222bc
1 from django.core.urlresolvers import reverse
2 from django.http import HttpResponseRedirect
3 from django.shortcuts import render
4 from django.contrib.sites.models import RequestSite
5 from django.contrib.auth.decorators import login_required
6 from django.views.decorators.vary import vary_on_cookie
7 from django.views.decorators.cache import cache_control
8 from django.views.generic.base import View
9 from django.utils.decorators import method_decorator
11 from mygpo.podcasts.models import Podcast
12 from mygpo.publisher.models import PublishedPodcast
13 from mygpo.userfeeds.feeds import FavoriteFeed
14 from mygpo.data.feeddownloader import update_podcast
16 import logging
17 logger = logging.getLogger(__name__)
20 class FavoritesPublic(View):
22 public = True
24 @method_decorator(vary_on_cookie)
25 @method_decorator(cache_control(private=True))
26 @method_decorator(login_required)
27 def post(self, request):
29 if self.public:
30 request.user.profile.favorite_feeds_token = ''
31 request.user.profile.save()
33 else:
34 request.user.profile.create_new_token('favorite_feeds_token')
35 request.user.profile.save()
37 return HttpResponseRedirect(reverse('share-favorites'))
41 class ShareFavorites(View):
43 @method_decorator(vary_on_cookie)
44 @method_decorator(cache_control(private=True))
45 @method_decorator(login_required)
46 def get(self, request):
47 user = request.user
49 favfeed = FavoriteFeed(user)
50 site = RequestSite(request)
51 feed_url = favfeed.get_public_url(site.domain)
53 podcast = Podcast.objects.filter(urls__url=feed_url).first()
55 token = user.profile.favorite_feeds_token
57 return render(request, 'share/favorites.html', {
58 'feed_token': token,
59 'site': site,
60 'podcast': podcast,
64 class PublicSubscriptions(View):
66 public = True
68 @method_decorator(vary_on_cookie)
69 @method_decorator(cache_control(private=True))
70 @method_decorator(login_required)
71 def post(self, request):
73 if self.public:
74 user.profile.subscriptions_token = ''
75 else:
76 user.profile.create_new_token('subscriptions_token')
78 user.profile.save()
80 return HttpResponseRedirect(reverse('share'))
83 class FavoritesFeedCreateEntry(View):
84 """ Creates a Podcast object for the user's favorites feed """
86 @method_decorator(vary_on_cookie)
87 @method_decorator(cache_control(private=True))
88 @method_decorator(login_required)
89 def post(self, request):
90 user = request.user
92 feed = FavoriteFeed(user)
93 site = RequestSite(request)
94 feed_url = feed.get_public_url(site.domain)
96 podcast = Podcast.objects.get_or_create_for_url(feed_url)
98 PublishedPodcast.objects.get_or_create(
99 podcast=podcast,
100 publisher=user,
103 update_podcast(feed_url)
105 return HttpResponseRedirect(reverse('share-favorites'))
108 @login_required
109 def overview(request):
110 user = request.user
111 site = RequestSite(request)
113 subscriptions_token = user.profile.get_token('subscriptions_token')
114 userpage_token = user.profile.get_token('userpage_token')
115 favfeed_token = user.profile.get_token('favorite_feeds_token')
117 favfeed = FavoriteFeed(user)
118 favfeed_url = favfeed.get_public_url(site.domain)
119 favfeed_podcast = Podcast.objects.filter(urls__url=favfeed_url).first()
121 return render(request, 'share/overview.html', {
122 'site': site,
123 'subscriptions_token': subscriptions_token,
124 'userpage_token': userpage_token,
125 'favfeed_token': favfeed_token,
126 'favfeed_podcast': favfeed_podcast,
130 @login_required
131 def set_token_public(request, token_name, public):
133 user = request.user
135 if public:
136 setattr(user.profile, token_name, '')
137 user.profile.save()
139 else:
140 user.profile.create_new_token(token_name)
141 user.profile.save()
143 return HttpResponseRedirect(reverse('share'))