Query "userdata" views in correct database
[mygpo.git] / mygpo / db / couchdb / podcast_state.py
blob3c70cab4099d33ad9f48503dfd8c8a5bb4f77a20
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):
11 if not 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(), {}],
19 include_docs = True,
20 wrapper = PodcastUserState,
24 @cache_result(timeout=60*60)
25 def subscribed_users(podcast):
27 if not 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(), {}, {}],
35 group = True,
36 group_level = 2,
37 stale = 'update_after',
40 users = (r['key'][1] for r in res)
41 return users
44 def subscribed_podcast_ids_by_user_id(user_id):
46 if not 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, {}, {}],
54 group = True,
55 group_level = 3,
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):
64 if not podcast:
65 raise QueryParameterMissing('podcast')
67 udb = get_userdata_database()
68 subscriber_sum = 0
70 for podcast_id in podcast.get_ids():
71 x = udb.view('subscribers/by_podcast',
72 startkey = [podcast_id, None],
73 endkey = [podcast_id, {}],
74 reduce = True,
75 group = True,
76 group_level = 1,
79 subscriber_sum += x.one()['value'] if x else 0
81 return subscriber_sum
84 def podcast_state_for_user_podcast(user, podcast):
86 if not user:
87 raise QueryParameterMissing('user')
89 if not podcast:
90 raise QueryParameterMissing('podcast')
92 udb = get_userdata_database()
94 r = udb.view('podcast_states/by_podcast',
95 key = [podcast.get_id(), user._id],
96 limit = 1,
97 include_docs = True,
98 wrapper = PodcastUserState,
101 if r:
102 return r.first()
104 else:
105 p = PodcastUserState()
106 p.podcast = podcast.get_id()
107 p.user = user._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)
113 return p
116 def podcast_states_for_user(user):
118 if not 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'],
126 include_docs = True,
127 wrapper = PodcastUserState,
129 return list(r)
132 def podcast_states_for_device(device_id):
134 if not 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, {}],
142 include_docs = True,
143 wrapper = PodcastUserState,
145 return list(r)
148 @cache_result(timeout=60*60)
149 def podcast_state_count():
150 udb = get_userdata_database()
151 r = udb.view('podcast_states/by_user',
152 limit = 0,
153 stale = 'update_after',
155 return r.total_rows
158 def subscribed_podcast_ids_by_device(device):
160 if not 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
177 if not user:
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],
185 reduce = False,
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])
194 state.save()
197 @repeat_on_conflict(['state'])
198 def delete_podcast_state(state):
199 state.delete()