1 from mygpo
.users
.models
import PodcastUserState
2 from mygpo
.couch
import get_main_database
3 from mygpo
.cache
import cache_result
6 def all_podcast_states(podcast
):
7 return PodcastUserState
.view('podcast_states/by_podcast',
8 startkey
= [podcast
.get_id(), None],
9 endkey
= [podcast
.get_id(), {}],
14 @cache_result(timeout
=60*60)
15 def subscribed_users(podcast
):
16 db
= get_main_database()
18 res
= db
.view('subscriptions/by_podcast',
19 startkey
= [podcast
.get_id(), None, None],
20 endkey
= [podcast
.get_id(), {}, {}],
23 stale
= 'update_after',
26 users
= (r
['key'][1] for r
in res
)
29 def subscribed_podcast_ids_by_user_id(user_id
):
30 subscribed
= db
.view('subscriptions/by_user',
31 startkey
= [user_id
, True, None, None],
32 endkey
= [user_id
, True, {}, {}],
35 stale
= 'update_after',
37 return set(r
['key'][2] for r
in subscribed
)
40 @cache_result(timeout
=60*60)
41 def podcast_subscriber_count(podcast
):
42 db
= get_main_database()
45 for podcast_id
in podcast
.get_ids():
46 x
= db
.view('subscriptions/by_podcast',
47 startkey
= [podcast_id
, None],
48 endkey
= [podcast_id
, {}],
53 subscriber_sum
+= x
.count()
59 def podcast_state_for_user_podcast(user
, podcast
):
60 r
= PodcastUserState
.view('podcast_states/by_podcast',
61 key
= [podcast
.get_id(), user
._id
],
70 p
= PodcastUserState()
71 p
.podcast
= podcast
.get_id()
73 p
.ref_url
= podcast
.url
74 p
.settings
['public_subscription'] = user
.settings
.get('public_subscriptions', True)
76 p
.set_device_state(user
.devices
)
81 def podcast_states_for_user(user
):
82 r
= PodcastUserState
.view('podcast_states/by_user',
83 startkey
= [user
._id
, None],
84 endkey
= [user
._id
, 'ZZZZ'],
90 def podcast_states_for_device(device_id
):
91 r
= PodcastUserState
.view('podcast_states/by_device',
92 startkey
= [device_id
, None],
93 endkey
= [device_id
, {}],
99 @cache_result(timeout
=60*60)
100 def podcast_state_count():
101 r
= PodcastUserState
.view('podcast_states/by_user',
103 stale
= 'update_after',
108 def subscribed_podcast_ids_by_device(device
):
109 r
= self
.view('subscriptions/by_device',
110 startkey
= [device
.id, None],
111 endkey
= [device
.id, {}]
113 return [res
['key'][1] for res
in r
]
116 def subscriptions_by_user(user
, public
=None):
118 Returns a list of (podcast-id, device-id) tuples for all
119 of the users subscriptions
122 r
= PodcastUserState
.view('subscriptions/by_user',
123 startkey
= [user
._id
, public
, None, None],
124 endkey
= [user
._id
+'ZZZ', None, None, None],
127 return [res
['key'][1:] for res
in r
]