Skip to content

Commit

Permalink
init: move record_once into finalize_app
Browse files Browse the repository at this point in the history
* finalize_app has to be called for the app creation for the tests too.
  entrypoints are not called there
  • Loading branch information
utnapischtim committed May 25, 2023
1 parent 1b7cd2e commit d00c7d9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 78 deletions.
54 changes: 54 additions & 0 deletions invenio_userprofiles/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

"""User profiles module for Invenio."""

from flask_menu import current_menu
from invenio_i18n import LazyString
from invenio_i18n import lazy_gettext as _
from invenio_theme.proxies import current_theme_icons

from . import config
from .api import current_userprofile
from .forms import confirm_register_form_factory, register_form_factory
from .views import blueprint


class InvenioUserProfiles(object):
Expand Down Expand Up @@ -64,3 +71,50 @@ def init_config(self, app):
app.config[
"SECURITY_REGISTER_USER_TEMPLATE"
] = "invenio_userprofiles/register_user.html"


def finalize_app(app):
"""Finalize app."""
init_menu(app)
init_ui(app)
init_api(app)


def init_menu(app):
"""Init menu."""
menu = app.extensions["menu"]
menu.submenu("settings.profile").register(
endpoint="invenio_userprofiles.profile",
text=_(
"%(icon)s Profile",
icon=LazyString(lambda: f'<i class="{current_theme_icons.user}"></i>'),
),
)

menu.submenu("breadcrumbs.settings.profile").register(
endpoint="invenio_userprofiles.profile",
text=_("Profile"),
)


def init_common(app):
"""Post initialization."""
if app.config["USERPROFILES_EXTEND_SECURITY_FORMS"]:
security_ext = app.extensions["security"]
security_ext.confirm_register_form = confirm_register_form_factory(
security_ext.confirm_register_form
)
security_ext.register_form = register_form_factory(security_ext.register_form)


def init_ui(app):
"""Post initialization for UI application."""
init_common(app)

# Register blueprint for templates
app.register_blueprint(blueprint, url_prefix=app.config["USERPROFILES_PROFILE_URL"])


def init_api(app):
"""Post initialization for API application."""
init_common(app)
42 changes: 0 additions & 42 deletions invenio_userprofiles/finalize_app.py

This file was deleted.

35 changes: 1 addition & 34 deletions invenio_userprofiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@
from invenio_db import db
from invenio_i18n import lazy_gettext as _

from .forms import (
EmailProfileForm,
PreferencesForm,
ProfileForm,
VerificationForm,
confirm_register_form_factory,
register_form_factory,
)
from .forms import EmailProfileForm, PreferencesForm, ProfileForm, VerificationForm
from .models import UserProfileProxy

blueprint = Blueprint(
Expand All @@ -53,32 +46,6 @@
)


def init_common(app):
"""Post initialization."""
if app.config["USERPROFILES_EXTEND_SECURITY_FORMS"]:
security_ext = app.extensions["security"]
security_ext.confirm_register_form = confirm_register_form_factory(
security_ext.confirm_register_form
)
security_ext.register_form = register_form_factory(security_ext.register_form)


@blueprint_ui_init.record_once
def init_ui(state):
"""Post initialization for UI application."""
app = state.app
init_common(app)

# Register blueprint for templates
app.register_blueprint(blueprint, url_prefix=app.config["USERPROFILES_PROFILE_URL"])


@blueprint_api_init.record_once
def init_api(state):
"""Post initialization for API application."""
init_common(state.app)


@blueprint.app_template_filter()
def userprofile(value):
"""Retrieve user profile for a given user id."""
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ invenio_db.alembic =
invenio_i18n.translations =
messages = invenio_userprofiles
invenio_base.finalize_app =
invenio_userprofiles = invenio_userprofiles.finalize_app:finalize_app
invenio_userprofiles = invenio_userprofiles.ext:finalize_app

[build_sphinx]
source-dir = docs/
Expand Down
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy_utils.functions import create_database, database_exists, drop_database

from invenio_userprofiles import InvenioUserProfiles
from invenio_userprofiles.ext import finalize_app
from invenio_userprofiles.views import blueprint_ui_init


Expand Down Expand Up @@ -106,7 +107,9 @@ def _init_userprofiles_app(app_):
@pytest.fixture
def app(base_app):
"""Flask application."""
return _init_userprofiles_app(base_app)
app_ = _init_userprofiles_app(base_app)
finalize_app(app_)
return app


@pytest.fixture
Expand Down

0 comments on commit d00c7d9

Please sign in to comment.