From 5ed68c0fc858003fc14834b1fb7ef1c67e9035ec Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Sun, 14 May 2023 00:02:00 +0200 Subject: [PATCH 1/5] fix: before_first_request deprecation * the deprecation warning has been fixed by using the new entry_point invenio_base.finalize_app --- invenio_administration/admin.py | 18 ++++++++---------- invenio_administration/ext.py | 26 +++++++++++++++++--------- invenio_administration/views/base.py | 1 + setup.cfg | 4 +++- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/invenio_administration/admin.py b/invenio_administration/admin.py index 89dab0e..f81c29c 100644 --- a/invenio_administration/admin.py +++ b/invenio_administration/admin.py @@ -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. @@ -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.""" @@ -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 ): diff --git a/invenio_administration/ext.py b/invenio_administration/ext.py index f8561e3..2562597 100644 --- a/invenio_administration/ext.py +++ b/invenio_administration/ext.py @@ -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. @@ -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, @@ -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): @@ -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() diff --git a/invenio_administration/views/base.py b/invenio_administration/views/base.py index e1bcd49..33e69f3 100644 --- a/invenio_administration/views/base.py +++ b/invenio_administration/views/base.py @@ -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. diff --git a/setup.cfg b/setup.cfg index 33acede..75647c8 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 @@ -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/ From bdb28bd0171b2d7ea54d188c93520c218cf291e2 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Wed, 20 Mar 2024 13:59:29 +0100 Subject: [PATCH 2/5] fix: import missing --- invenio_administration/admin.py | 2 +- invenio_administration/views/base.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/invenio_administration/admin.py b/invenio_administration/admin.py index f81c29c..d5109a2 100644 --- a/invenio_administration/admin.py +++ b/invenio_administration/admin.py @@ -15,7 +15,7 @@ from invenio_administration.menu import AdminMenu -from .views.base import AdminResourceListView, AdminView +from .views.base import AdminFormView, AdminResourceDetailView, AdminView class Administration: diff --git a/invenio_administration/views/base.py b/invenio_administration/views/base.py index 33e69f3..f4a518b 100644 --- a/invenio_administration/views/base.py +++ b/invenio_administration/views/base.py @@ -282,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): From 505e5735c519050f202b842567dd3cf33881bd6f Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Thu, 21 Mar 2024 11:46:10 +0100 Subject: [PATCH 3/5] fix: tests not working since finalize_app * the problem was that the mock fixtures were called after the app creation. this does not work because in the finalize_app they have to be ready already. therefore they are now called as a entry point before of the finalize_app --- tests/conftest.py | 6 +++- tests/mock_module/administration/mock.py | 6 ++-- tests/mock_module/ext.py | 35 ++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 tests/mock_module/ext.py diff --git a/tests/conftest.py b/tests/conftest.py index 91a7ffd..b16563e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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 @@ -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", + ], } diff --git a/tests/mock_module/administration/mock.py b/tests/mock_module/administration/mock.py index 451d396..1ca82b9 100644 --- a/tests/mock_module/administration/mock.py +++ b/tests/mock_module/administration/mock.py @@ -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): diff --git a/tests/mock_module/ext.py b/tests/mock_module/ext.py new file mode 100644 index 0000000..d75b5da --- /dev/null +++ b/tests/mock_module/ext.py @@ -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 From e30eb318a756af87e0bec882dab8a667ad1b8f7e Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Thu, 21 Mar 2024 17:03:59 +0100 Subject: [PATCH 4/5] setup: bump dependencies * invenio-accounts, invenio-theme, invenio-base, invenio-app --- setup.cfg | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 75647c8..4437fd5 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,13 +30,13 @@ 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 = @@ -44,7 +44,7 @@ tests = 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 = From 7e93bae4eb9b3fc52154e641b7de544b5f45d7f4 Mon Sep 17 00:00:00 2001 From: Christoph Ladurner Date: Fri, 22 Mar 2024 10:37:02 +0100 Subject: [PATCH 5/5] fix: used the wrong class --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index b16563e..320a0ea 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -171,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