1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-11-24 18:47:23 +00:00

Some little code refactoring

This commit is contained in:
Ozzieisaacs 2021-05-02 10:05:25 +02:00
parent 8dc11e89bd
commit 02fc698f1c
3 changed files with 38 additions and 67 deletions

View File

@ -495,30 +495,30 @@ def check_valid_restricted_column(column):
def update_view_configuration(): def update_view_configuration():
to_save = request.form.to_dict() 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_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_int = lambda x: config.set_from_dictionary(to_save, x, int)
_config_string("config_calibre_web_title") _config_string(to_save, "config_calibre_web_title")
_config_string("config_columns_to_ignore") _config_string(to_save, "config_columns_to_ignore")
if _config_string("config_title_regex"): if _config_string(to_save, "config_title_regex"):
calibre_db.update_title_sort(config) calibre_db.update_title_sort(config)
if not check_valid_read_column(to_save.get("config_read_column", "0")): if not check_valid_read_column(to_save.get("config_read_column", "0")):
flash(_(u"Invalid Read Column"), category="error") flash(_(u"Invalid Read Column"), category="error")
log.debug("Invalid Read column") log.debug("Invalid Read column")
return view_configuration() 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")): if not check_valid_restricted_column(to_save.get("config_restricted_column", "0")):
flash(_(u"Invalid Restricted Column"), category="error") flash(_(u"Invalid Restricted Column"), category="error")
log.debug("Invalid Restricted Column") log.debug("Invalid Restricted Column")
return view_configuration() return view_configuration()
_config_int("config_restricted_column") _config_int(to_save, "config_restricted_column")
_config_int("config_theme") _config_int(to_save, "config_theme")
_config_int("config_random_books") _config_int(to_save, "config_random_books")
_config_int("config_books_per_page") _config_int(to_save, "config_books_per_page")
_config_int("config_authors_max") _config_int(to_save, "config_authors_max")
config.config_default_role = constants.selected_roles(to_save) config.config_default_role = constants.selected_roles(to_save)

View File

@ -152,12 +152,8 @@ def HandleSyncRequest():
# in case of external changes (e.g: adding a book through Calibre). # in case of external changes (e.g: adding a book through Calibre).
calibre_db.reconnect_db(config, ub.app_DB_path) calibre_db.reconnect_db(config, ub.app_DB_path)
only_kobo_shelves = ( only_kobo_shelves = calibre_db.session.query(ub.Shelf).filter(ub.Shelf.user_id == current_user.id)\
calibre_db.session.query(ub.Shelf) .filter(ub.Shelf.kobo_sync).count() > 0
.filter(ub.Shelf.user_id == current_user.id)
.filter(ub.Shelf.kobo_sync)
.count()
) > 0
if only_kobo_shelves: if only_kobo_shelves:
changed_entries = ( changed_entries = (
@ -168,7 +164,7 @@ def HandleSyncRequest():
.join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id) .join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id)
.filter(or_(db.Books.last_modified > sync_token.books_last_modified, .filter(or_(db.Books.last_modified > sync_token.books_last_modified,
ub.BookShelf.date_added > 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(db.Books.id)
.order_by(ub.ArchivedBook.last_modified) .order_by(ub.ArchivedBook.last_modified)
.join(ub.BookShelf, db.Books.id == ub.BookShelf.book_id) .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) 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) .join(db.Data).outerjoin(ub.ArchivedBook, db.Books.id == ub.ArchivedBook.book_id)
.filter(db.Books.last_modified > sync_token.books_last_modified) .filter(db.Books.last_modified > sync_token.books_last_modified)
.filter(calibre_db.common_filters())
.filter(db.Data.format.in_(KOBO_FORMATS)) .filter(db.Data.format.in_(KOBO_FORMATS))
.order_by(db.Books.last_modified) .order_by(db.Books.last_modified)
.order_by(db.Books.id) .order_by(db.Books.id)
@ -230,57 +227,38 @@ def HandleSyncRequest():
new_books_last_created = max(ts_created, new_books_last_created) new_books_last_created = max(ts_created, new_books_last_created)
max_change = (changed_entries max_change = changed_entries.from_self().filter(ub.ArchivedBook.is_archived)\
.from_self() .order_by(func.datetime(ub.ArchivedBook.last_modified).desc()).first()
.filter(ub.ArchivedBook.is_archived)
.order_by(func.datetime(ub.ArchivedBook.last_modified).desc()) max_change = max_change.last_modified if max_change else new_archived_last_modified
.first()
)
if max_change:
max_change = max_change.last_modified
else:
max_change = new_archived_last_modified
new_archived_last_modified = max(new_archived_last_modified, max_change) new_archived_last_modified = max(new_archived_last_modified, max_change)
# no. of books returned # no. of books returned
book_count = changed_entries.count() book_count = changed_entries.count()
# last entry: # last entry:
if book_count: books_last_id = changed_entries.all()[-1].Books.id or -1 if book_count else -1
books_last_id = changed_entries.all()[-1].Books.id or -1
else:
books_last_id = -1
# generate reading state data # generate reading state data
changed_reading_states = ub.session.query(ub.KoboReadingState) changed_reading_states = ub.session.query(ub.KoboReadingState)
if only_kobo_shelves: if only_kobo_shelves:
changed_reading_states = ( changed_reading_states = changed_reading_states.join(ub.BookShelf,
changed_reading_states.join(ub.BookShelf, ub.KoboReadingState.book_id == ub.BookShelf.book_id) ub.KoboReadingState.book_id == ub.BookShelf.book_id)\
.join(ub.Shelf) .join(ub.Shelf)\
.filter( .filter(ub.Shelf.kobo_sync,
ub.Shelf.kobo_sync, or_(
or_( func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified,
func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified, ub.BookShelf.date_added > sync_token.books_last_modified
ub.BookShelf.date_added > sync_token.books_last_modified )).distinct()
)
)
).distinct()
else: else:
changed_reading_states = ( changed_reading_states = changed_reading_states.filter(
changed_reading_states.filter( func.datetime(ub.KoboReadingState.last_modified) > sync_token.reading_state_last_modified)
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,
changed_reading_states = ( ub.KoboReadingState.book_id.notin_(reading_states_in_new_entitlements)))
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(): 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() 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) 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_created = new_books_last_created
sync_token.books_last_modified = new_books_last_modified sync_token.books_last_modified = new_books_last_modified

View File

@ -242,16 +242,9 @@ def edit_shelf(shelf_id):
def create_edit_shelf(shelf, title, page, shelf_id=False): def create_edit_shelf(shelf, title, page, shelf_id=False):
if request.method == "POST": if request.method == "POST":
to_save = request.form.to_dict() to_save = request.form.to_dict()
if "is_public" in to_save: shelf.is_public = 1 if to_save.get("is_public") else 0
shelf.is_public = 1
else:
shelf.is_public = 0
if config.config_kobo_sync: if config.config_kobo_sync:
if "kobo_sync" in to_save: shelf.kobo_sync = True if to_save.get("kobo_sync") else False
shelf.kobo_sync = True
else:
shelf.kobo_sync = False
if check_shelf_is_unique(shelf, to_save, shelf_id): if check_shelf_is_unique(shelf, to_save, shelf_id):
shelf.name = to_save["title"] shelf.name = to_save["title"]