From d648785471b8213dc0d080c563aafba07f5408ab Mon Sep 17 00:00:00 2001 From: mmonkey Date: Fri, 17 Sep 2021 01:42:56 -0500 Subject: [PATCH] Review feedback fixes --- cps/helper.py | 5 +++-- cps/jinjia.py | 2 ++ cps/schedule.py | 7 +++++-- cps/services/background_scheduler.py | 4 ++-- cps/tasks/thumbnail.py | 1 + cps/templates/book_cover.html | 2 +- cps/ub.py | 2 +- cps/web.py | 22 +++++++++++----------- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/cps/helper.py b/cps/helper.py index 758d2531..6f22a701 100644 --- a/cps/helper.py +++ b/cps/helper.py @@ -554,13 +554,14 @@ def get_thumbnails_for_books(books): cache = fs.FileSystem() thumbnail_files = cache.list_cache_files(fs.CACHE_TYPE_THUMBNAILS) - return ub.session\ + thumbnails = ub.session\ .query(ub.Thumbnail)\ .filter(ub.Thumbnail.book_id.in_(book_ids))\ - .filter(ub.Thumbnail.filename.in_(thumbnail_files))\ .filter(ub.Thumbnail.expiration > datetime.utcnow())\ .all() + return list(filter(lambda t: t.filename in thumbnail_files, thumbnails)) + def get_thumbnails_for_book_series(series): books = list(map(lambda s: s[0], series)) diff --git a/cps/jinjia.py b/cps/jinjia.py index ac6d3d33..5f86478c 100644 --- a/cps/jinjia.py +++ b/cps/jinjia.py @@ -129,10 +129,12 @@ def formatseriesindex_filter(series_index): return series_index return 0 + @jinjia.app_template_filter('escapedlink') def escapedlink_filter(url, text): return "{}".format(url, escape(text)) + @jinjia.app_template_filter('uuidfilter') def uuidfilter(var): return uuid4() diff --git a/cps/schedule.py b/cps/schedule.py index 7ee43410..8ab9c732 100644 --- a/cps/schedule.py +++ b/cps/schedule.py @@ -18,6 +18,7 @@ from __future__ import division, print_function, unicode_literals +from datetime import datetime from .services.background_scheduler import BackgroundScheduler from .tasks.database import TaskReconnectDatabase from .tasks.thumbnail import TaskSyncCoverThumbnailCache, TaskGenerateCoverThumbnails @@ -26,8 +27,10 @@ from .tasks.thumbnail import TaskSyncCoverThumbnailCache, TaskGenerateCoverThumb def register_jobs(): scheduler = BackgroundScheduler() - # Generate 100 book cover thumbnails every 5 minutes - scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(limit=100), trigger='cron', minute='*/5') + # Generate up to 1000 book covers daily + generate_thumbnails_task = scheduler.add_task(user=None, task=lambda: TaskGenerateCoverThumbnails(limit=1000), + trigger='interval', days=1) + generate_thumbnails_task.modify(next_run_time=datetime.now()) # Cleanup book cover cache every 6 hours scheduler.add_task(user=None, task=lambda: TaskSyncCoverThumbnailCache(), trigger='cron', minute='15', hour='*/6') diff --git a/cps/services/background_scheduler.py b/cps/services/background_scheduler.py index efa57379..122168e8 100644 --- a/cps/services/background_scheduler.py +++ b/cps/services/background_scheduler.py @@ -41,7 +41,7 @@ class BackgroundScheduler: return cls._instance def add(self, func, trigger, **trigger_args): - self.scheduler.add_job(func=func, trigger=trigger, **trigger_args) + return self.scheduler.add_job(func=func, trigger=trigger, **trigger_args) def add_task(self, user, task, trigger, **trigger_args): def scheduled_task(): @@ -49,4 +49,4 @@ class BackgroundScheduler: self.log.info('Running scheduled task in background: ' + worker_task.name + ': ' + worker_task.message) WorkerThread.add(user, worker_task) - self.add(func=scheduled_task, trigger=trigger, **trigger_args) + return self.add(func=scheduled_task, trigger=trigger, **trigger_args) diff --git a/cps/tasks/thumbnail.py b/cps/tasks/thumbnail.py index 70ddc06b..fed12e8b 100644 --- a/cps/tasks/thumbnail.py +++ b/cps/tasks/thumbnail.py @@ -37,6 +37,7 @@ except (ImportError, RuntimeError) as e: THUMBNAIL_RESOLUTION_1X = 1 THUMBNAIL_RESOLUTION_2X = 2 +THUMBNAIL_RESOLUTION_3X = 3 class TaskGenerateCoverThumbnails(CalibreTask): diff --git a/cps/templates/book_cover.html b/cps/templates/book_cover.html index 67e7c24b..884ff4cd 100644 --- a/cps/templates/book_cover.html +++ b/cps/templates/book_cover.html @@ -1,4 +1,4 @@ -{% macro book_cover_image(book, thumbnails, title) -%} +{% macro book_cover_image(book, thumbnails, title=None) -%} {%- set book_title = book.title if book.title else book.name -%} {%- set book_title = title if title else book_title -%} {% set srcset = thumbnails|get_book_thumbnail_srcset if thumbnails|length else '' %} diff --git a/cps/ub.py b/cps/ub.py index 9d3a5845..e0bee4ef 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -532,7 +532,7 @@ class Thumbnail(Base): resolution = Column(SmallInteger, default=1) filename = Column(String, default=filename) generated_at = Column(DateTime, default=lambda: datetime.datetime.utcnow()) - expiration = Column(DateTime, default=lambda: datetime.datetime.utcnow() + datetime.timedelta(days=30)) + expiration = Column(DateTime, default=lambda: datetime.datetime.utcnow() + datetime.timedelta(days=90)) # Add missing tables during migration of database diff --git a/cps/web.py b/cps/web.py index 64bdd8e4..f43bc6ad 100644 --- a/cps/web.py +++ b/cps/web.py @@ -416,7 +416,7 @@ def render_books_list(data, sort, book_id, page): db.Books.id == db.books_series_link.c.book, db.Series) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, title=_(u"Books"), page=website, thumbnails=thumbnails) @@ -467,7 +467,7 @@ def render_hot_books(page): ub.delete_download(book.Downloads.book_id) numBooks = entries.__len__() pagination = Pagination(page, config.config_books_per_page, numBooks) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, title=_(u"Hot Books (Most Downloaded)"), page="hot", thumbnails=thumbnails) else: @@ -498,7 +498,7 @@ def render_downloaded_books(page, order, user_id): ub.delete_download(book.id) user = ub.session.query(ub.User).filter(ub.User.id == user_id).first() - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, @@ -551,7 +551,7 @@ def render_publisher_books(page, book_id, order): db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=book_id, title=_(u"Publisher: %(name)s", name=publisher.name), page="publisher", thumbnails=thumbnails) @@ -566,7 +566,7 @@ def render_series_books(page, book_id, order): db.Books, db.Books.series.any(db.Series.id == book_id), [order[0]]) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id, title=_(u"Series: %(serie)s", serie=name.name), page="series", thumbnails=thumbnails) @@ -581,7 +581,7 @@ def render_ratings_books(page, book_id, order): db.Books.ratings.any(db.Ratings.id == book_id), [order[0]]) if name and name.rating <= 10: - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id, title=_(u"Rating: %(rating)s stars", rating=int(name.rating / 2)), page="ratings", thumbnails=thumbnails) @@ -596,7 +596,7 @@ def render_formats_books(page, book_id, order): db.Books, db.Books.data.any(db.Data.format == book_id.upper()), [order[0]]) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, pagination=pagination, entries=entries, id=book_id, title=_(u"File format: %(format)s", format=name.format), page="formats", thumbnails=thumbnails) @@ -614,7 +614,7 @@ def render_category_books(page, book_id, order): db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=book_id, title=_(u"Category: %(name)s", name=name.name), page="category", thumbnails=thumbnails) @@ -635,7 +635,7 @@ def render_language_books(page, name, order): db.Books, db.Books.languages.any(db.Languages.lang_code == name), [order[0]]) - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, id=name, title=_(u"Language: %(name)s", name=lang_name), page="language", thumbnails=thumbnails) @@ -688,7 +688,7 @@ def render_read_books(page, are_read, as_xml=False, order=None): name = _(u'Unread Books') + ' (' + str(pagination.total_count) + ')' pagename = "unread" - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, title=name, page=pagename, thumbnails=thumbnails) @@ -713,7 +713,7 @@ def render_archived_books(page, order): name = _(u'Archived Books') + ' (' + str(len(archived_book_ids)) + ')' pagename = "archived" - thumbnails = get_thumbnails_for_books(entries + random if random else entries) + thumbnails = get_thumbnails_for_books(entries + random if type(random) is list else entries) return render_title_template('index.html', random=random, entries=entries, pagination=pagination, title=name, page=pagename, thumbnails=thumbnails)