From 628595c0cb9f7ac57706147ac9ac0e7832faa802 Mon Sep 17 00:00:00 2001 From: Javier Romero Castro Date: Thu, 12 Jan 2023 15:31:33 +0100 Subject: [PATCH] views: add register urls dynamically * closes https://github.com/inveniosoftware/invenio-pages/issues/58 --- invenio_pages/views.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/invenio_pages/views.py b/invenio_pages/views.py index fdd808c..29c6e83 100644 --- a/invenio_pages/views.py +++ b/invenio_pages/views.py @@ -37,6 +37,16 @@ ) +@blueprint.before_app_first_request +def register_pages(): + """Register URL rule of all static pages to the application.""" + # We need to set the function view, to be able to directly register the urls in the Flask.url_map + current_app.view_functions["invenio_pages.view"] = view + + for page in Page.query.all(): + _add_url_rule(page.url) + + @blueprint.app_template_filter("render_string") def render_string(source): """Render a string in sandboxed environment. @@ -93,11 +103,13 @@ def handle_not_found(exception, **extra): return exception -def _add_url_rule(url_or_urls): +def _add_url_rule(url): """Register URL rule to application URL map.""" - if isinstance(url_or_urls, str): - url_or_urls = [url_or_urls] - map( - lambda url: current_app.add_url_rule(url, "invenio_pages.view", view), - url_or_urls, + rule = current_app.url_rule_class( + url, + endpoint="invenio_pages.view", + methods=["GET", "HEAD", "OPTIONS"], + strict_slashes=True, + merge_slashes=True, ) + current_app.url_map.add(rule)