move all PodcastUserState db queries into separate module
[mygpo.git] / mygpo / db / couchdb / podcast_state.py
blob2c53f07f5dac1f72740cdbf529bee8ddcef8472b
1 from mygpo.users.models import PodcastUserState
2 from mygpo.couch import get_main_database
3 from mygpo.cache import cache_result
6 def all_podcast_states(podcast):
7 return PodcastUserState.view('podcast_states/by_podcast',
8 startkey = [podcast.get_id(), None],
9 endkey = [podcast.get_id(), {}],
10 include_docs = True,
14 @cache_result(timeout=60*60)
15 def subscribed_users(podcast):
16 db = get_main_database()
18 res = db.view('subscriptions/by_podcast',
19 startkey = [podcast.get_id(), None, None],
20 endkey = [podcast.get_id(), {}, {}],
21 group = True,
22 group_level = 2,
23 stale = 'update_after',
26 users = (r['key'][1] for r in res)
29 def subscribed_podcast_ids_by_user_id(user_id):
30 subscribed = db.view('subscriptions/by_user',
31 startkey = [user_id, True, None, None],
32 endkey = [user_id, True, {}, {}],
33 group = True,
34 group_level = 3,
35 stale = 'update_after',
37 return set(r['key'][2] for r in subscribed)
40 @cache_result(timeout=60*60)
41 def podcast_subscriber_count(podcast):
42 db = get_main_database()
43 subscriber_sum = 0
45 for podcast_id in podcast.get_ids():
46 x = db.view('subscriptions/by_podcast',
47 startkey = [podcast_id, None],
48 endkey = [podcast_id, {}],
49 reduce = True,
50 group = True,
51 group_level = 2,
53 subscriber_sum += x.count()
55 return subscriber_sum
59 def podcast_state_for_user_podcast(user, podcast):
60 r = PodcastUserState.view('podcast_states/by_podcast',
61 key = [podcast.get_id(), user._id],
62 limit = 1,
63 include_docs = True,
66 if r:
67 return r.first()
69 else:
70 p = PodcastUserState()
71 p.podcast = podcast.get_id()
72 p.user = user._id
73 p.ref_url = podcast.url
74 p.settings['public_subscription'] = user.settings.get('public_subscriptions', True)
76 p.set_device_state(user.devices)
78 return p
81 def podcast_states_for_user(user):
82 r = PodcastUserState.view('podcast_states/by_user',
83 startkey = [user._id, None],
84 endkey = [user._id, 'ZZZZ'],
85 include_docs = True,
87 return list(r)
90 def podcast_states_for_device(device_id):
91 r = PodcastUserState.view('podcast_states/by_device',
92 startkey = [device_id, None],
93 endkey = [device_id, {}],
94 include_docs = True,
96 return list(r)
99 @cache_result(timeout=60*60)
100 def podcast_state_count():
101 r = PodcastUserState.view('podcast_states/by_user',
102 limit = 0,
103 stale = 'update_after',
105 return r.total_rows
108 def subscribed_podcast_ids_by_device(device):
109 r = self.view('subscriptions/by_device',
110 startkey = [device.id, None],
111 endkey = [device.id, {}]
113 return [res['key'][1] for res in r]
116 def subscriptions_by_user(user, public=None):
118 Returns a list of (podcast-id, device-id) tuples for all
119 of the users subscriptions
122 r = PodcastUserState.view('subscriptions/by_user',
123 startkey = [user._id, public, None, None],
124 endkey = [user._id+'ZZZ', None, None, None],
125 reduce = False,
127 return [res['key'][1:] for res in r]