From 6cb713d62c2662b51765a6b52b98e7b193e02d92 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 21 Nov 2021 10:21:45 +0100 Subject: [PATCH] Added filtering of languages Bugfix show all allowed languages in user settings in case restrictions currently apply --- cps/admin.py | 2 +- cps/db.py | 15 ++++++++------- cps/templates/languages.html | 26 +++++++++++++++++++++----- cps/web.py | 31 +++++++++++++------------------ 4 files changed, 43 insertions(+), 31 deletions(-) diff --git a/cps/admin.py b/cps/admin.py index 068ac356..f104aa29 100644 --- a/cps/admin.py +++ b/cps/admin.py @@ -1575,7 +1575,7 @@ def edit_user(user_id): if not content or (not config.config_anonbrowse and content.name == "Guest"): flash(_(u"User not found"), category="error") return redirect(url_for('admin.admin')) - languages = calibre_db.speaking_language() + languages = calibre_db.speaking_language(return_all_languages=True) translations = babel.list_translations() + [LC('en')] kobo_support = feature_support['kobo'] and config.config_kobo_sync if request.method == "POST": diff --git a/cps/db.py b/cps/db.py index 800fb3dd..4e76072d 100644 --- a/cps/db.py +++ b/cps/db.py @@ -612,7 +612,7 @@ class CalibreDB(): return self.session.query(Data).filter(Data.book == book_id).filter(Data.format == file_format).first() # Language and content filters for displaying in the UI - def common_filters(self, allow_show_archived=False): + def common_filters(self, allow_show_archived=False, return_all_languages=False): if not allow_show_archived: archived_books = ( ub.session.query(ub.ArchivedBook) @@ -625,10 +625,10 @@ class CalibreDB(): else: archived_filter = true() - if current_user.filter_language() != "all": - lang_filter = Books.languages.any(Languages.lang_code == current_user.filter_language()) - else: + if current_user.filter_language() == "all" or return_all_languages: lang_filter = true() + else: + lang_filter = Books.languages.any(Languages.lang_code == current_user.filter_language()) negtags_list = current_user.list_denied_tags() postags_list = current_user.list_allowed_tags() neg_content_tags_filter = false() if negtags_list == [''] else Books.tags.any(Tags.name.in_(negtags_list)) @@ -796,18 +796,19 @@ 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): + def speaking_language(self, languages=None, return_all_languages=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()) \ + .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 languages + return sorted(languages, key=lambda x: x.name, reverse=reverse_order) + def update_title_sort(self, config, conn=None): # user defined sort function for calibre databases (Series, etc.) diff --git a/cps/templates/languages.html b/cps/templates/languages.html index 2b482e19..771d77cf 100644 --- a/cps/templates/languages.html +++ b/cps/templates/languages.html @@ -1,19 +1,35 @@ {% extends "layout.html" %} {% block body %}

{{title}}

+
-
+
{% for lang in languages %} {% if loop.index0 == (loop.length/2)|int and loop.length > 20 %} -
-
+
+
{% endif %} -
+
{{lang_counter[loop.index0].bookcount}}
{% endfor %} -
+
{% endblock %} +{% block js %} + +{% endblock %} diff --git a/cps/web.py b/cps/web.py index ea6faf19..cff0036e 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1012,30 +1012,25 @@ def formats_list(): @web.route("/language") @login_required_if_no_ano def language_overview(): - if current_user.check_visibility(constants.SIDEBAR_LANGUAGE): - charlist = list() - if current_user.filter_language() == u"all": - languages = calibre_db.speaking_language() - # ToDo: generate first character list for languages + 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: - #try: - # cur_l = LC.parse(current_user.filter_language()) - #except UnknownLocaleError: - # cur_l = None - - languages = calibre_db.session.query(db.Languages).filter( - db.Languages.lang_code == current_user.filter_language()).all() - languages[0].name = isoLanguages.get_language_name(get_locale(), languages[0].name.lang_code) - #if cur_l: - # languages[0].name = cur_l.get_language_name(get_locale()) - #else: - # languages[0].name = _(isoLanguages.get(part3=languages[0].lang_code).name) + order = db.Languages.lang_code.asc() + order_no = 1 + charlist = list() + languages = calibre_db.speaking_language(reverse_order=not order_no) + for lang in languages: + upper_lang = lang.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, charlist=charlist, title=_(u"Languages"), page="langlist", - data="language") + data="language", order=order_no) else: abort(404)