remove backported Counter class
[mygpo.git] / mygpo / db / couchdb / podcastlist.py
blob352da6c0ae474216521d7ffb00f25ef333db51b4
1 from random import random
3 from django.core.cache import cache
5 from mygpo.share.models import PodcastList
6 from mygpo.cache import cache_result
7 from mygpo.db import QueryParameterMissing
11 def podcastlist_for_user_slug(user_id, slug):
13 if not user_id:
14 raise QueryParameterMissing('user_id')
16 if not slug:
17 raise QueryParameterMissing('slug')
19 key = 'plist-%s-%s' % (user_id, slug)
21 l = cache.get(key)
22 if l:
23 return l
25 r = PodcastList.view('podcastlists/by_user_slug',
26 key = [user_id, slug],
27 include_docs = True,
30 if r:
31 l = r.one()
32 cache.set(key, l, 60)
33 return l
35 return None
39 def podcastlists_for_user(user_id):
41 if not user_id:
42 raise QueryParameterMissing('user_id')
44 r = PodcastList.view('podcastlists/by_user_slug',
45 startkey = [user_id, None],
46 endkey = [user_id, {}],
47 include_docs = True,
49 return list(r)
53 @cache_result(timeout=60*69)
54 def podcastlists_by_rating(**kwargs):
55 r = PodcastList.view('podcastlists/by_rating',
56 descending = True,
57 include_docs = True,
58 stale = 'update_after',
59 **kwargs
61 return list(r)
65 @cache_result(timeout=60*60)
66 def podcastlist_count(with_rating=True):
67 view = 'podcastlists/by_rating' if with_rating else \
68 'podcastlists/by_user_slug'
70 return PodcastList.view(view,
71 limit = 0,
72 stale = 'update_after',
73 ).total_rows
77 def random_podcastlists(chunk_size=1):
79 while True:
80 rnd = random()
81 res = PodcastList.view('podcastlists/random',
82 startkey = rnd,
83 include_docs = True,
84 limit = chunk_size,
85 stale = 'ok',
88 if not res:
89 break
91 for r in res:
92 yield r