From 0feb62c142bac7818ff4947218e6b3789098522f Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sun, 9 Jan 2022 13:54:35 +0100 Subject: [PATCH] Improvements delete user (delete also depending entries in tables) Fixes kobo sync with sync only selected shelfs --- cps/admin.py | 9 +++++++++ cps/kobo.py | 5 +++-- cps/kobo_sync_status.py | 3 ++- cps/shelf.py | 4 ++++ cps/ub.py | 4 ++-- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 29f5fb20..706dce39 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -1415,7 +1415,16 @@ def _delete_user(content): for us in ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id): ub.session.query(ub.BookShelf).filter(us.id == ub.BookShelf.shelf).delete() ub.session.query(ub.Shelf).filter(content.id == ub.Shelf.user_id).delete() + ub.session.query(ub.Bookmark).filter(content.id == ub.Bookmark.user_id).delete() ub.session.query(ub.User).filter(ub.User.id == content.id).delete() + ub.session.query(ub.ArchivedBook).filter(ub.ArchivedBook.user_id == content.id).delete() + ub.session.query(ub.RemoteAuthToken).filter(ub.RemoteAuthToken.user_id == content.id).delete() + ub.session.query(ub.User_Sessions).filter(ub.User_Sessions.user_id == content.id).delete() + ub.session.query(ub.KoboSyncedBooks).filter(ub.KoboSyncedBooks.user_id == content.id).delete() + # delete KoboReadingState and all it's children + kobo_entries = ub.session.query(ub.KoboReadingState).filter(ub.KoboReadingState.user_id == content.id).all() + for kobo_entry in kobo_entries: + ub.session.delete(kobo_entry) ub.session_commit() log.info(u"User {} deleted".format(content.name)) return(_(u"User '%(nick)s' deleted", nick=content.name)) diff --git a/cps/kobo.py b/cps/kobo.py index 706056d3..67304c93 100644 --- a/cps/kobo.py +++ b/cps/kobo.py @@ -297,7 +297,8 @@ def HandleSyncRequest(): 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))) + ub.KoboReadingState.book_id.notin_(reading_states_in_new_entitlements)))\ + .order_by(ub.KoboReadingState.last_modified) cont_sync |= bool(changed_reading_states.count() > SYNC_ITEM_LIMIT) for kobo_reading_state in changed_reading_states.limit(SYNC_ITEM_LIMIT).all(): book = calibre_db.session.query(db.Books).filter(db.Books.id == kobo_reading_state.book_id).one_or_none() @@ -849,7 +850,7 @@ def get_ub_read_status(kobo_read_status): def get_or_create_reading_state(book_id): book_read = ub.session.query(ub.ReadBook).filter(ub.ReadBook.book_id == book_id, - ub.ReadBook.user_id == current_user.id).one_or_none() + ub.ReadBook.user_id == int(current_user.id)).one_or_none() if not book_read: book_read = ub.ReadBook(user_id=current_user.id, book_id=book_id) if not book_read.kobo_reading_state: diff --git a/cps/kobo_sync_status.py b/cps/kobo_sync_status.py index a22ec165..6df0a503 100644 --- a/cps/kobo_sync_status.py +++ b/cps/kobo_sync_status.py @@ -60,7 +60,7 @@ def change_archived_books(book_id, state=None, message=None): return archived_book.is_archived -# select all books which are synced by the current user and do not belong to a synced shelf and them to archive +# select all books which are synced by the current user and do not belong to a synced shelf and set them to archive # select all shelves from current user which are synced and do not belong to the "only sync" shelves def update_on_sync_shelfs(user_id): books_to_archive = (ub.session.query(ub.KoboSyncedBooks) @@ -75,6 +75,7 @@ def update_on_sync_shelfs(user_id): .filter(ub.KoboSyncedBooks.user_id == user_id).delete() ub.session_commit() + # Search all shelf which are currently not synced shelves_to_archive = ub.session.query(ub.Shelf).filter(ub.Shelf.user_id == user_id).filter( ub.Shelf.kobo_sync == 0).all() for a in shelves_to_archive: diff --git a/cps/shelf.py b/cps/shelf.py index b1fbdd04..29a4fe99 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -251,6 +251,10 @@ def create_edit_shelf(shelf, page_title, page, shelf_id=False): is_public = 1 if to_save.get("is_public") else 0 if config.config_kobo_sync: shelf.kobo_sync = True if to_save.get("kobo_sync") else False + if shelf.kobo_sync: + ub.session.query(ub.ShelfArchive).filter(ub.ShelfArchive.user_id == current_user.id).filter( + ub.ShelfArchive.uuid == shelf.uuid).delete() + ub.session_commit() shelf_title = to_save.get("title", "") if check_shelf_is_unique(shelf, shelf_title, is_public, shelf_id): shelf.name = shelf_title diff --git a/cps/ub.py b/cps/ub.py index 6b3c27ca..1d5db58a 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -421,8 +421,8 @@ class KoboReadingState(Base): book_id = Column(Integer) last_modified = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow) priority_timestamp = Column(DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow) - current_bookmark = relationship("KoboBookmark", uselist=False, backref="kobo_reading_state", cascade="all") - statistics = relationship("KoboStatistics", uselist=False, backref="kobo_reading_state", cascade="all") + current_bookmark = relationship("KoboBookmark", uselist=False, backref="kobo_reading_state", cascade="all, delete") + statistics = relationship("KoboStatistics", uselist=False, backref="kobo_reading_state", cascade="all, delete") class KoboBookmark(Base):