From 2e67bd24074a65ad7d43ef502de28298b1a04b00 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sat, 10 Oct 2020 10:32:53 +0200 Subject: [PATCH] Fix add to shelf from search --- cps/__init__.py | 1 - cps/db.py | 8 ++++++-- cps/shelf.py | 8 ++++---- cps/ub.py | 10 +++++++++- cps/web.py | 13 +++---------- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cps/__init__.py b/cps/__init__.py index 28c2ebd4..03945b57 100644 --- a/cps/__init__.py +++ b/cps/__init__.py @@ -73,7 +73,6 @@ ub.init_db(cli.settingspath) # pylint: disable=no-member config = config_sql.load_configuration(ub.session) -searched_ids = {} web_server = WebServer() babel = Babel() diff --git a/cps/db.py b/cps/db.py index ca784665..b2ab257a 100644 --- a/cps/db.py +++ b/cps/db.py @@ -543,7 +543,7 @@ class CalibreDB(): bind=cls.engine)) for inst in cls.instances: inst.initSession() - + cls._init = True return True @@ -685,7 +685,11 @@ class CalibreDB(): else: offset = 0 limit_all = result_count - return result[offset:limit_all], result_count, pagination + + ub.store_ids(result) + + + 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): diff --git a/cps/shelf.py b/cps/shelf.py index 19a350d5..ea7f1eeb 100644 --- a/cps/shelf.py +++ b/cps/shelf.py @@ -29,7 +29,7 @@ from flask_login import login_required, current_user from sqlalchemy.sql.expression import func from sqlalchemy.exc import OperationalError, InvalidRequestError -from . import logger, ub, searched_ids, calibre_db +from . import logger, ub, calibre_db from .web import login_required_if_no_ano, render_title_template @@ -124,18 +124,18 @@ def search_to_shelf(shelf_id): flash(_(u"You are not allowed to add a book to the the shelf: %(name)s", name=shelf.name), category="error") return redirect(url_for('web.index')) - if current_user.id in searched_ids and searched_ids[current_user.id]: + if current_user.id in ub.searched_ids and ub.searched_ids[current_user.id]: books_for_shelf = list() books_in_shelf = ub.session.query(ub.BookShelf).filter(ub.BookShelf.shelf == shelf_id).all() if books_in_shelf: book_ids = list() for book_id in books_in_shelf: book_ids.append(book_id.book_id) - for searchid in searched_ids[current_user.id]: + for searchid in ub.searched_ids[current_user.id]: if searchid not in book_ids: books_for_shelf.append(searchid) else: - books_for_shelf = searched_ids[current_user.id] + books_for_shelf = ub.searched_ids[current_user.id] if not books_for_shelf: log.error("Books are already part of %s", shelf) diff --git a/cps/ub.py b/cps/ub.py index 06c65f38..145575d1 100644 --- a/cps/ub.py +++ b/cps/ub.py @@ -28,7 +28,7 @@ from binascii import hexlify from flask import g from flask_babel import gettext as _ -from flask_login import AnonymousUserMixin +from flask_login import AnonymousUserMixin, current_user from werkzeug.local import LocalProxy try: from flask_dance.consumer.backend.sqla import OAuthConsumerMixin @@ -54,6 +54,7 @@ from . import constants session = None app_DB_path = None Base = declarative_base() +searched_ids = {} def get_sidebar_config(kwargs=None): @@ -123,6 +124,13 @@ def get_sidebar_config(kwargs=None): return sidebar +def store_ids(result): + ids = list() + for element in result: + ids.append(element.id) + searched_ids[current_user.id] = ids + + class UserBase: @property diff --git a/cps/web.py b/cps/web.py index 61db9e92..44d011af 100644 --- a/cps/web.py +++ b/cps/web.py @@ -54,7 +54,7 @@ from werkzeug.datastructures import Headers from werkzeug.security import generate_password_hash, check_password_hash from . import constants, logger, isoLanguages, services -from . import searched_ids, lm, babel, db, ub, config, get_locale, app +from . import lm, babel, db, ub, config, get_locale, app from . import calibre_db from .gdriveutils import getFileFromEbooksFolder, do_gdrive_download from .helper import check_valid_domain, render_task_status, \ @@ -956,10 +956,6 @@ def render_prepare_search_form(cc): def render_search_results(term, offset=None, order=None, limit=None): entries, result_count, pagination = calibre_db.get_search_results(term, offset, order, limit) - ids = list() - for element in entries: - ids.append(element.id) - searched_ids[current_user.id] = ids return render_title_template('search.html', searchterm=term, pagination=pagination, @@ -1239,6 +1235,7 @@ def search(): title=_(u"Search"), page="search") + @web.route("/advanced_search", methods=['POST']) @login_required_if_no_ano def advanced_search(): @@ -1380,11 +1377,7 @@ def render_adv_search_results(term, offset=None, order=None, limit=None): func.lower(db.cc_classes[c.id].value).ilike("%" + custom_query + "%"))) q = q.order_by(*order).all() flask_session['query'] = json.dumps(term) - # ToDo: Check saved ids mechanism ? - ids = list() - for element in q: - ids.append(element.id) - searched_ids[current_user.id] = ids + 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: