From 226cf6fc381e18f48424ead7a2e19f100a879aef Mon Sep 17 00:00:00 2001 From: Ruud Date: Fri, 6 Sep 2013 22:45:37 +0200 Subject: [PATCH] Make sure to not query db when there aren't any ids --- couchpotato/core/media/movie/_base/main.py | 2 + couchpotato/core/plugins/dashboard/main.py | 82 ++++++++++++++++-------------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/couchpotato/core/media/movie/_base/main.py b/couchpotato/core/media/movie/_base/main.py index 1c7eb31..d5d41cc 100644 --- a/couchpotato/core/media/movie/_base/main.py +++ b/couchpotato/core/media/movie/_base/main.py @@ -213,6 +213,8 @@ class MovieBase(MovieTypeBase): q = q.filter(or_(*filter_or)) total_count = q.count() + if total_count == 0: + return 0, [] if order == 'release_order': q = q.order_by(desc(Release.last_edit)) diff --git a/couchpotato/core/plugins/dashboard/main.py b/couchpotato/core/plugins/dashboard/main.py index 5a35167..2da4d8c 100644 --- a/couchpotato/core/plugins/dashboard/main.py +++ b/couchpotato/core/plugins/dashboard/main.py @@ -61,56 +61,60 @@ class Dashboard(Plugin): .order_by(asc(LibraryTitle.simple_title)) active = q.all() + movies = [] - # Do the shuffle - if random: - rndm.shuffle(active) + if len(active) > 0: - movie_ids = [] - for movie in active: - movie_id, profile_id, info, year = movie + # Do the shuffle + if random: + rndm.shuffle(active) - pp = profile_pre.get(profile_id) - if not pp: continue + movie_ids = [] + for movie in active: + movie_id, profile_id, info, year = movie - eta = info.get('release_date', {}) or {} - coming_soon = False + pp = profile_pre.get(profile_id) + if not pp: continue - # Theater quality - if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, year, single = True): - coming_soon = True - elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, year, single = True): - coming_soon = True + eta = info.get('release_date', {}) or {} + coming_soon = False - if coming_soon: + # Theater quality + if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, year, single = True): + coming_soon = True + elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, year, single = True): + coming_soon = True - # Don't list older movies - if ((not late and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or - (late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))): - movie_ids.append(movie_id) + if coming_soon: - if len(movie_ids) >= limit: - break + # Don't list older movies + if ((not late and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or + (late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))): + movie_ids.append(movie_id) - # Get all movie information - movies_raw = db.query(Movie) \ - .options(joinedload_all('library.titles')) \ - .options(joinedload_all('library.files')) \ - .options(joinedload_all('files')) \ - .filter(Movie.id.in_(movie_ids)) \ - .all() + if len(movie_ids) >= limit: + break - # Create dict by movie id - movie_dict = {} - for movie in movies_raw: - movie_dict[movie.id] = movie + if len(movie_ids) > 0: - movies = [] - for movie_id in movie_ids: - movies.append(movie_dict[movie_id].to_dict({ - 'library': {'titles': {}, 'files':{}}, - 'files': {}, - })) + # Get all movie information + movies_raw = db.query(Movie) \ + .options(joinedload_all('library.titles')) \ + .options(joinedload_all('library.files')) \ + .options(joinedload_all('files')) \ + .filter(Movie.id.in_(movie_ids)) \ + .all() + + # Create dict by movie id + movie_dict = {} + for movie in movies_raw: + movie_dict[movie.id] = movie + + for movie_id in movie_ids: + movies.append(movie_dict[movie_id].to_dict({ + 'library': {'titles': {}, 'files':{}}, + 'files': {}, + })) return { 'success': True,