1 from mygpo
.users
.models
import PodcastUserState
2 from mygpo
.users
.settings
import PUBLIC_SUB_PODCAST
, PUBLIC_SUB_USER
3 from mygpo
.db
.couchdb
import get_main_database
4 from mygpo
.cache
import cache_result
5 from mygpo
.db
import QueryParameterMissing
6 from mygpo
.decorators
import repeat_on_conflict
9 def all_podcast_states(podcast
):
12 raise QueryParameterMissing('podcast')
14 return PodcastUserState
.view('podcast_states/by_podcast',
15 startkey
= [podcast
.get_id(), None],
16 endkey
= [podcast
.get_id(), {}],
21 @cache_result(timeout
=60*60)
22 def subscribed_users(podcast
):
25 raise QueryParameterMissing('podcast')
27 db
= get_main_database()
29 res
= db
.view('subscriptions/by_podcast',
30 startkey
= [podcast
.get_id(), None, None],
31 endkey
= [podcast
.get_id(), {}, {}],
34 stale
= 'update_after',
37 users
= (r
['key'][1] for r
in res
)
40 def subscribed_podcast_ids_by_user_id(user_id
):
43 raise QueryParameterMissing('user_id')
45 subscribed
= db
.view('subscriptions/by_user',
46 startkey
= [user_id
, True, None, None],
47 endkey
= [user_id
, True, {}, {}],
50 stale
= 'update_after',
52 return set(r
['key'][2] for r
in subscribed
)
55 @cache_result(timeout
=60*60)
56 def podcast_subscriber_count(podcast
):
59 raise QueryParameterMissing('podcast')
61 db
= get_main_database()
64 for podcast_id
in podcast
.get_ids():
65 x
= db
.view('subscribers/by_podcast',
66 startkey
= [podcast_id
, None],
67 endkey
= [podcast_id
, {}],
73 subscriber_sum
+= x
.one()['value'] if x
else 0
78 def podcast_state_for_user_podcast(user
, podcast
):
81 raise QueryParameterMissing('user')
84 raise QueryParameterMissing('podcast')
87 r
= PodcastUserState
.view('podcast_states/by_podcast',
88 key
= [podcast
.get_id(), user
._id
],
97 p
= PodcastUserState()
98 p
.podcast
= podcast
.get_id()
100 p
.ref_url
= podcast
.url
101 p
.settings
[PUBLIC_SUB_PODCAST
.name
]=user
.get_wksetting(PUBLIC_SUB_USER
)
103 p
.set_device_state(user
.devices
)
108 def podcast_states_for_user(user
):
111 raise QueryParameterMissing('user')
113 r
= PodcastUserState
.view('podcast_states/by_user',
114 startkey
= [user
._id
, None],
115 endkey
= [user
._id
, 'ZZZZ'],
121 def podcast_states_for_device(device_id
):
124 raise QueryParameterMissing('device_id')
126 r
= PodcastUserState
.view('podcast_states/by_device',
127 startkey
= [device_id
, None],
128 endkey
= [device_id
, {}],
134 @cache_result(timeout
=60*60)
135 def podcast_state_count():
136 r
= PodcastUserState
.view('podcast_states/by_user',
138 stale
= 'update_after',
143 def subscribed_podcast_ids_by_device(device
):
146 raise QueryParameterMissing('device')
148 db
= get_main_database()
149 r
= db
.view('subscriptions/by_device',
150 startkey
= [device
.id, None],
151 endkey
= [device
.id, {}]
153 return [res
['key'][1] for res
in r
]
156 def subscriptions_by_user(user
, public
=None):
158 Returns a list of (podcast-id, device-id) tuples for all
159 of the users subscriptions
163 raise QueryParameterMissing('user')
165 r
= PodcastUserState
.view('subscriptions/by_user',
166 startkey
= [user
._id
, public
, None, None],
167 endkey
= [user
._id
+'ZZZ', None, None, None],
170 return [res
['key'][1:] for res
in r
]
173 @repeat_on_conflict(['state'])
174 def add_subscription_action(state
, action
):
175 state
.add_actions([action
])