1 from datetime
import datetime
3 from django
.core
.urlresolvers
import reverse
4 from django
.contrib
.auth
.decorators
import login_required
5 from django
.contrib
.sites
.models
import RequestSite
6 from django
.shortcuts
import render
, get_object_or_404
7 from django
.http
import HttpResponse
8 from django
.views
.decorators
.vary
import vary_on_cookie
9 from django
.views
.decorators
.cache
import cache_control
10 from django
.utils
.translation
import ugettext
as _
11 from django
.contrib
.syndication
.views
import Feed
12 from django
.contrib
.auth
import get_user_model
14 from mygpo
.podcasts
.models
import Podcast
15 from mygpo
.subscriptions
.models
import Subscription
, PodcastConfig
16 from mygpo
.users
.settings
import PUBLIC_SUB_PODCAST
17 from mygpo
.utils
import unzip
, skip_pairs
18 from mygpo
.api
import simple
19 from mygpo
.subscriptions
import get_subscribed_podcasts
20 from mygpo
.decorators
import requires_token
21 from mygpo
.users
.models
import HistoryEntry
22 from mygpo
.subscriptions
import (get_subscribed_podcasts
,
23 get_subscription_change_history
, get_subscription_history
)
24 from mygpo
.web
.utils
import get_podcast_link_target
28 @cache_control(private
=True)
30 def show_list(request
):
31 current_site
= RequestSite(request
)
32 subscriptionlist
= create_subscriptionlist(request
)
33 return render(request
, 'subscriptions.html', {
34 'subscriptionlist': subscriptionlist
,
39 @cache_control(private
=True)
41 def download_all(request
):
42 podcasts
= get_subscribed_podcasts(request
.user
)
43 response
= simple
.format_podcast_list(podcasts
, 'opml', request
.user
.username
)
44 response
['Content-Disposition'] = 'attachment; filename=all-subscriptions.opml'
48 def create_subscriptionlist(request
):
51 # get all non-deleted subscriptions
52 subscriptions
= Subscription
.objects
.filter(user
=user
)\
53 .exclude(deleted
=True)\
54 .select_related('podcast', 'client')
56 # grou clients by subscribed podcasts
57 subscription_list
= {}
58 for subscription
in subscriptions
:
59 podcast
= subscription
.podcast
61 if not podcast
in subscription_list
:
62 subscription_list
[podcast
] = {
65 'episodes': podcast
.episode_count
,
68 subscription_list
[podcast
]['devices'].append(subscription
.client
)
70 # sort most recently updated podcast first
71 subscriptions
= subscription_list
.values()
72 now
= datetime
.utcnow()
73 sort_key
= lambda s
: s
['podcast'].latest_episode_timestamp
or now
74 subscriptions
= sorted(subscriptions
, key
=sort_key
, reverse
=True)
78 @requires_token(token_name
='subscriptions_token')
79 def subscriptions_feed(request
, username
):
80 # Create to feed manually so we can wrap the token-authentication around it
81 f
= SubscriptionsFeed(username
)
82 obj
= f
.get_object(request
, username
)
83 feedgen
= f
.get_feed(obj
, request
)
84 response
= HttpResponse(content_type
=feedgen
.mime_type
)
85 feedgen
.write(response
, 'utf-8')
89 class SubscriptionsFeed(Feed
):
90 """ A feed showing subscription changes for a certain user """
94 def __init__(self
, username
):
95 self
.username
= username
97 def get_object(self
, request
, username
):
98 self
.site
= RequestSite(request
)
99 User
= get_user_model()
100 user
= get_object_or_404(User
, username
=username
)
103 def title(self
, user
):
104 return _('%(username)s\'s Podcast Subscriptions on %(site)s') % \
105 dict(username
=user
.username
, site
=self
.site
)
107 def description(self
, user
):
108 return _('Recent changes to %(username)s\'s podcast subscriptions on %(site)s') % \
109 dict(username
=user
.username
, site
=self
.site
)
111 def link(self
, user
):
112 return reverse('shared-subscriptions', args
=[user
.username
])
114 def items(self
, user
):
115 history
= get_subscription_history(user
, public_only
=True)
116 history
= get_subscription_change_history(history
)
117 history
= list(history
)[-self
.NUM_ITEMS
:]
120 def author_name(self
, user
):
123 def author_link(self
, user
):
124 return reverse('shared-subscriptions', args
=[user
.username
])
126 # entry-specific data below
128 description_template
= "subscription-feed-description.html"
130 def item_title(self
, entry
):
131 if entry
.action
== 'subscribe':
132 s
= _('%(username)s subscribed to %(podcast)s (%(site)s)')
134 s
= _('%(username)s unsubscribed from %(podcast)s (%(site)s)')
136 return s
% dict(username
=self
.username
,
137 podcast
=entry
.podcast
.display_title
,
140 def item_link(self
, item
):
141 return get_podcast_link_target(item
.podcast
)
143 def item_pubdate(self
, item
):
144 return item
.timestamp