Skip to content

Commit

Permalink
views: add register urls dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Jan 12, 2023
1 parent 445c962 commit 628595c
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions invenio_pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

0 comments on commit 628595c

Please sign in to comment.