catch missing parameters to query methods
[mygpo.git] / mygpo / db / couchdb / user.py
bloba0ace9ee9ac15d83593966f07fc3181fc0825abb
1 from mygpo.cache import cache_result
2 from mygpo.counter import Counter
3 from mygpo.couch import get_main_database
4 from mygpo.db import QueryParameterMissing
5 from mygpo.db.couchdb.episode import episodes_by_id
8 @cache_result(timeout=60)
9 def get_num_listened_episodes(user):
11 if not user:
12 raise QueryParameterMissing('user')
14 db = get_main_database()
15 r = db.view('listeners/by_user_podcast',
16 startkey = [user._id, None],
17 endkey = [user._id, {}],
18 reduce = True,
19 group_level = 2,
22 return map(_wrap_num_listened, r)
25 def _wrap_num_listened(obj):
26 count = obj['value']
27 podcast = obj['key'][1]
28 return (podcast, count)
31 @cache_result(timeout=60)
32 def get_num_played_episodes(user, since=None, until={}):
33 """ Number of played episodes in interval """
35 if not user:
36 raise QueryParameterMissing('user')
38 since_str = since.strftime('%Y-%m-%d') if since else None
39 until_str = until.strftime('%Y-%m-%d') if until else {}
41 startkey = [user._id, since_str]
42 endkey = [user._id, until_str]
44 db = get_main_database()
45 res = db.view('listeners/by_user',
46 startkey = startkey,
47 endkey = endkey,
48 reduce = True,
51 val = res.one()
52 return val['value'] if val else 0
57 @cache_result(timeout=60)
58 def get_latest_episodes(user, count=10):
59 """ Returns the latest episodes that the user has accessed """
61 if not user:
62 raise QueryParameterMissing('user')
64 startkey = [user._id, {}]
65 endkey = [user._id, None]
67 db = get_main_database()
68 res = db.view('listeners/by_user',
69 startkey = startkey,
70 endkey = endkey,
71 include_docs = True,
72 descending = True,
73 limit = count,
74 reduce = False,
77 keys = [r['value'] for r in res]
78 return episodes_by_id(keys)
82 @cache_result(timeout=60)
83 def get_seconds_played(user, since=None, until={}):
84 """ Returns the number of seconds that the user has listened
86 Can be selected by timespan, podcast and episode """
88 if not user:
89 raise QueryParameterMissing('user')
91 since_str = since.strftime('%Y-%m-%dT%H:%M:%S') if since else None
92 until_str = until.strftime('%Y-%m-%dT%H:%M:%S') if until else {}
94 startkey = [user._id, since_str]
95 endkey = [user._id, until_str]
97 db = get_main_database()
98 res = db.view('listeners/times_played_by_user',
99 startkey = startkey,
100 endkey = endkey,
101 reduce = True,
104 val = res.one()
105 return val['value'] if val else 0
109 @cache_result(timeout=60*60)
110 def suggestions_for_user(user):
112 if not user:
113 raise QueryParameterMissing('user')
115 from mygpo.users.models import Suggestions
116 r = Suggestions.view('suggestions/by_user',
117 key = user._id,
118 include_docs = True,
121 if r:
122 return r.first()
124 else:
125 s = Suggestions()
126 s.user = user._id
127 return s
130 @cache_result(timeout=60*60)
131 def user_agent_stats():
132 from mygpo.users.models import User
133 res = User.view('clients/by_ua_string',
134 wrap_doc = False,
135 group_level = 1,
136 stale = 'update_after',
139 return Counter(dict((r['key'], r['value']) for r in res))
142 def deleted_users():
143 from mygpo.users.models import User
144 users = User.view('users/deleted',
145 include_docs = True,
146 reduce = False,
148 return list(users)
151 def deleted_user_count():
152 total = User.view('users/deleted',
153 reduce = True,
155 return list(total)[0]['value'] if total else 0
159 @cache_result(timeout=60)
160 def user_history(user, start, length):
162 if not user:
163 raise QueryParameterMissing('user')
165 if length <= 0:
166 return []
168 db = get_main_database()
169 res = db.view('history/by_user',
170 descending = True,
171 startkey = [user._id, {}],
172 endkey = [user._id, None],
173 limit = length,
174 skip = start,
177 return map(_wrap_historyentry, res)
180 @cache_result(timeout=60)
181 def device_history(user, device, start, length):
183 if not user:
184 raise QueryParameterMissing('user')
186 if not device:
187 raise QueryParameterMissing('device')
189 if length <= 0:
190 return []
192 db = get_main_database()
194 res = db.view('history/by_device',
195 descending = True,
196 startkey = [user._id, device.id, {}],
197 endkey = [user._id, device.id, None],
198 limit = length,
199 skip = start,
202 return map(_wrap_historyentry, res)
205 def _wrap_historyentry(action):
206 from mygpo.users.models import HistoryEntry
207 return HistoryEntry.from_action_dict(action['value'])