diff --git a/invenio_oauthclient/ext.py b/invenio_oauthclient/ext.py index 8c54dca..291c811 100644 --- a/invenio_oauthclient/ext.py +++ b/invenio_oauthclient/ext.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2018 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. @@ -10,6 +11,7 @@ import warnings +from flask import request from flask_login import user_logged_out from flask_principal import identity_loaded @@ -205,3 +207,74 @@ def init_app(self, app): app.extensions["invenio-oauthclient"] = state return state + + +def finalize_app(app): + """Finalize app.""" + override_template_configuration(app) + init_index_menu(app) + post_ext_init(app) + + +def override_template_configuration(app): + """Override template configuration.""" + template_key = app.config.get( + "OAUTHCLIENT_TEMPLATE_KEY", + "SECURITY_LOGIN_USER_TEMPLATE", # default template key + ) + if template_key is not None: + template = app.config[template_key] # keep the old value + app.config["OAUTHCLIENT_LOGIN_USER_TEMPLATE_PARENT"] = template + app.config[template_key] = app.config.get( + "OAUTHCLIENT_LOGIN_USER_TEMPLATE", + "invenio_oauthclient/login_user.html", + ) + + +def init_index_menu(app): + """Init index menu.""" + menu = app.extensions["menu"] + + def active_when(): + return request.endpoint.startswith("invenio_oauthclient_settings.") + + def visible_when(): + return bool(app.config.get("OAUTHCLIENT_REMOTE_APPS")) is not False + + menu.submenu("settings.oauthclient").register( + "invenio_oauthclient_settings.index", + _( + "%(icon)s Linked accounts", + icon=make_lazy_string( + lambda: f'' + ), + ), + order=3, + active_when=active_when, + visible_when=visible_when, + ) + + menu.submenu("breadcrumbs.settings.oauthclient").register( + "invenio_oauthclient_settings.index", + _("Linked accounts"), + ) + + +def post_ext_init(app): + """Setup blueprint.""" + app.config.setdefault( + "OAUTHCLIENT_SITENAME", + app.config.get("THEME_SITENAME", "Invenio"), + ) + app.config.setdefault( + "OAUTHCLIENT_BASE_TEMPLATE", + app.config.get("BASE_TEMPLATE", "invenio_oauthclient/base.html"), + ) + app.config.setdefault( + "OAUTHCLIENT_COVER_TEMPLATE", + app.config.get("COVER_TEMPLATE", "invenio_oauthclient/base_cover.html"), + ) + app.config.setdefault( + "OAUTHCLIENT_SETTINGS_TEMPLATE", + app.config.get("SETTINGS_TEMPLATE", "invenio_oauthclient/settings/base.html"), + ) diff --git a/invenio_oauthclient/finalize_app.py b/invenio_oauthclient/finalize_app.py deleted file mode 100644 index e3d3c34..0000000 --- a/invenio_oauthclient/finalize_app.py +++ /dev/null @@ -1,30 +0,0 @@ -# -*- coding: utf-8 -*- -# -# This file is part of Invenio. -# 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. - -"""Finalize app.""" - - -def finalize_app(app): - """Finalize app.""" - - override_template_configuration(app) - - -def override_template_configuration(app): - """Override template configuration.""" - template_key = app.config.get( - "OAUTHCLIENT_TEMPLATE_KEY", - "SECURITY_LOGIN_USER_TEMPLATE", # default template key - ) - if template_key is not None: - template = app.config[template_key] # keep the old value - app.config["OAUTHCLIENT_LOGIN_USER_TEMPLATE_PARENT"] = template - app.config[template_key] = app.config.get( - "OAUTHCLIENT_LOGIN_USER_TEMPLATE", - "invenio_oauthclient/login_user.html", - ) diff --git a/invenio_oauthclient/views/client.py b/invenio_oauthclient/views/client.py index 23beae0..29c7007 100644 --- a/invenio_oauthclient/views/client.py +++ b/invenio_oauthclient/views/client.py @@ -39,28 +39,6 @@ ) -@blueprint.record_once -def post_ext_init(state): - """Setup blueprint.""" - app = state.app - - app.config.setdefault( - "OAUTHCLIENT_SITENAME", app.config.get("THEME_SITENAME", "Invenio") - ) - app.config.setdefault( - "OAUTHCLIENT_BASE_TEMPLATE", - app.config.get("BASE_TEMPLATE", "invenio_oauthclient/base.html"), - ) - app.config.setdefault( - "OAUTHCLIENT_COVER_TEMPLATE", - app.config.get("COVER_TEMPLATE", "invenio_oauthclient/base_cover.html"), - ) - app.config.setdefault( - "OAUTHCLIENT_SETTINGS_TEMPLATE", - app.config.get("SETTINGS_TEMPLATE", "invenio_oauthclient/settings/base.html"), - ) - - @blueprint.route("/login") def auto_redirect_login(*args, **kwargs): """Handles automatic redirect to external auth service. diff --git a/invenio_oauthclient/views/settings.py b/invenio_oauthclient/views/settings.py index f5ebae4..0968f02 100644 --- a/invenio_oauthclient/views/settings.py +++ b/invenio_oauthclient/views/settings.py @@ -2,6 +2,7 @@ # # This file is part of Invenio. # Copyright (C) 2015-2018 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. @@ -10,13 +11,8 @@ from operator import itemgetter -from flask import Blueprint, current_app, render_template, request -from flask_breadcrumbs import register_breadcrumb +from flask import Blueprint, current_app, render_template from flask_login import current_user, login_required -from flask_menu import register_menu -from invenio_i18n import lazy_gettext as _ -from invenio_theme.proxies import current_theme_icons -from speaklater import make_lazy_string from ..models import RemoteAccount from ..proxies import current_oauthclient @@ -32,21 +28,6 @@ @blueprint.route("/", methods=["GET", "POST"]) @login_required -@register_menu( - blueprint, - "settings.oauthclient", - _( - "%(icon)s Linked accounts", - icon=make_lazy_string(lambda: f''), - ), - order=3, - active_when=lambda: request.endpoint.startswith("invenio_oauthclient_settings."), - visible_when=lambda: bool(current_app.config.get("OAUTHCLIENT_REMOTE_APPS")) - is not False, -) -@register_breadcrumb( - blueprint, "breadcrumbs.settings.oauthclient", _("Linked accounts") -) def index(): """List linked accounts.""" oauth = current_oauthclient.oauth diff --git a/setup.cfg b/setup.cfg index e9759d1..c38f8f7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -86,7 +86,7 @@ invenio_base.secret_key = invenio_i18n.translations = invenio_oauthclient = invenio_oauthclient invenio_base.finalize_app = - invenio_oauthclient = invenio_oauthclient.finalize_app:finalize_app + invenio_oauthclient = invenio_oauthclient.ext:finalize_app [build_sphinx] source-dir = docs/