Skip to content

Commit

Permalink
fix: before_app_first_request deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
utnapischtim committed Mar 19, 2024
1 parent 88ca939 commit ca523ae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
32 changes: 30 additions & 2 deletions invenio_pages/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2022 CERN.
# Copyright (C) 2023 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.

"""Static pages module for Invenio."""

from flask import url_for
from flask import request, url_for
from jinja2.sandbox import SandboxedEnvironment
from werkzeug.exceptions import NotFound

from . import config
from .records.models import PageModel as Page
from .resources import PageResource, PageResourceConfig
from .services import PageService, PageServiceConfig
from .views import handle_not_found
from .views import add_url_rule, handle_not_found, render_page


class InvenioPages(object):
Expand Down Expand Up @@ -113,3 +115,29 @@ def init_resources(self, app):

class InvenioPagesREST(InvenioPages):
"""Invenio App ILS REST API app."""


def finalize_app(app):
"""Finalize app."""
register_pages(app)


def register_pages(app):
"""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
app.view_functions["invenio_pages.view"] = view

for page in Page.query.all():
add_url_rule(page.url)


def view():
"""Public interface to the page view.
Models: `pages.pages`.
Templates: Uses the template defined by the ``template_name`` field
or ``pages/default.html`` if template_name is not defined.
Context: page `pages.pages` object.
"""
return render_page(request.path) # pragma: no cover
65 changes: 19 additions & 46 deletions invenio_pages/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) 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 @@ -14,31 +15,14 @@
from sqlalchemy.orm.exc import NoResultFound
from werkzeug.exceptions import NotFound

from invenio_pages.proxies import current_pages_service

from .proxies import current_pages_service
from .records.models import PageModel as Page

blueprint = Blueprint(
"invenio_pages", __name__, url_prefix="/", template_folder="templates"
)


@blueprint.before_app_first_request
def register_pages():
"""Register URL rules for all static pages in the application.
This function iterates over all static pages stored in the database and registers
their URL rules with the application. Pages with custom views are skipped to allow
default handling for pages without customizations.
"""
# 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():
if not page.has_custom_view: # Skip registration of pages with custom view
_add_url_rule(page.url)


@blueprint.app_template_filter("render_string")
def render_string(source):
"""Render a string in sandboxed environment.
Expand All @@ -49,32 +33,6 @@ def render_string(source):
return current_app.extensions["invenio-pages"].render_template(source)


def view():
"""Public interface to the page view.
Models: `pages.pages`.
Templates: Uses the template defined by the ``template_name`` field
or ``pages/default.html`` if template_name is not defined.
Context: page `pages.pages` object.
"""
return render_page(request.path) # pragma: no cover


def render_page(path):
"""Internal interface to the page view.
:param path: Page path.
:returns: The rendered template.
"""
try:
page = current_pages_service.read_by_url(g.identity, request.path).to_dict()
except NoResultFound:
abort(404)
return render_template(
[page["template_name"], current_app.config["PAGES_DEFAULT_TEMPLATE"]], page=page
)


def handle_not_found(exception, **extra):
"""Custom blueprint exception handler."""
assert isinstance(exception, NotFound)
Expand All @@ -83,7 +41,7 @@ def handle_not_found(exception, **extra):
db.or_(Page.url == request.path, Page.url == request.path + "/")
).first()
if page:
_add_url_rule(page.url)
add_url_rule(page.url)
return render_template(
[page.template_name, current_app.config["PAGES_DEFAULT_TEMPLATE"]],
page=page,
Expand All @@ -94,7 +52,7 @@ def handle_not_found(exception, **extra):
return exception


def _add_url_rule(url):
def add_url_rule(url, app=None):
"""Register URL rule to application URL map."""
rule = current_app.url_rule_class(
url,
Expand All @@ -110,3 +68,18 @@ def create_pages_api_bp(app):
"""Create the pages resource api blueprint."""
ext = app.extensions["invenio-pages"]
return ext.pages_resource.as_blueprint()


def render_page(path):
"""Internal interface to the page view.
:param path: Page path.
:returns: The rendered template.
"""
try:
page = current_pages_service.read_by_url(g.identity, request.path).to_dict()
except NoResultFound:
abort(404)
return render_template(
[page["template_name"], current_app.config["PAGES_DEFAULT_TEMPLATE"]], page=page
)
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ invenio_administration.views =
invenio_pages_list = invenio_pages.administration.views.pages:PageListView
invenio_pages_details = invenio_pages.administration.views.pages:PageDetailView
invenio_pages_create = invenio_pages.administration.views.pages:PageEditView
invenio_base.finalize_app =
invenio_pages = invenio_pages.ext:finalize_app

[extract_messages]
copyright_holder = CERN
Expand Down

0 comments on commit ca523ae

Please sign in to comment.