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 @@