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
17 logger
= logging
.getLogger(__name__
)
20 class FavoritesPublic(View
):
24 @method_decorator(vary_on_cookie
)
25 @method_decorator(cache_control(private
=True))
26 @method_decorator(login_required
)
27 def post(self
, request
):
30 request
.user
.profile
.favorite_feeds_token
= ''
31 request
.user
.profile
.save()
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
):
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', {
64 class PublicSubscriptions(View
):
68 @method_decorator(vary_on_cookie
)
69 @method_decorator(cache_control(private
=True))
70 @method_decorator(login_required
)
71 def post(self
, request
):
74 user
.profile
.subscriptions_token
= ''
76 user
.profile
.create_new_token('subscriptions_token')
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
):
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(
103 update_podcast(feed_url
)
105 return HttpResponseRedirect(reverse('share-favorites'))
109 def overview(request
):
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', {
123 'subscriptions_token': subscriptions_token
,
124 'userpage_token': userpage_token
,
125 'favfeed_token': favfeed_token
,
126 'favfeed_podcast': favfeed_podcast
,
131 def set_token_public(request
, token_name
, public
):
136 setattr(user
.profile
, token_name
, '')
140 user
.profile
.create_new_token(token_name
)
143 return HttpResponseRedirect(reverse('share'))