From a70d9d48d432dad5e3dd09017b20264a53acfc79 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Stefan=20K=C3=B6gl?= Date: Sun, 25 Nov 2012 22:10:52 +0100 Subject: [PATCH] catch missing parameters to query methods --- mygpo/db/__init__.py | 8 +++++ mygpo/db/couchdb/common.py | 30 ++++++++++++++++ mygpo/db/couchdb/directory.py | 26 ++++++++++++++ mygpo/db/couchdb/episode.py | 75 +++++++++++++++++++++++++++++++++++++++ mygpo/db/couchdb/episode_state.py | 62 ++++++++++++++++++++++++++++++++ mygpo/db/couchdb/podcast.py | 52 +++++++++++++++++++++++++++ mygpo/db/couchdb/podcast_state.py | 40 +++++++++++++++++++++ mygpo/db/couchdb/podcastlist.py | 12 +++++++ mygpo/db/couchdb/user.py | 53 ++++++++++++++++++++++----- 9 files changed, 349 insertions(+), 9 deletions(-) diff --git a/mygpo/db/__init__.py b/mygpo/db/__init__.py index e69de29b..66c441c7 100644 --- a/mygpo/db/__init__.py +++ b/mygpo/db/__init__.py @@ -0,0 +1,8 @@ + + +class DatabaseBackendException(Exception): + """ Generic database backend exception """ + + +class QueryParameterMissing(DatabaseBackendException): + """ A mandatory parameter to a query is missing """ diff --git a/mygpo/db/couchdb/common.py b/mygpo/db/couchdb/common.py index e594dbb5..13aebc98 100644 --- a/mygpo/db/couchdb/common.py +++ b/mygpo/db/couchdb/common.py @@ -1,6 +1,7 @@ from mygpo.core.models import SanitizingRule from mygpo.cache import cache_result from mygpo.couch import get_main_database +from mygpo.db import QueryParameterMissing from mygpo.db.couchdb.utils import multi_request_view @@ -10,6 +11,10 @@ class SanitizingRuleStub(object): @cache_result(timeout=60*60) def sanitizingrules_by_obj_type(obj_type): + + if not obj_type: + raise QueryParameterMissing('obj_type') + r = SanitizingRule.view('sanitizing_rules/by_target', include_docs = True, startkey = [obj_type, None], @@ -31,6 +36,10 @@ def _wrap_rule(rule): @cache_result(timeout=60*60) def sanitizingrule_for_slug(slug): + + if not slug: + raise QueryParameterMissing('slug') + r = SanitizingRule.view('sanitizing_rules/by_slug', include_docs=True, key=slug, @@ -40,6 +49,17 @@ def sanitizingrule_for_slug(slug): def missing_slug_count(doc_type, start, end): + + if not doc_type: + raise QueryParameterMissing('doc_type') + + if not start: + raise QueryParameterMissing('start') + + if not end: + raise QueryParameterMissing('end') + + db = get_main_database() res = db.view('slugs/missing', startkey = [doc_type] + end, @@ -54,6 +74,16 @@ def missing_slug_count(doc_type, start, end): def missing_slugs(doc_type, start, end, wrapper, **kwargs): + if not doc_type: + raise QueryParameterMissing('doc_type') + + if not start: + raise QueryParameterMissing('start') + + if not end: + raise QueryParameterMissing('end') + + db = get_main_database() return multi_request_view(db, 'slugs/missing', startkey = [doc_type] + end, diff --git a/mygpo/db/couchdb/directory.py b/mygpo/db/couchdb/directory.py index 9994001f..9236b75f 100644 --- a/mygpo/db/couchdb/directory.py +++ b/mygpo/db/couchdb/directory.py @@ -5,11 +5,16 @@ from mygpo.directory.models import Category from mygpo.couch import get_main_database from mygpo.cache import cache_result from mygpo.db.couchdb.utils import multi_request_view +from mygpo.db import QueryParameterMissing from mygpo.counter import Counter @cache_result(timeout=60*60) def category_for_tag(tag): + + if not tag: + raise QueryParameterMissing('tag') + r = Category.view('categories/by_tags', key = tag, include_docs = True, @@ -20,6 +25,14 @@ def category_for_tag(tag): @cache_result(timeout=60*60) def top_categories(offset, count, with_podcasts=False): + + if offset is None: + raise QueryParameterMissing('offset') + + if not count: + raise QueryParameterMissing('count') + + if with_podcasts: r = Category.view('categories/by_update', descending = True, @@ -52,6 +65,10 @@ def _category_wrapper(r): def tags_for_podcast(podcast): """ all tags for the podcast, in decreasing order of importance """ + if not podcast: + raise QueryParameterMissing('podcast') + + db = get_main_database() res = db.view('tags/by_podcast', startkey = [podcast.get_id(), None], @@ -81,6 +98,10 @@ def tags_for_podcast(podcast): def tags_for_user(user, podcast_id=None): """ mapping of all podcasts tagged by the user with a list of tags """ + if not user: + raise QueryParameterMissing('user') + + db = get_main_database() res = db.view('tags/by_user', startkey = [user._id, podcast_id], @@ -121,6 +142,11 @@ def all_tags(): @cache_result(timeout=60*60) def toplist(res_cls, view, key, limit, **view_args): + + if not limit: + raise QueryParameterMissing('limit') + + r = res_cls.view(view, startkey = key + [{}], endkey = key + [None], diff --git a/mygpo/db/couchdb/episode.py b/mygpo/db/couchdb/episode.py index c4430191..8cf1a7e6 100644 --- a/mygpo/db/couchdb/episode.py +++ b/mygpo/db/couchdb/episode.py @@ -5,6 +5,7 @@ from django.core.cache import cache from mygpo.core.models import Podcast, Episode, MergedIdException from mygpo.cache import cache_result +from mygpo.db import QueryParameterMissing from mygpo.db.couchdb.utils import is_couchdb_id from mygpo.couch import get_main_database from mygpo.db.couchdb.podcast import podcast_for_url, podcast_for_slug_id @@ -12,6 +13,10 @@ from mygpo.db.couchdb.podcast import podcast_for_url, podcast_for_slug_id @cache_result(timeout=60*60) def episode_by_id(episode_id, current_id=False): + + if not episode_id: + raise QueryParameterMissing('episode_id') + r = Episode.view('episodes/by_id', key = episode_id, include_docs = True, @@ -29,6 +34,13 @@ def episode_by_id(episode_id, current_id=False): @cache_result(timeout=60*60) def episodes_by_id(episode_ids): + + if episode_ids is None: + raise QueryParameterMissing('episode_ids') + + if not episode_ids: + return [] + r = Episode.view('episodes/by_id', include_docs = True, keys = episode_ids, @@ -38,6 +50,10 @@ def episodes_by_id(episode_ids): @cache_result(timeout=60*60) def episode_for_oldid(oldid): + + if not oldid: + raise QueryParameterMissing('oldid') + oldid = int(oldid) r = Episode.view('episodes/by_oldid', key = oldid, @@ -49,6 +65,14 @@ def episode_for_oldid(oldid): @cache_result(timeout=60*60) def episode_for_slug(podcast_id, episode_slug): + + if not podcast_id: + raise QueryParameterMissing('podcast_id') + + if not episode_slug: + raise QueryParameterMissing('episode_slug') + + r = Episode.view('episodes/by_slug', key = [podcast_id, episode_slug], include_docs = True, @@ -57,6 +81,14 @@ def episode_for_slug(podcast_id, episode_slug): def episode_for_podcast_url(podcast_url, episode_url, create=False): + + if not podcast_url: + raise QueryParameterMissing('podcast_url') + + if not episode_url: + raise QueryParameterMissing('episode_url') + + podcast = podcast_for_url(podcast_url, create=create) if not podcast: # podcast does not exist and should not be created @@ -67,6 +99,13 @@ def episode_for_podcast_url(podcast_url, episode_url, create=False): def episode_for_podcast_id_url(podcast_id, episode_url, create=False): + if not podcast_id: + raise QueryParameterMissing('podcast_id') + + if not episode_url: + raise QueryParameterMissing('episode_url') + + key = u'episode-podcastid-%s-url-%s' % ( sha1(podcast_id.encode('utf-8')).hexdigest(), sha1(episode_url.encode('utf-8')).hexdigest()) @@ -101,6 +140,13 @@ def episode_for_podcast_id_url(podcast_id, episode_url, create=False): def episode_for_slug_id(p_slug_id, e_slug_id): """ Returns the Episode for Podcast Slug/Id and Episode Slug/Id """ + if not p_slug_id: + raise QueryParameterMissing('p_slug_id') + + if not e_slug_id: + raise QueryParameterMissing('e_slug_id') + + # The Episode-Id is unique, so take that if is_couchdb_id(e_slug_id): return episode_by_id(e_slug_id) @@ -130,6 +176,13 @@ def episode_count(): def episodes_to_dict(ids, use_cache=False): + if ids is None: + raise QueryParameterMissing('ids') + + if not ids: + return {} + + ids = list(set(ids)) objs = dict() @@ -160,6 +213,11 @@ def episodes_to_dict(ids, use_cache=False): def episode_slugs_per_podcast(podcast_id, base_slug): + + if not podcast_id: + raise QueryParameterMissing('podcast_id') + + res = Episode.view('episodes/by_slug', startkey = [podcast_id, base_slug], endkey = [podcast_id, base_slug + 'ZZZZZ'], @@ -170,6 +228,10 @@ def episode_slugs_per_podcast(podcast_id, base_slug): def episodes_for_podcast_uncached(podcast, since=None, until={}, **kwargs): + if not podcast: + raise QueryParameterMissing('podcast') + + if kwargs.get('descending', False): since, until = until, since @@ -195,6 +257,11 @@ episodes_for_podcast = cache_result(timeout=60*60)(episodes_for_podcast_uncached @cache_result(timeout=60*60) def episode_count_for_podcast(podcast, since=None, until={}, **kwargs): + + if not podcast: + raise QueryParameterMissing('podcast') + + if kwargs.get('descending', False): since, until = until, since @@ -216,6 +283,10 @@ def episode_count_for_podcast(podcast, since=None, until={}, **kwargs): def favorite_episodes_for_user(user): + + if not user: + raise QueryParameterMissing('user') + favorites = Episode.view('favorites/episodes_by_user', key = user._id, include_docs = True, @@ -224,6 +295,10 @@ def favorite_episodes_for_user(user): def chapters_for_episode(episode_id): + + if not episode_id: + raise QueryParameterMissing('episode_id') + db = get_main_database() r = db.view('chapters/by_episode', startkey = [episode_id, None], diff --git a/mygpo/db/couchdb/episode_state.py b/mygpo/db/couchdb/episode_state.py index 7a94c955..3ca986e9 100644 --- a/mygpo/db/couchdb/episode_state.py +++ b/mygpo/db/couchdb/episode_state.py @@ -5,6 +5,7 @@ from dateutil import parser from django.core.cache import cache from mygpo.users.models import EpisodeUserState +from mygpo.db import QueryParameterMissing from mygpo.db.couchdb.podcast import podcast_by_id, podcast_for_url from mygpo.db.couchdb.episode import episode_for_podcast_id_url from mygpo.couch import get_main_database @@ -14,6 +15,13 @@ from mygpo.cache import cache_result def episode_state_for_user_episode(user, episode): + if not user: + raise QueryParameterMissing('user') + + if not episode: + raise QueryParameterMissing('episode') + + key = 'episode-state-userid-%s-episodeid-%s' % (sha1(user._id).hexdigest(), sha1(episode._id).hexdigest()) @@ -48,6 +56,10 @@ def episode_state_for_user_episode(user, episode): def all_episode_states(episode): + + if not episode: + raise QueryParameterMissing('episode') + r = EpisodeUserState.view('episode_states/by_podcast_episode', startkey = [episode.podcast, episode._id, None], endkey = [episode.podcast, episode._id, {}], @@ -58,6 +70,10 @@ def all_episode_states(episode): def all_podcast_episode_states(podcast): + + if not podcast: + raise QueryParameterMissing('podcast') + r = EpisodeUserState.view('episode_states/by_podcast_episode', startkey = [podcast.get_id(), None, None], endkey = [podcast.get_id(), {}, {}], @@ -71,6 +87,9 @@ def all_podcast_episode_states(podcast): def podcast_listener_count(episode): """ returns the number of users that have listened to this podcast """ + if not episode: + raise QueryParameterMissing('episode') + r = EpisodeUserState.view('listeners/by_podcast', startkey = [episode.get_id(), None], endkey = [episode.get_id(), {}], @@ -85,6 +104,9 @@ def podcast_listener_count(episode): def podcast_listener_count_timespan(podcast, start=None, end={}): """ returns (date, listener-count) tuples for all days w/ listeners """ + if not podcast: + raise QueryParameterMissing('podcast') + if isinstance(start, datetime): start = start.isoformat() @@ -106,6 +128,10 @@ def podcast_listener_count_timespan(podcast, start=None, end={}): def episode_listener_counts(episode): """ (Episode-Id, listener-count) tuples for episodes w/ listeners """ + if not episode: + raise QueryParameterMissing('episode') + + r = EpisodeUserState.view('listeners/by_podcast_episode', startkey = [episode.get_id(), None, None], endkey = [episode.get_id(), {}, {}], @@ -121,6 +147,13 @@ def episode_listener_counts(episode): def get_podcasts_episode_states(podcast, user_id): """ Returns the latest episode actions for the podcast's episodes """ + if not podcast: + raise QueryParameterMissing('podcast') + + if not user_id: + raise QueryParameterMissing('user_id') + + db = get_main_database() res = db.view('episode_states/by_user_podcast', startkey = [user_id, podcast.get_id(), None], @@ -135,6 +168,10 @@ def get_podcasts_episode_states(podcast, user_id): def episode_listener_count(episode, start=None, end={}): """ returns the number of users that have listened to this episode """ + if not episode: + raise QueryParameterMissing('episode') + + r = EpisodeUserState.view('listeners/by_episode', startkey = [episode._id, start], endkey = [episode._id, end], @@ -150,6 +187,10 @@ def episode_listener_count(episode, start=None, end={}): def episode_listener_count_timespan(episode, start=None, end={}): """ returns (date, listener-count) tuples for all days w/ listeners """ + if not episode: + raise QueryParameterMissing('episode') + + if isinstance(start, datetime): start = start.isoformat() @@ -170,6 +211,16 @@ def episode_listener_count_timespan(episode, start=None, end={}): def episode_state_for_ref_urls(user, podcast_url, episode_url): + if not user: + raise QueryParameterMissing('user') + + if not podcast_url: + raise QueryParameterMissing('podcast_url') + + if not episode_url: + raise QueryParameterMissing('episode_url') + + cache_key = 'episode-state-%s-%s-%s' % (user._id, sha1(podcast_url).hexdigest(), sha1(episode_url).hexdigest()) @@ -203,6 +254,10 @@ def get_episode_actions(user_id, since=None, until={}, podcast_id=None, device_id=None): """ Returns Episode Actions for the given criteria""" + if not user_id: + raise QueryParameterMissing('user_id') + + since_str = since.strftime('%Y-%m-%dT%H:%M:%S') if since else None until_str = until.strftime('%Y-%m-%dT%H:%M:%S') if until else {} @@ -258,6 +313,13 @@ def get_nth_episode_state(n): def get_duplicate_episode_states(user, episode): + + if not user: + raise QueryParameterMissing('user') + + if not episode: + raise QueryParameterMissing('episode') + states = EpisodeUserState.view('episode_states/by_user_episode', key = [user, episode], include_docs = True, diff --git a/mygpo/db/couchdb/podcast.py b/mygpo/db/couchdb/podcast.py index 4054dd4f..4e8667c4 100644 --- a/mygpo/db/couchdb/podcast.py +++ b/mygpo/db/couchdb/podcast.py @@ -9,6 +9,7 @@ from mygpo.core.models import Podcast, PodcastGroup, PodcastSubscriberData from mygpo.decorators import repeat_on_conflict from mygpo.cache import cache_result from mygpo.couch import get_main_database +from mygpo.db import QueryParameterMissing from mygpo.db.couchdb.utils import multi_request_view, is_couchdb_id @@ -35,6 +36,9 @@ def podcasts_for_tag(tag): Some podcasts might be returned twice """ + if not tag: + raise QueryParameterMissing('tag') + res = multi_request_view(Podcast, 'podcasts/by_tag', wrap = False, startkey = [tag, None], @@ -82,6 +86,10 @@ def get_podcast_languages(): @cache_result(timeout=60*60) def podcast_by_id(podcast_id, current_id=False): + + if not podcast_id: + raise QueryParameterMissing('podcast_id') + r = Podcast.view('podcasts/by_id', key = podcast_id, classes = [Podcast, PodcastGroup], @@ -98,12 +106,20 @@ def podcast_by_id(podcast_id, current_id=False): @cache_result(timeout=60*60) def podcastgroup_by_id(group_id): + + if not group_id: + raise QueryParameterMissing('group_id') + return PodcastGroup.get(group_id) @cache_result(timeout=60*60) def podcast_for_slug(slug): + + if not slug: + raise QueryParameterMissing('slug') + r = Podcast.view('podcasts/by_slug', startkey = [slug, None], endkey = [slug, {}], @@ -138,6 +154,9 @@ def podcast_for_slug_id(slug_id): def podcastgroup_for_slug_id(slug_id): """ Returns the Podcast for either an CouchDB-ID for a Slug """ + if not slug_id: + raise QueryParameterMissing('slug_id') + if is_couchdb_id(slug_id): return PodcastGroup.get(slug_id) @@ -148,6 +167,13 @@ def podcastgroup_for_slug_id(slug_id): def podcasts_by_id(ids): + + if ids is None: + raise QueryParameterMissing('ids') + + if not ids: + return [] + r = Podcast.view('podcasts/by_id', keys = ids, include_docs = True, @@ -160,6 +186,10 @@ def podcasts_by_id(ids): @cache_result(timeout=60*60) def podcast_for_oldid(oldid): + + if not oldid: + raise QueryParameterMissing('oldid') + r = Podcast.view('podcasts/by_oldid', key = long(oldid), classes = [Podcast, PodcastGroup], @@ -175,6 +205,10 @@ def podcast_for_oldid(oldid): @cache_result(timeout=60*60) def podcastgroup_for_oldid(oldid): + + if not oldid: + raise QueryParameterMissing('oldid') + r = PodcastGroup.view('podcasts/groups_by_oldid', key = long(oldid), include_docs = True, @@ -186,6 +220,9 @@ def podcastgroup_for_oldid(oldid): def podcast_for_url(url, create=False): + if not url: + raise QueryParameterMissing('url') + key = 'podcast-by-url-%s' % sha1(url).hexdigest() podcast = cache.get(key) @@ -284,6 +321,13 @@ def all_podcasts_groups(cls): def podcasts_to_dict(ids, use_cache=False): + if ids is None: + raise QueryParameterMissing('ids') + + if not ids: + return dict() + + ids = list(set(ids)) objs = dict() @@ -329,6 +373,10 @@ def podcasts_need_update(): def subscriberdata_for_podcast(podcast_id): + + if not podcast_id: + raise QueryParameterMissing('podcast_id') + r = PodcastSubscriberData.view('podcasts/subscriber_data', key = podcast_id, include_docs = True, @@ -377,6 +425,10 @@ def search_wrapper(result): @cache_result(timeout=60*60) def search(q, offset=0, num_results=20): + + if not q: + return [], 0 + db = get_main_database() #FIXME current couchdbkit can't parse responses for multi-query searches diff --git a/mygpo/db/couchdb/podcast_state.py b/mygpo/db/couchdb/podcast_state.py index acd8eeb0..971dc3f7 100644 --- a/mygpo/db/couchdb/podcast_state.py +++ b/mygpo/db/couchdb/podcast_state.py @@ -1,9 +1,14 @@ from mygpo.users.models import PodcastUserState from mygpo.couch import get_main_database from mygpo.cache import cache_result +from mygpo.db import QueryParameterMissing def all_podcast_states(podcast): + + if not podcast: + raise QueryParameterMissing('podcast') + return PodcastUserState.view('podcast_states/by_podcast', startkey = [podcast.get_id(), None], endkey = [podcast.get_id(), {}], @@ -13,6 +18,10 @@ def all_podcast_states(podcast): @cache_result(timeout=60*60) def subscribed_users(podcast): + + if not podcast: + raise QueryParameterMissing('podcast') + db = get_main_database() res = db.view('subscriptions/by_podcast', @@ -27,6 +36,10 @@ def subscribed_users(podcast): def subscribed_podcast_ids_by_user_id(user_id): + + if not user_id: + raise QueryParameterMissing('user_id') + subscribed = db.view('subscriptions/by_user', startkey = [user_id, True, None, None], endkey = [user_id, True, {}, {}], @@ -39,6 +52,10 @@ def subscribed_podcast_ids_by_user_id(user_id): @cache_result(timeout=60*60) def podcast_subscriber_count(podcast): + + if not podcast: + raise QueryParameterMissing('podcast') + db = get_main_database() subscriber_sum = 0 @@ -57,6 +74,14 @@ def podcast_subscriber_count(podcast): def podcast_state_for_user_podcast(user, podcast): + + if not user: + raise QueryParameterMissing('user') + + if not podcast: + raise QueryParameterMissing('podcast') + + r = PodcastUserState.view('podcast_states/by_podcast', key = [podcast.get_id(), user._id], limit = 1, @@ -79,6 +104,10 @@ def podcast_state_for_user_podcast(user, podcast): def podcast_states_for_user(user): + + if not user: + raise QueryParameterMissing('user') + r = PodcastUserState.view('podcast_states/by_user', startkey = [user._id, None], endkey = [user._id, 'ZZZZ'], @@ -88,6 +117,10 @@ def podcast_states_for_user(user): def podcast_states_for_device(device_id): + + if not device_id: + raise QueryParameterMissing('device_id') + r = PodcastUserState.view('podcast_states/by_device', startkey = [device_id, None], endkey = [device_id, {}], @@ -106,6 +139,10 @@ def podcast_state_count(): def subscribed_podcast_ids_by_device(device): + + if not device: + raise QueryParameterMissing('device') + db = get_main_database() r = db.view('subscriptions/by_device', startkey = [device.id, None], @@ -120,6 +157,9 @@ def subscriptions_by_user(user, public=None): of the users subscriptions """ + if not user: + raise QueryParameterMissing('user') + r = PodcastUserState.view('subscriptions/by_user', startkey = [user._id, public, None, None], endkey = [user._id+'ZZZ', None, None, None], diff --git a/mygpo/db/couchdb/podcastlist.py b/mygpo/db/couchdb/podcastlist.py index 382f1c52..50567773 100644 --- a/mygpo/db/couchdb/podcastlist.py +++ b/mygpo/db/couchdb/podcastlist.py @@ -2,11 +2,20 @@ from random import random from mygpo.share.models import PodcastList from mygpo.cache import cache_result +from mygpo.db import QueryParameterMissing + @cache_result(timeout=60) def podcastlist_for_user_slug(user_id, slug): + if not user_id: + raise QueryParameterMissing('user_id') + + if not slug: + raise QueryParameterMissing('slug') + + r = PodcastList.view('podcastlists/by_user_slug', key = [user_id, slug], include_docs = True, @@ -17,6 +26,9 @@ def podcastlist_for_user_slug(user_id, slug): def podcastlists_for_user(user_id): + if not user_id: + raise QueryParameterMissing('user_id') + r = PodcastList.view('podcastlists/by_user_slug', startkey = [user_id, None], endkey = [user_id, {}], diff --git a/mygpo/db/couchdb/user.py b/mygpo/db/couchdb/user.py index 3b2032d1..a0ace9ee 100644 --- a/mygpo/db/couchdb/user.py +++ b/mygpo/db/couchdb/user.py @@ -1,11 +1,16 @@ from mygpo.cache import cache_result from mygpo.counter import Counter from mygpo.couch import get_main_database +from mygpo.db import QueryParameterMissing from mygpo.db.couchdb.episode import episodes_by_id @cache_result(timeout=60) def get_num_listened_episodes(user): + + if not user: + raise QueryParameterMissing('user') + db = get_main_database() r = db.view('listeners/by_user_podcast', startkey = [user._id, None], @@ -24,14 +29,17 @@ def _wrap_num_listened(obj): @cache_result(timeout=60) -def get_num_played_episodes(self, since=None, until={}): +def get_num_played_episodes(user, since=None, until={}): """ Number of played episodes in interval """ + if not user: + raise QueryParameterMissing('user') + since_str = since.strftime('%Y-%m-%d') if since else None until_str = until.strftime('%Y-%m-%d') if until else {} - startkey = [self._id, since_str] - endkey = [self._id, until_str] + startkey = [user._id, since_str] + endkey = [user._id, until_str] db = get_main_database() res = db.view('listeners/by_user', @@ -47,11 +55,14 @@ def get_num_played_episodes(self, since=None, until={}): @cache_result(timeout=60) -def get_latest_episodes(self, count=10): +def get_latest_episodes(user, count=10): """ Returns the latest episodes that the user has accessed """ - startkey = [self._id, {}] - endkey = [self._id, None] + if not user: + raise QueryParameterMissing('user') + + startkey = [user._id, {}] + endkey = [user._id, None] db = get_main_database() res = db.view('listeners/by_user', @@ -69,16 +80,19 @@ def get_latest_episodes(self, count=10): @cache_result(timeout=60) -def get_seconds_played(self, since=None, until={}): +def get_seconds_played(user, since=None, until={}): """ Returns the number of seconds that the user has listened Can be selected by timespan, podcast and episode """ + if not user: + raise QueryParameterMissing('user') + since_str = since.strftime('%Y-%m-%dT%H:%M:%S') if since else None until_str = until.strftime('%Y-%m-%dT%H:%M:%S') if until else {} - startkey = [self._id, since_str] - endkey = [self._id, until_str] + startkey = [user._id, since_str] + endkey = [user._id, until_str] db = get_main_database() res = db.view('listeners/times_played_by_user', @@ -94,6 +108,10 @@ def get_seconds_played(self, since=None, until={}): @cache_result(timeout=60*60) def suggestions_for_user(user): + + if not user: + raise QueryParameterMissing('user') + from mygpo.users.models import Suggestions r = Suggestions.view('suggestions/by_user', key = user._id, @@ -140,6 +158,13 @@ def deleted_user_count(): @cache_result(timeout=60) def user_history(user, start, length): + + if not user: + raise QueryParameterMissing('user') + + if length <= 0: + return [] + db = get_main_database() res = db.view('history/by_user', descending = True, @@ -154,6 +179,16 @@ def user_history(user, start, length): @cache_result(timeout=60) def device_history(user, device, start, length): + + if not user: + raise QueryParameterMissing('user') + + if not device: + raise QueryParameterMissing('device') + + if length <= 0: + return [] + db = get_main_database() res = db.view('history/by_device', -- 2.11.4.GIT