Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: before_first_request deprecation #171

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions invenio_administration/admin.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) 2022-2024 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,7 +15,7 @@

from invenio_administration.menu import AdminMenu

from .views.base import AdminResourceListView, AdminView
from .views.base import AdminFormView, AdminResourceDetailView, AdminView


class Administration:
Expand Down Expand Up @@ -62,10 +63,13 @@ def __init__(
if self.dashboard_view_class is not None:
self._add_dashboard_view()

@app.before_first_request
def init_menu():
self._menu.register_menu_entries(current_menu, self._menu_key)
self._menu.register_admin_entry(current_menu, self.endpoint)
def init_menu(self):
"""Finalize app.

This method will be called within the finalize_app entrypoint.
"""
self._menu.register_menu_entries(current_menu, self._menu_key)
self._menu.register_admin_entry(current_menu, self.endpoint)

def load_admin_dashboard(self, app):
"""Load dashboard view configuration."""
Expand Down Expand Up @@ -104,12 +108,6 @@ def add_view(self, view, view_instance, *args, **kwargs):
view_func=view,
)

from invenio_administration.views.base import (
AdminFormView,
AdminResourceDetailView,
)

# TODO change to be configurable
if not isinstance(view_instance, AdminResourceDetailView) and not isinstance(
view_instance, AdminFormView
):
Expand Down
26 changes: 17 additions & 9 deletions invenio_administration/ext.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) 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.
Expand All @@ -28,6 +29,7 @@ def __init__(self, app=None, entry_point_group="invenio_administration.views"):

def init_app(self, app):
"""Initialize application."""
self.view_classes = []
self.init_config(app)
self.administration = Administration(
app,
Expand Down Expand Up @@ -82,16 +84,9 @@ def register_view(self, view_class, extension_name, app, *args, **kwargs):
if issubclass(view_class, AdminResourceBaseView):
self.register_resource(app, view_class, extension_name)

@staticmethod
def register_resource(app, view_class, extension_name):
def register_resource(self, app, view_class, extension_name):
"""Set views schema."""

@app.before_first_request
def register_view_resource():
if view_class.resource_config:
view_class.set_resource(extension_name=extension_name)
if view_class.schema:
view_class.set_schema(extension_name=extension_name)
self.view_classes.append((view_class, extension_name))

@staticmethod
def _extract_extension_name(entrypoint_path):
Expand All @@ -109,3 +104,16 @@ def init_config(app):
for k in dir(config):
if k.startswith("ADMINISTRATION_"):
app.config.setdefault(k, getattr(config, k))


def finalize_app(app):
"""Finalize the app."""
view_classes = app.extensions["invenio-administration"].view_classes

for view_class, extension_name in view_classes:
if view_class.resource_config:
view_class.set_resource(extension_name=extension_name)
if view_class.schema:
view_class.set_schema(extension_name=extension_name)

app.extensions["invenio-administration"].administration.init_menu()
2 changes: 1 addition & 1 deletion invenio_administration/views/base.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) 2022-2024 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.
Expand Down Expand Up @@ -281,7 +282,6 @@ def get_context(self, pid_value=None):
"resource_name": (
self.resource_name if self.resource_name else self.pid_path
),
"request_headers": self.request_headers,
}

def get(self, pid_value=None):
Expand Down
12 changes: 7 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This file is part of Invenio.
# Copyright (C) 2015-2024 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-2024 Graz University of Technology.
# Copyright (C) 2023 KTH Royal Institute of Technology.
#
# Invenio is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -30,21 +30,21 @@ zip_safe = False
install_requires =
Flask-Menu>=0.5.0
Flask-Principal>=0.4.0
invenio-accounts>=4.0.0,<5.0.0
invenio-base>=1.2.9,<2.0.0
invenio-accounts>=5.0.0,<6.0.0
invenio-base>=1.3.0,<2.0.0
invenio-db[postgresql,mysql]>=1.0.9,<2.0.0
invenio-vocabularies>=3.0.0,<4.0.0
invenio-records-resources>=5.0.0,<6.0.0
invenio-search-ui>=2.1.2,<3.0.0
invenio-theme>=2.0.0,<3.0.0
invenio-theme>=3.0.0,<4.0.0

[options.extras_require]
tests =
pytest-black-ng>=0.4.0
pytest-invenio>=2.1.0,<3.0.0
sphinx>=4.5
invenio-access>=2.0.0,<3.0.0
invenio-app>=1.3.4,<2.0.0
invenio-app>=1.4.0,<2.0.0
elasticsearch7 =
invenio-search[elasticsearch7]>=2.1.0,<3.0.0
opensearch1 =
Expand All @@ -61,6 +61,8 @@ invenio_assets.webpack =
invenio_administration = invenio_administration.webpack:theme
invenio_i18n.translations =
messages = invenio_administration
invenio_base.finalize_app =
finalize_app = invenio_administration.ext:finalize_app

[build_sphinx]
source-dir = docs/
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2022 CERN.
# Copyright (C) 2024 Graz University of Technology.
#
# invenio-administration is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see LICENSE file for more
Expand Down Expand Up @@ -43,7 +44,10 @@ def extra_entry_points():
"invenio_administration.views": [
"mock_module = mock_module.administration.mock:MockView",
"mock_module = mock_module.administration.mock:MockViewAlternate",
]
],
"invenio_base.apps": [
"test = mock_module.ext:MockExtension",
],
}


Expand Down Expand Up @@ -167,7 +171,7 @@ def client_with_login(client, admin):
@pytest.fixture
def current_admin_menu(test_app):
"""Current Admin flask menu instance."""
return test_app.extensions["menu"]
return test_app.extensions["menu"].root_node


@pytest.fixture
Expand Down
6 changes: 2 additions & 4 deletions tests/mock_module/administration/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#
# This file is part of Invenio.
# Copyright (C) 2022 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.
"""Mock view for testing."""

from invenio_administration.views.base import (
AdminResourceDetailView,
AdminResourceListView,
)
from invenio_administration.views.base import AdminResourceListView


class MockView(AdminResourceListView):
Expand Down
35 changes: 35 additions & 0 deletions tests/mock_module/ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
#
# This file is part of Invenio.
# Copyright (C) 2022 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.

"""Mock service ext."""

from invenio_records_resources.resources import RecordResource
from invenio_records_resources.services import RecordService

from .administration.mock import MockView
from .config import ServiceConfig
from .resource import MockResource


class MockExtension:
"""Mock extension."""

def __init__(self, app=None):
"""Extension initialization."""
if app:
self.init_app(app)

def init_app(self, app):
"""Init app."""
mock_service = RecordService(ServiceConfig)
mock_resource = RecordResource(MockResource, mock_service)

app.extensions["mock-module"] = self
# Adds resource's config name as a class attribute.
self.__dict__[MockView.resource_config] = mock_resource
Loading