0ffdea26046c43d96ceb52cbbab32a12c555f8e8
[mygpo.git] / mygpo / db / couchdb / podcast_state.py
blob0ffdea26046c43d96ceb52cbbab32a12c555f8e8
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):
11 if not podcast:
12 raise QueryParameterMissing('podcast')
14 return PodcastUserState.view('podcast_states/by_podcast',
15 startkey = [podcast.get_id(), None],
16 endkey = [podcast.get_id(), {}],
17 include_docs = True,
21 @cache_result(timeout=60*60)
22 def subscribed_users(podcast):
24 if not 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(), {}, {}],
32 group = True,
33 group_level = 2,
34 stale = 'update_after',
37 users = (r['key'][1] for r in res)
40 def subscribed_podcast_ids_by_user_id(user_id):
42 if not 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, {}, {}],
48 group = True,
49 group_level = 3,
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):
58 if not podcast:
59 raise QueryParameterMissing('podcast')
61 db = get_main_database()
62 subscriber_sum = 0
64 for podcast_id in podcast.get_ids():
65 x = db.view('subscribers/by_podcast',
66 startkey = [podcast_id, None],
67 endkey = [podcast_id, {}],
68 reduce = True,
69 group = True,
70 group_level = 1,
73 subscriber_sum += x.one()['value'] if x else 0
75 return subscriber_sum
78 def podcast_state_for_user_podcast(user, podcast):
80 if not user:
81 raise QueryParameterMissing('user')
83 if not podcast:
84 raise QueryParameterMissing('podcast')
87 r = PodcastUserState.view('podcast_states/by_podcast',
88 key = [podcast.get_id(), user._id],
89 limit = 1,
90 include_docs = True,
93 if r:
94 return r.first()
96 else:
97 p = PodcastUserState()
98 p.podcast = podcast.get_id()
99 p.user = user._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)
105 return p
108 def podcast_states_for_user(user):
110 if not user:
111 raise QueryParameterMissing('user')
113 r = PodcastUserState.view('podcast_states/by_user',
114 startkey = [user._id, None],
115 endkey = [user._id, 'ZZZZ'],
116 include_docs = True,
118 return list(r)
121 def podcast_states_for_device(device_id):
123 if not device_id:
124 raise QueryParameterMissing('device_id')
126 r = PodcastUserState.view('podcast_states/by_device',
127 startkey = [device_id, None],
128 endkey = [device_id, {}],
129 include_docs = True,
131 return list(r)
134 @cache_result(timeout=60*60)
135 def podcast_state_count():
136 r = PodcastUserState.view('podcast_states/by_user',
137 limit = 0,
138 stale = 'update_after',
140 return r.total_rows
143 def subscribed_podcast_ids_by_device(device):
145 if not 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
162 if not user:
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],
168 reduce = False,
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])
176 state.save()