Skip to content

Commit

Permalink
refactor: blueprint creation to new style
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Dec 6, 2024
1 parent a1a09f2 commit 36b7b5b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
5 changes: 1 addition & 4 deletions invenio_search_ui/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
#
# 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.

"""UI for Invenio-Search."""

from . import config
from .views import blueprint, search


class InvenioSearchUI(object):
Expand All @@ -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.
Expand Down
45 changes: 27 additions & 18 deletions invenio_search_ui/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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():
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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"]}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 36b7b5b

Please sign in to comment.