*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects

Create startup blueprint for post-init logic

Merged Moser, Maximilian requested to merge mm/updates into master
5 files
+ 68
30
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 18
28
@@ -7,23 +7,11 @@
@@ -7,23 +7,11 @@
"""Invenio module containing the theme for TU Wien."""
"""Invenio module containing the theme for TU Wien."""
from flask.config import Config
from . import config
from . import config
from .views import create_blueprint
from .views import create_blueprint
class TUWConfig(Config):
"""Override for the Flask config that evaluates the SITE_{API,UI}_URL proxies."""
def __getitem__(self, key):
value = super().__getitem__(key)
if key in ("SITE_UI_URL", "SITE_API_URL"):
value = str(value)
return value
class InvenioThemeTUW:
class InvenioThemeTUW:
"""Invenio-Theme-TUW extension."""
"""Invenio-Theme-TUW extension."""
@@ -35,32 +23,34 @@ class InvenioThemeTUW:
@@ -35,32 +23,34 @@ class InvenioThemeTUW:
def init_app(self, app):
def init_app(self, app):
"""Flask application initialization."""
"""Flask application initialization."""
self.init_config(app)
self.init_config(app)
 
self.init_blueprint(app)
app.extensions["invenio-theme-tuw"] = self
app.extensions["invenio-theme-tuw"] = self
blueprint = create_blueprint(app)
def init_blueprint(self, app):
 
"""Initialize blueprint."""
 
self.blueprint = blueprint = create_blueprint(app)
app.register_blueprint(blueprint)
app.register_blueprint(blueprint)
 
# since invenio-app-rdm currently (october 2022) doesn't offer an easy way of
 
# overriding the provided jinja templates, we have to perform a workaround:
 
# the first blueprint that has a definition for a template (per name) gets
 
# selected by 'flask.render_template()' - thus, we just have to make sure that
 
# our blueprint is inserted before the invenio_app_rdm blueprints
 
#
 
# this has to be done after everything (including *all* blueprints)
 
# has been initialized, and is only really relevant for the UI
 
# thus, we use `app.before_first_request` rather than the startup blueprint
 
#
 
# NOTE: the below code requires Flask 2.0.2+ and Python 3.7+
 
# for insertion-ordered dictionaries
@app.before_first_request
@app.before_first_request
def patch_config():
def register_tuw_bp_first():
# replace the app's config with our own override that evaluates
# the LocalProxy objects set for SITE_{API,UI}_URL by casting them
# into strings (which is their expected type)
app.config = TUWConfig(app.config.root_path, app.config)
@app.before_first_request
def register_bp_first():
# since invenio-app-rdm currently (april 2021) doesn't offer an easy way of
# overriding the provided jinja templates, we have to perform a workaround:
# the first blueprint that has a definition for a template (per name) gets
# selected by 'render_template'
# thus, we just have to make sure that our blueprint is inserted before the
# 'invenio_app_rdm' blueprints
# note: the below code requires Flask 2.0.2+ and Python 3.7+
bps = {
bps = {
name: bp
name: bp
for name, bp in app.blueprints.items()
for name, bp in app.blueprints.items()
if name != "invenio_theme_tuw"
if name != "invenio_theme_tuw"
}
}
 
app.blueprints = {"invenio_theme_tuw": blueprint, **bps}
app.blueprints = {"invenio_theme_tuw": blueprint, **bps}
def init_config(self, app):
def init_config(self, app):
Loading