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
.requests
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
16 from mygpo
.users
.settings
import PUBLIC_SUB_PODCAST
17 from mygpo
.api
import simple
18 from mygpo
.subscriptions
import get_subscribed_podcasts
19 from mygpo
.decorators
import requires_token
20 from mygpo
.users
.models
import HistoryEntry
21 from mygpo
.subscriptions
import (get_subscribed_podcasts
,
22 get_subscription_change_history
, get_subscription_history
)
23 from mygpo
.web
.utils
import get_podcast_link_target
27 @cache_control(private
=True)
29 def show_list(request
):
30 current_site
= RequestSite(request
)
31 subscriptionlist
= create_subscriptionlist(request
)
32 return render(request
, 'subscriptions.html', {
33 'subscriptionlist': subscriptionlist
,
38 @cache_control(private
=True)
40 def download_all(request
):
41 podcasts
= get_subscribed_podcasts(request
.user
)
42 response
= simple
.format_podcast_list(podcasts
, 'opml', request
.user
.username
)
43 response
['Content-Disposition'] = 'attachment; filename=all-subscriptions.opml'
47 def create_subscriptionlist(request
):
50 # get all non-deleted subscriptions
51 subscriptions
= Subscription
.objects
.filter(user
=user
)\
52 .exclude(deleted
=True)\
53 .select_related('podcast', 'client')
55 # grou clients by subscribed podcasts
56 subscription_list
= {}
57 for subscription
in subscriptions
:
58 podcast
= subscription
.podcast
60 if not podcast
in subscription_list
:
61 subscription_list
[podcast
] = {
64 'episodes': podcast
.episode_count
,
67 subscription_list
[podcast
]['devices'].append(subscription
.client
)
69 # sort most recently updated podcast first
70 subscriptions
= subscription_list
.values()
71 now
= datetime
.utcnow()
72 sort_key
= lambda s
: s
['podcast'].latest_episode_timestamp
or now
73 subscriptions
= sorted(subscriptions
, key
=sort_key
, reverse
=True)
77 @requires_token(token_name
='subscriptions_token')
78 def subscriptions_feed(request
, username
):
79 # Create to feed manually so we can wrap the token-authentication around it
80 f
= SubscriptionsFeed(username
)
81 obj
= f
.get_object(request
, username
)
82 feedgen
= f
.get_feed(obj
, request
)
83 response
= HttpResponse(content_type
=feedgen
.mime_type
)
84 feedgen
.write(response
, 'utf-8')
88 class SubscriptionsFeed(Feed
):
89 """ A feed showing subscription changes for a certain user """
93 def __init__(self
, username
):
94 self
.username
= username
96 def get_object(self
, request
, username
):
97 self
.site
= RequestSite(request
)
98 User
= get_user_model()
99 user
= get_object_or_404(User
, username
=username
)
102 def title(self
, user
):
103 return _('%(username)s\'s Podcast Subscriptions on %(site)s') % \
104 dict(username
=user
.username
, site
=self
.site
)
106 def description(self
, user
):
107 return _('Recent changes to %(username)s\'s podcast subscriptions on %(site)s') % \
108 dict(username
=user
.username
, site
=self
.site
)
110 def link(self
, user
):
111 return reverse('shared-subscriptions', args
=[user
.username
])
113 def items(self
, user
):
114 history
= get_subscription_history(user
, public_only
=True)
115 history
= get_subscription_change_history(history
)
116 history
= list(history
)[-self
.NUM_ITEMS
:]
119 def author_name(self
, user
):
122 def author_link(self
, user
):
123 return reverse('shared-subscriptions', args
=[user
.username
])
125 # entry-specific data below
127 description_template
= "subscription-feed-description.html"
129 def item_title(self
, entry
):
130 if entry
.action
== 'subscribe':
131 s
= _('%(username)s subscribed to %(podcast)s (%(site)s)')
133 s
= _('%(username)s unsubscribed from %(podcast)s (%(site)s)')
135 return s
% dict(username
=self
.username
,
136 podcast
=entry
.podcast
.display_title
,
139 def item_link(self
, item
):
140 return get_podcast_link_target(item
.podcast
)
142 def item_pubdate(self
, item
):
143 return item
.timestamp