From 02fc698f1c5af3ece937153af6b0d50f5aa10ea9 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 2 May 2021 10:05:25 +0200 Subject: [PATCH] Some little code refactoring --- cps/admin.py | 22 ++++++++-------- cps/kobo.py | 72 ++++++++++++++++++---------------------------------- cps/shelf.py | 11 ++------ 3 files changed, 38 insertions(+), 67 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 633ee0f2..230672bf 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -495,30 +495,30 @@ def check_valid_restricted_column(column): def update_view_configuration(): to_save = request.form.to_dict() - _config_string = lambda x: config.set_from_dictionary(to_save, x, lambda y: y.strip() if y else y) - _config_int = lambda x: config.set_from_dictionary(to_save, x, int) + # _config_string = lambda x: config.set_from_dictionary(to_save, x, lambda y: y.strip() if y else y) + # _config_int = lambda x: config.set_from_dictionary(to_save, x, int) - _config_string("config_calibre_web_title") - _config_string("config_columns_to_ignore") - if _config_string("config_title_regex"): + _config_string(to_save, "config_calibre_web_title") + _config_string(to_save, "config_columns_to_ignore") + if _config_string(to_save, "config_title_regex"): calibre_db.update_title_sort(config) if not check_valid_read_column(to_save.get("config_read_column", "0")): flash(_(u"Invalid Read Column"), category="error") log.debug("Invalid Read column") return view_configuration() - _config_int("config_read_column") + _config_int(to_save, "config_read_column") if not check_valid_restricted_column(to_save.get("config_restricted_column", "0")): flash(_(u"Invalid Restricted Column"), category="error") log.debug("Invalid Restricted Column") return view_configuration() - _config_int("config_restricted_column") + _config_int(to_save, "config_restricted_column") - _config_int("config_theme") - _config_int("config_random_books") - _config_int("config_books_per_page") - _config_int("config_authors_max") + _config_int(to_save, "config_theme") + _config_int(to_save, "config_random_books") + _config_int(to_save, "config_books_per_page") + _config_int(to_save, "config_authors_max") config.config_default_role = constants.selected_roles(to_save) diff --git a/cps/kobo.py b/cps/kobo.py index 59961586..c3bd82e5 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -152,12 +152,8 @@ def HandleSyncRequest(): # in case of external changes (e.g: adding a book through Calibre). calibre_db.reconnect_db(config, ub.app_DB_path) - only_kobo_shelves = ( - calibre_db.session.query(ub.Shelf) - .filter(ub.Shelf.user_id == current_user.id) - .filter(ub.Shelf.kobo_sync) - .count() - ) > 0 + only_kobo_shelves = calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id)\ + .filter(ub.Shelf.kobo_sync).count() > 0 if only_kobo_shelves: changed_entries = ( @@ -168,7 +164,7 @@ def HandleSyncRequest(): .join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id) .filter(or_(db.Books.last_modified > sync_token.books_last_modified, ub.BookShelf.date_added > sync_token.books_last_modified)) - .filter(db.Data.format.in_(KOBO_FORMATS)) + .filter(db.Data.format.in_(KOBO_FORMATS)).filter(calibre_db.common_filters()) .order_by(db.Books.id) .order_by(ub.ArchivedBook.last_modified) .join(ub.BookShelf, db.Books.id == ub.BookShelf.book_id) @@ -181,6 +177,7 @@ def HandleSyncRequest(): calibre_db.session.query(db.Books, ub.ArchivedBook.last_modified, ub.ArchivedBook.is_archived) .join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id) .filter(db.Books.last_modified > sync_token.books_last_modified) + .filter(calibre_db.common_filters()) .filter(db.Data.format.in_(KOBO_FORMATS)) .order_by(db.Books.last_modified) .order_by(db.Books.id) @@ -230,57 +227,38 @@ def HandleSyncRequest(): new_books_last_created = max(ts_created, new_books_last_created) - max_change = (changed_entries - .from_self() - .filter(ub.ArchivedBook.is_archived) - .order_by(func.datetime(ub.ArchivedBook.last_modified).desc()) - .first() - ) - if max_change: - max_change = max_change.last_modified - else: - max_change = new_archived_last_modified + max_change = changed_entries.from_self().filter(ub.ArchivedBook.is_archived)\ + .order_by(func.datetime(ub.ArchivedBook.last_modified).desc()).first() + + max_change = max_change.last_modified if max_change else new_archived_last_modified + new_archived_last_modified = max(new_archived_last_modified, max_change) # no. of books returned book_count = changed_entries.count() # last entry: - if book_count: - books_last_id = changed_entries.all()[-1].Books.id or -1 - else: - books_last_id = -1 + books_last_id = changed_entries.all()[-1].Books.id or -1 if book_count else -1 # generate reading state data changed_reading_states = ub.session.query(ub.KoboReadingState) if only_kobo_shelves: - changed_reading_states = ( - changed_reading_states.join(ub.BookShelf, ub.KoboReadingState.book_id == ub.BookShelf.book_id) - .join(ub.Shelf) - .filter( - ub.Shelf.kobo_sync, - or_( - func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified, - ub.BookShelf.date_added > sync_token.books_last_modified - ) - ) - ).distinct() - + changed_reading_states = changed_reading_states.join(ub.BookShelf, + ub.KoboReadingState.book_id == ub.BookShelf.book_id)\ + .join(ub.Shelf)\ + .filter(ub.Shelf.kobo_sync, + or_( + func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified, + ub.BookShelf.date_added > sync_token.books_last_modified + )).distinct() else: - changed_reading_states = ( - changed_reading_states.filter( - func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified - ) - ) - changed_reading_states = ( - changed_reading_states.filter( - and_( - ub.KoboReadingState.user_id == current_user.id, - ub.KoboReadingState.book_id.notin_(reading_states_in_new_entitlements) - ) - ) - ) + changed_reading_states = changed_reading_states.filter( + func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified) + + changed_reading_states = changed_reading_states.filter( + and_(ub.KoboReadingState.user_id == current_user.id, + ub.KoboReadingState.book_id.notin_(reading_states_in_new_entitlements))) for kobo_reading_state in changed_reading_states.all(): book = calibre_db.session.query(db.Books).filter(db.Books.id == kobo_reading_state.book_id).one_or_none() @@ -292,7 +270,7 @@ def HandleSyncRequest(): }) new_reading_state_last_modified = max(new_reading_state_last_modified, kobo_reading_state.last_modified) - sync_shelves(sync_token, sync_results, only_kobo_shelves=only_kobo_shelves) + sync_shelves(sync_token, sync_results, only_kobo_shelves) sync_token.books_last_created = new_books_last_created sync_token.books_last_modified = new_books_last_modified diff --git a/cps/shelf.py b/cps/shelf.py index 1024b30e..cccfbeea 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -242,16 +242,9 @@ def edit_shelf(shelf_id): def create_edit_shelf(shelf, title, page, shelf_id=False): if request.method == "POST": to_save = request.form.to_dict() - if "is_public" in to_save: - shelf.is_public = 1 - else: - shelf.is_public = 0 - + shelf.is_public = 1 if to_save.get("is_public") else 0 if config.config_kobo_sync: - if "kobo_sync" in to_save: - shelf.kobo_sync = True - else: - shelf.kobo_sync = False + shelf.kobo_sync = True if to_save.get("kobo_sync") else False if check_shelf_is_unique(shelf, to_save, shelf_id): shelf.name = to_save["title"]