catch missing parameters to query methods
[mygpo.git] / mygpo / db / couchdb / directory.py
blob9236b75f343750ccb1f8c4d2feacc399f6f59182
1 from collections import defaultdict
2 from operator import itemgetter
4 from mygpo.directory.models import Category
5 from mygpo.couch import get_main_database
6 from mygpo.cache import cache_result
7 from mygpo.db.couchdb.utils import multi_request_view
8 from mygpo.db import QueryParameterMissing
9 from mygpo.counter import Counter
12 @cache_result(timeout=60*60)
13 def category_for_tag(tag):
15 if not tag:
16 raise QueryParameterMissing('tag')
18 r = Category.view('categories/by_tags',
19 key = tag,
20 include_docs = True,
21 stale = 'update_after',
23 return r.first() if r else None
26 @cache_result(timeout=60*60)
27 def top_categories(offset, count, with_podcasts=False):
29 if offset is None:
30 raise QueryParameterMissing('offset')
32 if not count:
33 raise QueryParameterMissing('count')
36 if with_podcasts:
37 r = Category.view('categories/by_update',
38 descending = True,
39 skip = offset,
40 limit = count,
41 include_docs = True,
42 stale = 'update_after'
45 else:
46 db = get_main_database()
47 r = db.view('categories/by_update',
48 descending = True,
49 skip = offset,
50 limit = count,
51 stale = 'update_after',
52 wrapper = _category_wrapper,
55 return list(r)
58 def _category_wrapper(r):
59 c = Category()
60 c.label = r['value'][0]
61 c._weight = r['value'][1]
62 return c
65 def tags_for_podcast(podcast):
66 """ all tags for the podcast, in decreasing order of importance """
68 if not podcast:
69 raise QueryParameterMissing('podcast')
72 db = get_main_database()
73 res = db.view('tags/by_podcast',
74 startkey = [podcast.get_id(), None],
75 endkey = [podcast.get_id(), {}],
76 reduce = True,
77 group = True,
78 group_level = 2,
79 stale = 'update_after',
82 tags = Counter(dict((x['key'][1], x['value']) for x in res))
84 res = db.view('usertags/by_podcast',
85 startkey = [podcast.get_id(), None],
86 endkey = [podcast.get_id(), {}],
87 reduce = True,
88 group = True,
89 group_level = 2,
92 tags.update(Counter(dict( (x['key'][1], x['value']) for x in res)))
94 get_tag = itemgetter(0)
95 return map(get_tag, tags.most_common())
98 def tags_for_user(user, podcast_id=None):
99 """ mapping of all podcasts tagged by the user with a list of tags """
101 if not user:
102 raise QueryParameterMissing('user')
105 db = get_main_database()
106 res = db.view('tags/by_user',
107 startkey = [user._id, podcast_id],
108 endkey = [user._id, podcast_id or {}]
111 tags = defaultdict(list)
112 for r in res:
113 tags[r['key'][1]].append(r['value'])
114 return tags
117 def all_tags():
118 """ Returns all tags
120 Some tags might be returned twice """
121 db = get_main_database()
122 res = multi_request_view(db, 'podcasts/by_tag',
123 wrap = False,
124 reduce = True,
125 group = True,
126 group_level = 1
129 for r in res:
130 yield r['key'][0]
132 res = multi_request_view(db, 'usertags/podcasts',
133 wrap = False,
134 reduce = True,
135 group = True,
136 group_level = 1
139 for r in res:
140 yield r['key'][0]
143 @cache_result(timeout=60*60)
144 def toplist(res_cls, view, key, limit, **view_args):
146 if not limit:
147 raise QueryParameterMissing('limit')
150 r = res_cls.view(view,
151 startkey = key + [{}],
152 endkey = key + [None],
153 include_docs = True,
154 descending = True,
155 limit = limit,
156 stale = 'update_after',
157 **view_args
159 return list(r)