Query "userdata" views in correct database
[mygpo.git] / mygpo / db / couchdb / directory.py
blob4106f9bcaacb0564f4f18d9a124827c34c125592
1 from collections import defaultdict, Counter
2 from operator import itemgetter
4 from mygpo.directory.models import Category
5 from mygpo.db.couchdb import get_main_database, get_categories_database, \
6 get_userdata_database
7 from mygpo.cache import cache_result
8 from mygpo.db.couchdb.utils import multi_request_view
9 from mygpo.db import QueryParameterMissing
12 @cache_result(timeout=60*60)
13 def category_for_tag(tag):
15 if not tag:
16 raise QueryParameterMissing('tag')
18 db = get_categories_database()
19 r = db.view('categories/by_tags',
20 key = tag,
21 include_docs = True,
22 stale = 'update_after',
23 schema = Category
25 return r.first() if r else None
28 @cache_result(timeout=60*60)
29 def top_categories(offset, count, with_podcasts=False):
31 if offset is None:
32 raise QueryParameterMissing('offset')
34 if not count:
35 raise QueryParameterMissing('count')
37 db = get_categories_database()
39 if with_podcasts:
40 r = db.view('categories/by_update',
41 descending = True,
42 skip = offset,
43 limit = count,
44 include_docs = True,
45 stale = 'update_after',
46 schema = Category,
49 else:
50 r = db.view('categories/by_update',
51 descending = True,
52 skip = offset,
53 limit = count,
54 stale = 'update_after',
55 wrapper = _category_wrapper,
58 return list(r)
61 def _category_wrapper(r):
62 c = Category()
63 c.label = r['value'][0]
64 c._weight = r['value'][1]
65 return c
68 def save_category(category):
69 db = get_categories_database()
70 db.save_doc(category)
73 def tags_for_podcast(podcast):
74 """ all tags for the podcast, in decreasing order of importance """
76 if not podcast:
77 raise QueryParameterMissing('podcast')
80 db = get_main_database()
81 res = db.view('tags/by_podcast',
82 startkey = [podcast.get_id(), None],
83 endkey = [podcast.get_id(), {}],
84 reduce = True,
85 group = True,
86 group_level = 2,
87 stale = 'update_after',
90 tags = Counter(dict((x['key'][1], x['value']) for x in res))
92 udb = get_userdata_database()
93 res = udb.view('usertags/by_podcast',
94 startkey = [podcast.get_id(), None],
95 endkey = [podcast.get_id(), {}],
96 reduce = True,
97 group = True,
98 group_level = 2,
101 tags.update(Counter(dict( (x['key'][1], x['value']) for x in res)))
103 get_tag = itemgetter(0)
104 return map(get_tag, tags.most_common())
107 def tags_for_user(user, podcast_id=None):
108 """ mapping of all podcasts tagged by the user with a list of tags """
110 if not user:
111 raise QueryParameterMissing('user')
114 db = get_main_database()
115 res = db.view('tags/by_user',
116 startkey = [user._id, podcast_id],
117 endkey = [user._id, podcast_id or {}]
120 tags = defaultdict(list)
121 for r in res:
122 tags[r['key'][1]].append(r['value'])
123 return tags
126 def all_tags():
127 """ Returns all tags
129 Some tags might be returned twice """
130 db = get_main_database()
131 res = multi_request_view(db, 'podcasts/by_tag',
132 wrap = False,
133 reduce = True,
134 group = True,
135 group_level = 1
138 for r in res:
139 yield r['key'][0]
141 udb = get_userdata_database()
142 res = multi_request_view(udb, 'usertags/podcasts',
143 wrap = False,
144 reduce = True,
145 group = True,
146 group_level = 1
149 for r in res:
150 yield r['key'][0]
153 @cache_result(timeout=60*60)
154 def toplist(res_cls, view, key, limit, **view_args):
156 if not limit:
157 raise QueryParameterMissing('limit')
160 r = res_cls.view(view,
161 startkey = key + [{}],
162 endkey = key + [None],
163 include_docs = True,
164 descending = True,
165 limit = limit,
166 stale = 'update_after',
167 **view_args
169 return list(r)