fix cmd update-related-podcasts
[mygpo.git] / mygpo / db / couchdb / directory.py
blob833a9bd9c0d3e8432d73749c85c95f195f3c16cb
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
6 from mygpo.cache import cache_result
7 from mygpo.db.couchdb.utils import multi_request_view
8 from mygpo.db import QueryParameterMissing
11 @cache_result(timeout=60*60)
12 def category_for_tag(tag):
14 if not tag:
15 raise QueryParameterMissing('tag')
17 r = Category.view('categories/by_tags',
18 key = tag,
19 include_docs = True,
20 stale = 'update_after',
22 return r.first() if r else None
25 @cache_result(timeout=60*60)
26 def top_categories(offset, count, with_podcasts=False):
28 if offset is None:
29 raise QueryParameterMissing('offset')
31 if not count:
32 raise QueryParameterMissing('count')
35 if with_podcasts:
36 r = Category.view('categories/by_update',
37 descending = True,
38 skip = offset,
39 limit = count,
40 include_docs = True,
41 stale = 'update_after'
44 else:
45 db = get_main_database()
46 r = db.view('categories/by_update',
47 descending = True,
48 skip = offset,
49 limit = count,
50 stale = 'update_after',
51 wrapper = _category_wrapper,
54 return list(r)
57 def _category_wrapper(r):
58 c = Category()
59 c.label = r['value'][0]
60 c._weight = r['value'][1]
61 return c
64 def tags_for_podcast(podcast):
65 """ all tags for the podcast, in decreasing order of importance """
67 if not podcast:
68 raise QueryParameterMissing('podcast')
71 db = get_main_database()
72 res = db.view('tags/by_podcast',
73 startkey = [podcast.get_id(), None],
74 endkey = [podcast.get_id(), {}],
75 reduce = True,
76 group = True,
77 group_level = 2,
78 stale = 'update_after',
81 tags = Counter(dict((x['key'][1], x['value']) for x in res))
83 res = db.view('usertags/by_podcast',
84 startkey = [podcast.get_id(), None],
85 endkey = [podcast.get_id(), {}],
86 reduce = True,
87 group = True,
88 group_level = 2,
91 tags.update(Counter(dict( (x['key'][1], x['value']) for x in res)))
93 get_tag = itemgetter(0)
94 return map(get_tag, tags.most_common())
97 def tags_for_user(user, podcast_id=None):
98 """ mapping of all podcasts tagged by the user with a list of tags """
100 if not user:
101 raise QueryParameterMissing('user')
104 db = get_main_database()
105 res = db.view('tags/by_user',
106 startkey = [user._id, podcast_id],
107 endkey = [user._id, podcast_id or {}]
110 tags = defaultdict(list)
111 for r in res:
112 tags[r['key'][1]].append(r['value'])
113 return tags
116 def all_tags():
117 """ Returns all tags
119 Some tags might be returned twice """
120 db = get_main_database()
121 res = multi_request_view(db, 'podcasts/by_tag',
122 wrap = False,
123 reduce = True,
124 group = True,
125 group_level = 1
128 for r in res:
129 yield r['key'][0]
131 res = multi_request_view(db, 'usertags/podcasts',
132 wrap = False,
133 reduce = True,
134 group = True,
135 group_level = 1
138 for r in res:
139 yield r['key'][0]
142 @cache_result(timeout=60*60)
143 def toplist(res_cls, view, key, limit, **view_args):
145 if not limit:
146 raise QueryParameterMissing('limit')
149 r = res_cls.view(view,
150 startkey = key + [{}],
151 endkey = key + [None],
152 include_docs = True,
153 descending = True,
154 limit = limit,
155 stale = 'update_after',
156 **view_args
158 return list(r)