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_userdata_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 udb
= get_userdata_database()
16 return udb
.view('podcast_states/by_podcast',
17 startkey
= [podcast
.get_id(), None],
18 endkey
= [podcast
.get_id(), {}],
20 wrapper
= PodcastUserState
,
24 @cache_result(timeout
=60*60)
25 def subscribed_users(podcast
):
28 raise QueryParameterMissing('podcast')
30 udb
= get_userdata_database()
32 res
= udb
.view('subscriptions/by_podcast',
33 startkey
= [podcast
.get_id(), None, None],
34 endkey
= [podcast
.get_id(), {}, {}],
37 stale
= 'update_after',
40 users
= (r
['key'][1] for r
in res
)
44 def subscribed_podcast_ids_by_user_id(user_id
):
47 raise QueryParameterMissing('user_id')
49 udb
= get_userdata_database()
51 subscribed
= udb
.view('subscriptions/by_user',
52 startkey
= [user_id
, True, None, None],
53 endkey
= [user_id
, True, {}, {}],
56 stale
= 'update_after',
58 return set(r
['key'][2] for r
in subscribed
)
61 @cache_result(timeout
=60*60)
62 def podcast_subscriber_count(podcast
):
65 raise QueryParameterMissing('podcast')
67 udb
= get_userdata_database()
70 for podcast_id
in podcast
.get_ids():
71 x
= udb
.view('subscribers/by_podcast',
72 startkey
= [podcast_id
, None],
73 endkey
= [podcast_id
, {}],
79 subscriber_sum
+= x
.one()['value'] if x
else 0
84 def podcast_state_for_user_podcast(user
, podcast
):
87 raise QueryParameterMissing('user')
90 raise QueryParameterMissing('podcast')
92 udb
= get_userdata_database()
94 r
= udb
.view('podcast_states/by_podcast',
95 key
= [podcast
.get_id(), user
._id
],
98 wrapper
= PodcastUserState
,
105 p
= PodcastUserState()
106 p
.podcast
= podcast
.get_id()
108 p
.ref_url
= podcast
.url
109 p
.settings
[PUBLIC_SUB_PODCAST
.name
]=user
.get_wksetting(PUBLIC_SUB_USER
)
111 p
.set_device_state(user
.devices
)
116 def podcast_states_for_user(user
):
119 raise QueryParameterMissing('user')
121 udb
= get_userdata_database()
123 r
= udb
.view('podcast_states/by_user',
124 startkey
= [user
._id
, None],
125 endkey
= [user
._id
, 'ZZZZ'],
127 wrapper
= PodcastUserState
,
132 def podcast_states_for_device(device_id
):
135 raise QueryParameterMissing('device_id')
137 udb
= get_userdata_database()
139 r
= udb
.view('podcast_states/by_device',
140 startkey
= [device_id
, None],
141 endkey
= [device_id
, {}],
143 wrapper
= PodcastUserState
,
148 @cache_result(timeout
=60*60)
149 def podcast_state_count():
150 udb
= get_userdata_database()
151 r
= udb
.view('podcast_states/by_user',
153 stale
= 'update_after',
158 def subscribed_podcast_ids_by_device(device
):
161 raise QueryParameterMissing('device')
163 udb
= get_userdata_database()
164 r
= udb
.view('subscriptions/by_device',
165 startkey
= [device
.id, None],
166 endkey
= [device
.id, {}]
168 return [res
['key'][1] for res
in r
]
171 def subscriptions_by_user(user
, public
=None):
173 Returns a list of (podcast-id, device-id) tuples for all
174 of the users subscriptions
178 raise QueryParameterMissing('user')
180 udb
= get_userdata_database()
182 r
= udb
.view('subscriptions/by_user',
183 startkey
= [user
._id
, public
, None, None],
184 endkey
= [user
._id
+'ZZZ', None, None, None],
186 wrapper
= PodcastUserState
,
188 return [res
['key'][1:] for res
in r
]
191 @repeat_on_conflict(['state'])
192 def add_subscription_action(state
, action
):
193 state
.add_actions([action
])
197 @repeat_on_conflict(['state'])
198 def delete_podcast_state(state
):