From 2814617e4bbc93a7627e1656762bd0e0eda7054c Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Fri, 16 Oct 2020 19:56:24 +0200 Subject: [PATCH 1/2] Fix for #1660 (Pagination in advanced search not working, error 500 for no filter in advanced search) Fix #1666 (Pagination in search not working) --- cps/web.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/cps/web.py b/cps/web.py index 8abae0a9..3c95a69f 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1231,7 +1231,9 @@ def reconnect(): def search(): term = request.args.get("query") if term: - return render_search_results(term, 0, None, config.config_books_per_page) + # flask_session['query'] = json.dumps(request.form) + return redirect(url_for('web.books_list', data="search", sort_param='stored', query=term)) + # return render_search_results(term, 0, None, config.config_books_per_page) else: return render_title_template('search.html', searchterm="", @@ -1243,8 +1245,9 @@ def search(): @web.route("/advanced_search", methods=['POST']) @login_required_if_no_ano def advanced_search(): - term = request.form - return render_adv_search_results(term, 0, None, config.config_books_per_page) + flask_session['query'] = json.dumps(request.form) + return redirect(url_for('web.books_list', data="advsearch", sort_param='stored', query="")) + def render_adv_search_results(term, offset=None, order=None, limit=None): order = order or [db.Books.sort] @@ -1379,18 +1382,18 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): else: q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any( func.lower(db.cc_classes[c.id].value).ilike("%" + custom_query + "%"))) - q = q.order_by(*order).all() - flask_session['query'] = json.dumps(term) - ub.store_ids(q) - # entries, result_count, pagination = calibre_db.get_search_results(term, offset, order, limit) - result_count = len(q) - if offset != None and limit != None: - offset = int(offset) - limit_all = offset + int(limit) - pagination = Pagination((offset / (int(limit)) + 1), limit, result_count) - else: - offset = 0 - limit_all = result_count + q = q.order_by(*order).all() + flask_session['query'] = json.dumps(term) + ub.store_ids(q) + # entries, result_count, pagination = calibre_db.get_search_results(term, offset, order, limit) + result_count = len(q) + if offset != None and limit != None: + offset = int(offset) + limit_all = offset + int(limit) + pagination = Pagination((offset / (int(limit)) + 1), limit, result_count) + else: + offset = 0 + limit_all = result_count return render_title_template('search.html', adv_searchterm=searchterm, pagination=pagination, From bc0416cbb474e70f47805382261a17d78ba66faa Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Fri, 16 Oct 2020 20:24:15 +0200 Subject: [PATCH 2/2] Fix for #1660 (advanced search for custom columns working, headline for advanced search working) --- cps/web.py | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/cps/web.py b/cps/web.py index 3c95a69f..aa1cac1a 100644 --- a/cps/web.py +++ b/cps/web.py @@ -1242,10 +1242,15 @@ def search(): page="search") -@web.route("/advanced_search", methods=['POST']) +@web.route("/advsearch", methods=['POST']) @login_required_if_no_ano def advanced_search(): - flask_session['query'] = json.dumps(request.form) + values = dict(request.form) + params = ['include_tag', 'exclude_tag', 'include_serie', 'exclude_serie', 'include_language', + 'exclude_language', 'include_extension', 'exclude_extension'] + for param in params: + values[param] = list(request.form.getlist(param)) + flask_session['query'] = json.dumps(values) return redirect(url_for('web.books_list', data="advsearch", sort_param='stored', query="")) @@ -1257,14 +1262,14 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): calibre_db.session.connection().connection.connection.create_function("lower", 1, db.lcase) q = calibre_db.session.query(db.Books).filter(calibre_db.common_filters(True)) - include_tag_inputs = request.form.getlist('include_tag') - exclude_tag_inputs = request.form.getlist('exclude_tag') - include_series_inputs = request.form.getlist('include_serie') - exclude_series_inputs = request.form.getlist('exclude_serie') - include_languages_inputs = request.form.getlist('include_language') - exclude_languages_inputs = request.form.getlist('exclude_language') - include_extension_inputs = request.form.getlist('include_extension') - exclude_extension_inputs = request.form.getlist('exclude_extension') + include_tag_inputs = term.get('include_tag') + exclude_tag_inputs = term.get('exclude_tag') + include_series_inputs = term.get('include_serie') + exclude_series_inputs = term.get('exclude_serie') + include_languages_inputs = term.get('include_language') + exclude_languages_inputs = term.get('exclude_language') + include_extension_inputs = term.get('include_extension') + exclude_extension_inputs = term.get('exclude_extension') author_name = term.get("author_name") book_title = term.get("book_title") @@ -1284,15 +1289,14 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): searchterm = [] cc_present = False for c in cc: - if request.form.get('custom_column_' + str(c.id)): - searchterm.extend([(u"%s: %s" % (c.name, request.form.get('custom_column_' + str(c.id))))]) + if term.get('custom_column_' + str(c.id)): + searchterm.extend([(u"%s: %s" % (c.name, term.get('custom_column_' + str(c.id))))]) cc_present = True if include_tag_inputs or exclude_tag_inputs or include_series_inputs or exclude_series_inputs or \ - include_languages_inputs or exclude_languages_inputs or author_name or book_title or \ - publisher or pub_start or pub_end or rating_low or rating_high or description or cc_present or \ - include_extension_inputs or exclude_extension_inputs: - searchterm = [] + include_languages_inputs or exclude_languages_inputs or author_name or book_title or \ + publisher or pub_start or pub_end or rating_low or rating_high or description or cc_present or \ + include_extension_inputs or exclude_extension_inputs: searchterm.extend((author_name.replace('|', ','), book_title, publisher)) if pub_start: try: @@ -1312,7 +1316,8 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): searchterm.extend(tag.name for tag in tag_names) serie_names = calibre_db.session.query(db.Series).filter(db.Series.id.in_(include_series_inputs)).all() searchterm.extend(serie.name for serie in serie_names) - language_names = calibre_db.session.query(db.Languages).filter(db.Languages.id.in_(include_languages_inputs)).all() + language_names = calibre_db.session.query(db.Languages).\ + filter(db.Languages.id.in_(include_languages_inputs)).all() if language_names: language_names = calibre_db.speaking_language(language_names) searchterm.extend(language.name for language in language_names) @@ -1323,9 +1328,9 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): searchterm.extend(ext for ext in include_extension_inputs) searchterm.extend(ext for ext in exclude_extension_inputs) # handle custom columns - for c in cc: - if request.form.get('custom_column_' + str(c.id)): - searchterm.extend([(u"%s: %s" % (c.name, request.form.get('custom_column_' + str(c.id))))]) + #for c in cc: + # if term.get('custom_column_' + str(c.id)): + # searchterm.extend([(u"%s: %s" % (c.name, term.get('custom_column_' + str(c.id))))]) searchterm = " + ".join(filter(None, searchterm)) q = q.filter() if author_name: @@ -1368,7 +1373,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): # search custom culumns for c in cc: - custom_query = request.form.get('custom_column_' + str(c.id)) + custom_query = term.get('custom_column_' + str(c.id)) if custom_query != '' and custom_query is not None: if c.datatype == 'bool': q = q.filter(getattr(db.Books, 'custom_column_' + str(c.id)).any( @@ -1403,7 +1408,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): -@web.route("/advanced_search", methods=['GET']) +@web.route("/advsearch", methods=['GET']) @login_required_if_no_ano def advanced_search_form(): # Build custom columns names