From 2b31b6a306407f049b5e58daaf8a186ba53eea93 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Sat, 12 Mar 2022 16:51:50 +0100 Subject: [PATCH] Fix for #2325 (author sort order differs from authors order with readonly database) --- cps/db.py | 25 +++++++---- cps/templates/author.html | 4 +- cps/templates/detail.html | 2 +- cps/templates/discover.html | 2 +- cps/templates/index.html | 4 +- cps/templates/listenmp3.html | 2 +- cps/templates/shelf.html | 2 +- cps/templates/shelfdown.html | 2 +- cps/web.py | 80 ++++++++++++++++++------------------ 9 files changed, 65 insertions(+), 58 deletions(-) diff --git a/cps/db.py b/cps/db.py index caf0ee6e..6ff0d03e 100644 --- a/cps/db.py +++ b/cps/db.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import sys +import copy import os import re import ast @@ -776,6 +776,8 @@ class CalibreDB(): # Orders all Authors in the list according to authors sort def order_authors(self, entries, list_return=False, combined=False): + # entries_copy = copy.deepcopy(entries) + # entries_copy =[] for entry in entries: if combined: sort_authors = entry.Books.author_sort.split('&') @@ -785,26 +787,31 @@ class CalibreDB(): sort_authors = entry.author_sort.split('&') ids = [a.id for a in entry.authors] authors_ordered = list() - error = False + # error = False for auth in sort_authors: results = self.session.query(Authors).filter(Authors.sort == auth.lstrip().strip()).all() # ToDo: How to handle not found author name if not len(results): log.error("Author {} not found to display name in right order".format(auth)) - error = True + # error = True break for r in results: if r.id in ids: authors_ordered.append(r) - if not error: + ids.remove(r.id) + for author_id in ids: + result = self.session.query(Authors).filter(Authors.id == author_id).first() + authors_ordered.append(result) + + if list_return: if combined: entry.Books.authors = authors_ordered else: - entry.authors = authors_ordered - if list_return: - return entries - else: - return authors_ordered + entry.ordered_authors = authors_ordered + else: + return authors_ordered + return entries + def get_typeahead(self, database, query, replace=('', ''), tag_filter=true()): query = query or '' diff --git a/cps/templates/author.html b/cps/templates/author.html index d832ad5c..fc811368 100644 --- a/cps/templates/author.html +++ b/cps/templates/author.html @@ -47,7 +47,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {% if not loop.first %} & @@ -110,7 +110,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {{author.name.replace('|',',')}} {% if loop.last %} diff --git a/cps/templates/detail.html b/cps/templates/detail.html index 06357ce8..c2153db8 100644 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -99,7 +99,7 @@

