diff --git a/invenio_search_ui/ext.py b/invenio_search_ui/ext.py index 26bd9c20..a01e073c 100644 --- a/invenio_search_ui/ext.py +++ b/invenio_search_ui/ext.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2022 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -9,7 +10,6 @@ """UI for Invenio-Search.""" from . import config -from .views import blueprint, search class InvenioSearchUI(object): @@ -31,9 +31,6 @@ def init_app(self, app): self.init_config(app) app.extensions["invenio-search-ui"] = self - search_view = app.config.get("SEARCH_UI_SEARCH_VIEW", search) - blueprint.add_url_rule("/search", "search", view_func=search_view) - def init_config(self, app): """Initialize configuration. diff --git a/invenio_search_ui/views.py b/invenio_search_ui/views.py index 8ec218d6..c59487ad 100644 --- a/invenio_search_ui/views.py +++ b/invenio_search_ui/views.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2022 CERN. +# Copyright (C) 2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -12,12 +13,32 @@ from flask import Blueprint, current_app, json, render_template -blueprint = Blueprint( - "invenio_search_ui", - __name__, - template_folder="templates", - static_folder="static", -) + +def create_blueprint(app): + """Create blueprint.""" + blueprint = Blueprint( + "invenio_search_ui", + __name__, + template_folder="templates", + static_folder="static", + ) + + search_view = app.config.get("SEARCH_UI_SEARCH_VIEW", search) + blueprint.add_url_rule("/search", "search", view_func=search_view) + + blueprint.add_app_template_filter(format_sortoptions, name="format_sortoptions") + + @blueprint.app_context_processor + def search_app_helpers(): + """Makes Invenio-Search-JS config generation available for Jinja.""" + return {"search_app_helpers": current_app.config["SEARCH_UI_SEARCH_CONFIG_GEN"]} + + return blueprint + + +def format_sortoptions(sort_options): + """Create sort options JSON dump for Invenio-Search-JS.""" + return json.dumps({"options": sorted_options(sort_options)}) def search(): @@ -46,12 +67,6 @@ def sorted_options(sort_options): ] -@blueprint.app_template_filter("format_sortoptions") -def format_sortoptions(sort_options): - """Create sort options JSON dump for Invenio-Search-JS.""" - return json.dumps({"options": sorted_options(sort_options)}) - - class SearchAppInvenioRestConfigHelper(object): """Configuration generator for Invenio-Search-JS. @@ -247,9 +262,3 @@ def generate(cls, options, **kwargs): } config.update(kwargs) return config - - -@blueprint.app_context_processor -def search_app_helpers(): - """Makes Invenio-Search-JS config generation available for Jinja.""" - return {"search_app_helpers": current_app.config["SEARCH_UI_SEARCH_CONFIG_GEN"]} diff --git a/setup.cfg b/setup.cfg index 644fdd31..e1451a8e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -44,7 +44,7 @@ tests = invenio_base.apps = invenio_search_ui = invenio_search_ui:InvenioSearchUI invenio_base.blueprints = - invenio_search_ui = invenio_search_ui.views:blueprint + invenio_search_ui = invenio_search_ui.views:create_blueprint invenio_search_ui.translations = messages = invenio_search_ui invenio_assets.webpack = diff --git a/tests/conftest.py b/tests/conftest.py index a3dede39..f3ac20e1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2022 CERN. -# Copyright (C) 2023 Graz University of Technology. +# Copyright (C) 2023-2024 Graz University of Technology. # # Invenio is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. @@ -20,7 +20,7 @@ from invenio_i18n import Babel from invenio_search_ui import InvenioSearchUI -from invenio_search_ui.views import blueprint +from invenio_search_ui.views import create_blueprint @pytest.yield_fixture() @@ -49,7 +49,8 @@ def app(): def api(): return {} - app.register_blueprint(blueprint) + app.register_blueprint(create_blueprint(app)) + # add extra test templates to the search app blueprint, to fake the # existence of `invenio-theme` base templates. test_templates_path = os.path.join(os.path.dirname(__file__), "templates")