Browse Source

Optimize dashboard soon listing

pull/2133/head
Ruud 12 years ago
parent
commit
c41b3a612a
  1. 108
      couchpotato/core/plugins/dashboard/main.py

108
couchpotato/core/plugins/dashboard/main.py

@ -40,64 +40,76 @@ class Dashboard(Plugin):
profile_pre[profile.get('id')] = contains profile_pre[profile.get('id')] = contains
# Get all active movies
active_status, snatched_status, downloaded_status, available_status = fireEvent('status.get', ['active', 'snatched', 'downloaded', 'available'], single = True)
subq = db.query(Movie).filter(Movie.status_id == active_status.get('id')).subquery()
q = db.query(Movie).join((subq, subq.c.id == Movie.id)) \
.options(joinedload_all('releases')) \
.options(joinedload_all('library.titles')) \
.options(joinedload_all('library.files')) \
.options(joinedload_all('status')) \
.options(joinedload_all('files'))
# Add limit # Add limit
limit = 12 limit = 12
if limit_offset: if limit_offset:
splt = splitString(limit_offset) if isinstance(limit_offset, (str, unicode)) else limit_offset splt = splitString(limit_offset) if isinstance(limit_offset, (str, unicode)) else limit_offset
limit = tryInt(splt[0]) limit = tryInt(splt[0])
all_movies = q.all() # Get all active movies
active_status = fireEvent('status.get', ['active'], single = True)
active = db.query(Movie) \
.filter(Movie.status_id == active_status.get('id')) \
.all()
all_movie_ids = [r.id for r in active]
# Do the shuffle
if random: if random:
rndm.shuffle(all_movies) rndm.shuffle(all_movie_ids)
group_limit = limit * 5
group_offset = 0
movies = [] movies = []
for movie in all_movies:
pp = profile_pre.get(movie.profile_id) while group_offset < len(all_movie_ids) and len(movies) < limit:
eta = movie.library.info.get('release_date', {}) or {}
coming_soon = False movie_ids = all_movie_ids[group_offset:group_offset + group_limit]
group_offset += group_limit
# Theater quality
if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, movie.library.year, single = True): # Only joined needed
coming_soon = True q = db.query(Movie) \
if pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, movie.library.year, single = True): .options(joinedload_all('library')) \
coming_soon = True .filter(Movie.id.in_(movie_ids))
all_movies = q.all()
# Skip if movie is snatched/downloaded/available
skip = False for movie in all_movies:
for release in movie.releases: pp = profile_pre.get(movie.profile_id)
if release.status_id in [snatched_status.get('id'), downloaded_status.get('id'), available_status.get('id')]: if not pp: continue
skip = True
break eta = movie.library.info.get('release_date', {}) or {}
if skip: coming_soon = False
continue
# Theater quality
if coming_soon: if pp.get('theater') and fireEvent('movie.searcher.could_be_released', True, eta, movie.library.year, single = True):
temp = movie.to_dict({ coming_soon = True
'library': {'titles': {}, 'files':{}}, elif pp.get('dvd') and fireEvent('movie.searcher.could_be_released', False, eta, movie.library.year, single = True):
'files': {}, coming_soon = True
})
if coming_soon:
# 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 # Don't list older movies
(late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))): if ((not late and (not eta.get('dvd') and not eta.get('theater') or eta.get('dvd') and eta.get('dvd') > (now - 2419200))) or
movies.append(temp) (late and (eta.get('dvd', 0) > 0 or eta.get('theater')) and eta.get('dvd') < (now - 2419200))):
movies.append(movie.id)
if len(movies) >= limit:
break if len(movies) >= limit:
break
db.expire_all()
# 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_(movies)) \
.all()
movies = []
for r in movies_raw:
movies.append(r.to_dict({
'library': {'titles': {}, 'files':{}},
'files': {},
}))
return { return {
'success': True, 'success': True,
'empty': len(movies) == 0, 'empty': len(movies) == 0,

Loading…
Cancel
Save