From 27dcbcd7e197699a800225f8f51dd784fd89fa57 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 27 Dec 2020 10:24:51 +0100 Subject: [PATCH] paged and orderable shelfs Fix for non writable settings db with non configured calibre-web --- cps/db.py | 2 ++ cps/shelf.py | 34 ++++++++++++++++++++++++++++++---- cps/static/js/main.js | 13 +++++++++++++ cps/templates/shelf.html | 6 +++--- cps/templates/shelf_order.html | 2 +- cps/templates/shelfdown.html | 25 ++++++------------------- cps/ub.py | 20 ++++++++++++-------- 7 files changed, 67 insertions(+), 35 deletions(-) diff --git a/cps/db.py b/cps/db.py index 2e428f72..f30fb609 100644 --- a/cps/db.py +++ b/cps/db.py @@ -445,6 +445,8 @@ class CalibreDB(): cls.config = config cls.dispose() + # toDo: if db changed -> delete shelfs, delete download books, delete read boks, kobo sync?? + if not config.config_calibre_dir: config.invalidate() return False diff --git a/cps/shelf.py b/cps/shelf.py index be102ea9..6dae333c 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -381,27 +381,53 @@ def order_shelf(shelf_id): title=_(u"Change order of Shelf: '%(name)s'", name=shelf.name), shelf=shelf, page="shelforder") +def change_shelf_order(shelf_id, order): + result = calibre_db.session.query(db.Books).join(ub.BookShelf,ub.BookShelf.book_id == db.Books.id)\ + .filter(ub.BookShelf.shelf == shelf_id).order_by(*order).all() + for index, entry in enumerate(result): + book = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id) \ + .filter(ub.BookShelf.book_id == entry.id).first() + book.order = index + try: + ub.session.commit() + except OperationalError: + ub.session.rollback() def render_show_shelf(shelf_type, shelf_id, page_no, sort_param): shelf = ub.session.query(ub.Shelf).filter(ub.Shelf.id == shelf_id).first() # check user is allowed to access shelf if shelf and check_shelf_view_permissions(shelf): + if shelf_type == 1: + # order = [ub.BookShelf.order.asc()] + if sort_param == 'pubnew': + change_shelf_order(shelf_id, [db.Books.pubdate.desc()]) + if sort_param == 'pubold': + change_shelf_order(shelf_id, [db.Books.pubdate]) + if sort_param == 'abc': + change_shelf_order(shelf_id, [db.Books.sort]) + if sort_param == 'zyx': + change_shelf_order(shelf_id, [db.Books.sort.desc()]) + if sort_param == 'new': + change_shelf_order(shelf_id, [db.Books.timestamp.desc()]) + if sort_param == 'old': + change_shelf_order(shelf_id, [db.Books.timestamp]) + if sort_param == 'authaz': + change_shelf_order(shelf_id, [db.Books.author_sort.asc()]) + if sort_param == 'authza': + change_shelf_order(shelf_id, [db.Books.author_sort.desc()]) page = "shelf.html" pagesize = 0 - order = [ub.BookShelf.order.asc()] else: pagesize = sys.maxsize page = 'shelfdown.html' - order = [ub.BookShelf.order.asc()] result, __, pagination = calibre_db.fill_indexpage(page_no, pagesize, db.Books, ub.BookShelf.shelf == shelf_id, - order, + [ub.BookShelf.order.asc()], ub.BookShelf,ub.BookShelf.book_id == db.Books.id) - # delete chelf entries where book is not existent anymore, can happen if book is deleted outside calibre-web wrong_entries = calibre_db.session.query(ub.BookShelf)\ .join(db.Books, ub.BookShelf.book_id == db.Books.id, isouter=True)\ diff --git a/cps/static/js/main.js b/cps/static/js/main.js index d02d3b58..d891d30e 100644 --- a/cps/static/js/main.js +++ b/cps/static/js/main.js @@ -497,6 +497,19 @@ $(function() { ); }); + $("#toggle_order_shelf").click(function() { + $("#new").toggleClass("disabled"); + $("#old").toggleClass("disabled"); + $("#asc").toggleClass("disabled"); + $("#desc").toggleClass("disabled"); + $("#auth_az").toggleClass("disabled"); + $("#auth_za").toggleClass("disabled"); + $("#pub_new").toggleClass("disabled"); + $("#pub_old").toggleClass("disabled"); + var alternative_text = $("#toggle_order_shelf").data('alt-text'); + $("#toggle_order_shelf")[0].attributes['data-alt-text'].value = $("#toggle_order_shelf").html(); + $("#toggle_order_shelf").html(alternative_text); + }); $("#btndeluser").click(function() { ConfirmDialog( diff --git a/cps/templates/shelf.html b/cps/templates/shelf.html index 32e3da4f..2110d905 100644 --- a/cps/templates/shelf.html +++ b/cps/templates/shelf.html @@ -8,8 +8,10 @@ {% if g.user.is_authenticated %} {% if (g.user.role_edit_shelfs() and shelf.is_public ) or not shelf.is_public %}
{{ _('Delete this Shelf') }}
- {{ _('Edit Shelf') }} + {{ _('Edit Shelf Properties') }} {% if entries.__len__() %} + {{ _('Arrange books manually') }} + {% endif %} {% endif %} diff --git a/cps/templates/shelf_order.html b/cps/templates/shelf_order.html index c5a698d2..1e49f29a 100644 --- a/cps/templates/shelf_order.html +++ b/cps/templates/shelf_order.html @@ -37,7 +37,7 @@ {% endfor %} - + {{_('Back')}} {% endblock %} diff --git a/cps/templates/shelfdown.html b/cps/templates/shelfdown.html index 9ec154be..77251e02 100644 --- a/cps/templates/shelfdown.html +++ b/cps/templates/shelfdown.html @@ -55,27 +55,14 @@
- {% if g.user.role_download() %} + {% if g.user.role_download() %} {% if entry.data|length %}
- {% if entry.data|length < 2 %} - - {% for format in entry.data %} - - {{format.format}} ({{ format.uncompressed_size|filesizeformat }}) - - {% endfor %} - {% else %} - - - {% endif %} + {% for format in entry.data %} + + {{format.format}} ({{ format.uncompressed_size|filesizeformat }}) + + {% endfor %}
{% endif %} {% endif %} diff --git a/cps/ub.py b/cps/ub.py index dbc3b419..f11b77b9 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -452,7 +452,7 @@ def migrate_Database(session): if not engine.dialect.has_table(engine.connect(), "archived_book"): ArchivedBook.__table__.create(bind=engine) if not engine.dialect.has_table(engine.connect(), "registration"): - ReadBook.__table__.create(bind=engine) + Registration.__table__.create(bind=engine) with engine.connect() as conn: conn.execute("insert into registration (domain, allow) values('%.%',1)") session.commit() @@ -501,12 +501,16 @@ def migrate_Database(session): for book_shelf in session.query(BookShelf).all(): book_shelf.date_added = datetime.datetime.now() session.commit() - # Handle table exists, but no content - cnt = session.query(Registration).count() - if not cnt: - with engine.connect() as conn: - conn.execute("insert into registration (domain, allow) values('%.%',1)") - session.commit() + try: + # Handle table exists, but no content + cnt = session.query(Registration).count() + if not cnt: + with engine.connect() as conn: + conn.execute("insert into registration (domain, allow) values('%.%',1)") + session.commit() + except exc.OperationalError: # Database is not writeable + print('Settings database is not writeable. Exiting...') + sys.exit(2) try: session.query(exists().where(BookShelf.order)).scalar() except exc.OperationalError: # Database is not compatible, some columns are missing @@ -591,7 +595,7 @@ def migrate_Database(session): session.commit() except exc.OperationalError: print('Settings database is not writeable. Exiting...') - sys.exit(1) + sys.exit(2) def clean_database(session):