From 5ed3b1cf5351f4cb06f225b6c535bcb8c652b96c Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Wed, 1 Dec 2021 21:38:43 +0100 Subject: [PATCH] On master: Order of language count in /language (fixes #2200) --- cps/db.py | 27 ++++++++++++++++++--------- cps/isoLanguages.py | 1 + cps/templates/languages.html | 6 +++--- cps/web.py | 16 ++++------------ 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cps/db.py b/cps/db.py index 4e76072d..70da9108 100644 --- a/cps/db.py +++ b/cps/db.py @@ -796,18 +796,27 @@ class CalibreDB(): return result[offset:limit_all], result_count, pagination # Creates for all stored languages a translated speaking name in the array for the UI - def speaking_language(self, languages=None, return_all_languages=False, reverse_order=False): + def speaking_language(self, languages=None, return_all_languages=False, with_count=False, reverse_order=False): from . import get_locale if not languages: - languages = self.session.query(Languages) \ - .join(books_languages_link) \ - .join(Books) \ - .filter(self.common_filters(return_all_languages=return_all_languages)) \ - .group_by(text('books_languages_link.lang_code')).all() - for lang in languages: - lang.name = isoLanguages.get_language_name(get_locale(), lang.lang_code) - return sorted(languages, key=lambda x: x.name, reverse=reverse_order) + if with_count: + languages = self.session.query(Languages, func.count('books_languages_link.book'))\ + .join(books_languages_link).join(Books)\ + .filter(self.common_filters(return_all_languages=return_all_languages)) \ + .group_by(text('books_languages_link.lang_code')).all() + for lang in languages: + lang[0].name = isoLanguages.get_language_name(get_locale(), lang[0].lang_code) + return sorted(languages, key=lambda x: x[0].name, reverse=reverse_order) + else: + languages = self.session.query(Languages) \ + .join(books_languages_link) \ + .join(Books) \ + .filter(self.common_filters(return_all_languages=return_all_languages)) \ + .group_by(text('books_languages_link.lang_code')).all() + for lang in languages: + lang.name = isoLanguages.get_language_name(get_locale(), lang.lang_code) + return sorted(languages, key=lambda x: x.name, reverse=reverse_order) def update_title_sort(self, config, conn=None): diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py index e220f63e..50447aca 100644 --- a/cps/isoLanguages.py +++ b/cps/isoLanguages.py @@ -57,6 +57,7 @@ def get_language_name(locale, lang_code): return get_language_names(locale)[lang_code] except KeyError: log.error('Missing translation for language name: {}'.format(lang_code)) + return "Unknown" def get_language_codes(locale, language_names, remainder=None): diff --git a/cps/templates/languages.html b/cps/templates/languages.html index 771d77cf..8331cb94 100644 --- a/cps/templates/languages.html +++ b/cps/templates/languages.html @@ -21,9 +21,9 @@
{% endif %} -
-
{{lang_counter[loop.index0].bookcount}}
- +
+
{{lang[1]}}
+
{% endfor %}
diff --git a/cps/web.py b/cps/web.py index 82f86488..2cb92a09 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1016,22 +1016,14 @@ def formats_list(): @login_required_if_no_ano def language_overview(): if current_user.check_visibility(constants.SIDEBAR_LANGUAGE) and current_user.filter_language() == u"all": - if current_user.get_view_property('language', 'dir') == 'desc': - order = db.Languages.lang_code.desc() - order_no = 0 - else: - order = db.Languages.lang_code.asc() - order_no = 1 + order_no = 0 if current_user.get_view_property('language', 'dir') == 'desc' else 1 charlist = list() - languages = calibre_db.speaking_language(reverse_order=not order_no) + languages = calibre_db.speaking_language(reverse_order=not order_no, with_count=True) for lang in languages: - upper_lang = lang.name[0].upper() + upper_lang = lang[0].name[0].upper() if upper_lang not in charlist: charlist.append(upper_lang) - lang_counter = calibre_db.session.query(db.books_languages_link, - func.count('books_languages_link.book').label('bookcount')).group_by( - text('books_languages_link.lang_code')).all() - return render_title_template('languages.html', languages=languages, lang_counter=lang_counter, + return render_title_template('languages.html', languages=languages, charlist=charlist, title=_(u"Languages"), page="langlist", data="language", order=order_no) else: