fix cmd update-related-podcasts
[mygpo.git] / mygpo / db / couchdb / podcast_state.py
blobed3ba7e62a562a34a5e7db703d23406ca72f9cbb
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)
38 return users
41 def subscribed_podcast_ids_by_user_id(user_id):
43 if not 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, {}, {}],
51 group = True,
52 group_level = 3,
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):
61 if not podcast:
62 raise QueryParameterMissing('podcast')
64 db = get_main_database()
65 subscriber_sum = 0
67 for podcast_id in podcast.get_ids():
68 x = db.view('subscribers/by_podcast',
69 startkey = [podcast_id, None],
70 endkey = [podcast_id, {}],
71 reduce = True,
72 group = True,
73 group_level = 1,
76 subscriber_sum += x.one()['value'] if x else 0
78 return subscriber_sum
81 def podcast_state_for_user_podcast(user, podcast):
83 if not user:
84 raise QueryParameterMissing('user')
86 if not podcast:
87 raise QueryParameterMissing('podcast')
90 r = PodcastUserState.view('podcast_states/by_podcast',
91 key = [podcast.get_id(), user._id],
92 limit = 1,
93 include_docs = True,
96 if r:
97 return r.first()
99 else:
100 p = PodcastUserState()
101 p.podcast = podcast.get_id()
102 p.user = user._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)
108 return p
111 def podcast_states_for_user(user):
113 if not user:
114 raise QueryParameterMissing('user')
116 r = PodcastUserState.view('podcast_states/by_user',
117 startkey = [user._id, None],
118 endkey = [user._id, 'ZZZZ'],
119 include_docs = True,
121 return list(r)
124 def podcast_states_for_device(device_id):
126 if not device_id:
127 raise QueryParameterMissing('device_id')
129 r = PodcastUserState.view('podcast_states/by_device',
130 startkey = [device_id, None],
131 endkey = [device_id, {}],
132 include_docs = True,
134 return list(r)
137 @cache_result(timeout=60*60)
138 def podcast_state_count():
139 r = PodcastUserState.view('podcast_states/by_user',
140 limit = 0,
141 stale = 'update_after',
143 return r.total_rows
146 def subscribed_podcast_ids_by_device(device):
148 if not 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
165 if not user:
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],
171 reduce = False,
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])
179 state.save()