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
)
41 def subscribed_podcast_ids_by_user_id(user_id
):
44 raise QueryParameterMissing('user_id')
46 db
= get_main_database()
48 subscribed
= db
.view('subscriptions/by_user',
49 startkey
= [user_id
, True, None, None],
50 endkey
= [user_id
, True, {}, {}],
53 stale
= 'update_after',
55 return set(r
['key'][2] for r
in subscribed
)
58 @cache_result(timeout
=60*60)
59 def podcast_subscriber_count(podcast
):
62 raise QueryParameterMissing('podcast')
64 db
= get_main_database()
67 for podcast_id
in podcast
.get_ids():
68 x
= db
.view('subscribers/by_podcast',
69 startkey
= [podcast_id
, None],
70 endkey
= [podcast_id
, {}],
76 subscriber_sum
+= x
.one()['value'] if x
else 0
81 def podcast_state_for_user_podcast(user
, podcast
):
84 raise QueryParameterMissing('user')
87 raise QueryParameterMissing('podcast')
90 r
= PodcastUserState
.view('podcast_states/by_podcast',
91 key
= [podcast
.get_id(), user
._id
],
100 p
= PodcastUserState()
101 p
.podcast
= podcast
.get_id()
103 p
.ref_url
= podcast
.url
104 p
.settings
[PUBLIC_SUB_PODCAST
.name
]=user
.get_wksetting(PUBLIC_SUB_USER
)
106 p
.set_device_state(user
.devices
)
111 def podcast_states_for_user(user
):
114 raise QueryParameterMissing('user')
116 r
= PodcastUserState
.view('podcast_states/by_user',
117 startkey
= [user
._id
, None],
118 endkey
= [user
._id
, 'ZZZZ'],
124 def podcast_states_for_device(device_id
):
127 raise QueryParameterMissing('device_id')
129 r
= PodcastUserState
.view('podcast_states/by_device',
130 startkey
= [device_id
, None],
131 endkey
= [device_id
, {}],
137 @cache_result(timeout
=60*60)
138 def podcast_state_count():
139 r
= PodcastUserState
.view('podcast_states/by_user',
141 stale
= 'update_after',
146 def subscribed_podcast_ids_by_device(device
):
149 raise QueryParameterMissing('device')
151 db
= get_main_database()
152 r
= db
.view('subscriptions/by_device',
153 startkey
= [device
.id, None],
154 endkey
= [device
.id, {}]
156 return [res
['key'][1] for res
in r
]
159 def subscriptions_by_user(user
, public
=None):
161 Returns a list of (podcast-id, device-id) tuples for all
162 of the users subscriptions
166 raise QueryParameterMissing('user')
168 r
= PodcastUserState
.view('subscriptions/by_user',
169 startkey
= [user
._id
, public
, None, None],
170 endkey
= [user
._id
+'ZZZ', None, None, None],
173 return [res
['key'][1:] for res
in r
]
176 @repeat_on_conflict(['state'])
177 def add_subscription_action(state
, action
):
178 state
.add_actions([action
])