{{entry.title}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {{author.name.replace('|',',')}} {% if not loop.last %} & diff --git a/cps/templates/discover.html b/cps/templates/discover.html index 74448b98..bceb6a8a 100644 --- a/cps/templates/discover.html +++ b/cps/templates/discover.html @@ -20,7 +20,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {% if not loop.first %} & diff --git a/cps/templates/index.html b/cps/templates/index.html index 162adc7d..b69a9284 100644 --- a/cps/templates/index.html +++ b/cps/templates/index.html @@ -19,7 +19,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {% if not loop.first %} & @@ -101,7 +101,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {% if not loop.first %} & diff --git a/cps/templates/listenmp3.html b/cps/templates/listenmp3.html index 279ec28f..7da62a20 100644 --- a/cps/templates/listenmp3.html +++ b/cps/templates/listenmp3.html @@ -105,7 +105,7 @@

diff --git a/cps/templates/shelf.html b/cps/templates/shelf.html index adfead60..2e4cf906 100644 --- a/cps/templates/shelf.html +++ b/cps/templates/shelf.html @@ -44,7 +44,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {% if loop.index > g.config_authors_max and g.config_authors_max != 0 %} {% if not loop.first %} & diff --git a/cps/templates/shelfdown.html b/cps/templates/shelfdown.html index 1d781310..78f00b5e 100644 --- a/cps/templates/shelfdown.html +++ b/cps/templates/shelfdown.html @@ -37,7 +37,7 @@

{{entry.title|shortentitle}}

- {% for author in entry.authors %} + {% for author in entry.ordered_authors %} {{author.name.replace('|',',')}} {% if not loop.last %} & diff --git a/cps/web.py b/cps/web.py index 6d33f809..94f511b1 100644 --- a/cps/web.py +++ b/cps/web.py @@ -300,43 +300,43 @@ def get_matching_tags(): return json_dumps -def get_sort_function(sort, data): +def get_sort_function(sort_param, data): order = [db.Books.timestamp.desc()] - if sort == 'stored': - sort = current_user.get_view_property(data, 'stored') + if sort_param == 'stored': + sort_param = current_user.get_view_property(data, 'stored') else: - current_user.set_view_property(data, 'stored', sort) - if sort == 'pubnew': + current_user.set_view_property(data, 'stored', sort_param) + if sort_param == 'pubnew': order = [db.Books.pubdate.desc()] - if sort == 'pubold': + if sort_param == 'pubold': order = [db.Books.pubdate] - if sort == 'abc': + if sort_param == 'abc': order = [db.Books.sort] - if sort == 'zyx': + if sort_param == 'zyx': order = [db.Books.sort.desc()] - if sort == 'new': + if sort_param == 'new': order = [db.Books.timestamp.desc()] - if sort == 'old': + if sort_param == 'old': order = [db.Books.timestamp] - if sort == 'authaz': + if sort_param == 'authaz': order = [db.Books.author_sort.asc(), db.Series.name, db.Books.series_index] - if sort == 'authza': + if sort_param == 'authza': order = [db.Books.author_sort.desc(), db.Series.name.desc(), db.Books.series_index.desc()] - if sort == 'seriesasc': + if sort_param == 'seriesasc': order = [db.Books.series_index.asc()] - if sort == 'seriesdesc': + if sort_param == 'seriesdesc': order = [db.Books.series_index.desc()] - if sort == 'hotdesc': + if sort_param == 'hotdesc': order = [func.count(ub.Downloads.book_id).desc()] - if sort == 'hotasc': + if sort_param == 'hotasc': order = [func.count(ub.Downloads.book_id).asc()] - if sort is None: - sort = "new" - return order, sort + if sort_param is None: + sort_param = "new" + return order, sort_param -def render_books_list(data, sort, book_id, page): - order = get_sort_function(sort, data) +def render_books_list(data, sort_param, book_id, page): + order = get_sort_function(sort_param, data) if data == "rated": return render_rated_books(page, book_id, order=order) elif data == "discover": @@ -604,7 +604,7 @@ def render_language_books(page, name, order): def render_read_books(page, are_read, as_xml=False, order=None): - sort = order[0] if order else [] + sort_param = order[0] if order else [] if not config.config_read_column: if are_read: db_filter = and_(ub.ReadBook.user_id == int(current_user.id), @@ -614,7 +614,7 @@ def render_read_books(page, are_read, as_xml=False, order=None): entries, random, pagination = calibre_db.fill_indexpage(page, 0, db.Books, db_filter, - sort, + sort_param, False, 0, db.books_series_link, db.Books.id == db.books_series_link.c.book, @@ -629,7 +629,7 @@ def render_read_books(page, are_read, as_xml=False, order=None): entries, random, pagination = calibre_db.fill_indexpage(page, 0, db.Books, db_filter, - sort, + sort_param, False, 0, db.books_series_link, db.Books.id == db.books_series_link.c.book, @@ -656,8 +656,8 @@ def render_read_books(page, are_read, as_xml=False, order=None): title=name, page=pagename, order=order[1]) -def render_archived_books(page, sort): - order = sort[0] or [] +def render_archived_books(page, sort_param): + order = sort_param[0] or [] archived_books = ( ub.session.query(ub.ArchivedBook) .filter(ub.ArchivedBook.user_id == int(current_user.id)) @@ -678,7 +678,7 @@ def render_archived_books(page, sort): name = _(u'Archived Books') + ' (' + str(len(archived_book_ids)) + ')' pagename = "archived" return render_title_template('index.html', random=random, entries=entries, pagination=pagination, - title=name, page=pagename, order=sort[1]) + title=name, page=pagename, order=sort_param[1]) def render_prepare_search_form(cc): @@ -767,32 +767,32 @@ def list_books(): off = int(request.args.get("offset") or 0) limit = int(request.args.get("limit") or config.config_books_per_page) search = request.args.get("search") - sort = request.args.get("sort", "id") + sort_param = request.args.get("sort", "id") order = request.args.get("order", "").lower() state = None join = tuple() - if sort == "state": + if sort_param == "state": state = json.loads(request.args.get("state", "[]")) - elif sort == "tags": + elif sort_param == "tags": order = [db.Tags.name.asc()] if order == "asc" else [db.Tags.name.desc()] join = db.books_tags_link, db.Books.id == db.books_tags_link.c.book, db.Tags - elif sort == "series": + elif sort_param == "series": order = [db.Series.name.asc()] if order == "asc" else [db.Series.name.desc()] join = db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series - elif sort == "publishers": + elif sort_param == "publishers": order = [db.Publishers.name.asc()] if order == "asc" else [db.Publishers.name.desc()] join = db.books_publishers_link, db.Books.id == db.books_publishers_link.c.book, db.Publishers - elif sort == "authors": + elif sort_param == "authors": order = [db.Authors.name.asc(), db.Series.name, db.Books.series_index] if order == "asc" \ else [db.Authors.name.desc(), db.Series.name.desc(), db.Books.series_index.desc()] join = db.books_authors_link, db.Books.id == db.books_authors_link.c.book, db.Authors, \ db.books_series_link, db.Books.id == db.books_series_link.c.book, db.Series - elif sort == "languages": + elif sort_param == "languages": order = [db.Languages.lang_code.asc()] if order == "asc" else [db.Languages.lang_code.desc()] join = db.books_languages_link, db.Books.id == db.books_languages_link.c.book, db.Languages - elif order and sort in ["sort", "title", "authors_sort", "series_index"]: - order = [text(sort + " " + order)] + elif order and sort_param in ["sort", "title", "authors_sort", "series_index"]: + order = [text(sort_param + " " + order)] elif not state: order = [db.Books.timestamp.desc()] @@ -817,7 +817,7 @@ def list_books(): except (KeyError, AttributeError): log.error("Custom Column No.%d is not existing in calibre database", read_column) # Skip linking read column and return None instead of read status - books =calibre_db.session.query(db.Books, None, ub.ArchivedBook.is_archived) + books = calibre_db.session.query(db.Books, None, ub.ArchivedBook.is_archived) books = (books.outerjoin(ub.ArchivedBook, and_(db.Books.id == ub.ArchivedBook.book_id, int(current_user.id) == ub.ArchivedBook.user_id)) .filter(calibre_db.common_filters(allow_show_archived=True)).all()) @@ -1263,7 +1263,7 @@ def extend_search_term(searchterm, def render_adv_search_results(term, offset=None, order=None, limit=None): - sort = order[0] if order else [db.Books.sort] + sort_param = order[0] if order else [db.Books.sort] pagination = None cc = get_cc_columns(filter_config_custom_read=True) @@ -1378,7 +1378,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): log.debug_or_exception(ex) flash(_("Error on search for custom columns, please restart Calibre-Web"), category="error") - q = q.order_by(*sort).all() + q = q.order_by(*sort_param).all() flask_session['query'] = json.dumps(term) ub.store_combo_ids(q) result_count = len(q) @@ -1792,7 +1792,7 @@ def show_book(book_id): entry.tags = sort(entry.tags, key=lambda tag: tag.name) - entry.authors = calibre_db.order_authors([entry]) + entry.ordered_authors = calibre_db.order_authors([entry]) entry.kindle_list = check_send_to_kindle(entry) entry.reader_list = check_read_formats(entry)