Skip to content

Commit

Permalink
fix: before_first_request deprecation
Browse files Browse the repository at this point in the history
* the deprecation warning has been fixed by using the new entry_point
  invenio_base.finalize_app
  • Loading branch information
utnapischtim committed May 17, 2023
1 parent 4451047 commit 13b3468
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
22 changes: 11 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 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 @@ -14,7 +15,7 @@

from invenio_administration.menu import AdminMenu

from .views.base import 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,13 +108,9 @@ def add_view(self, view, view_instance, *args, **kwargs):
view_func=view,
)

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

if not isinstance(view_instance, AdminResourceDetailView) and not isinstance(
view_instance, AdminFormView):
view_instance, AdminFormView
):
self._menu.add_view_to_menu(view_instance)

def _add_dashboard_view(self):
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()
3 changes: 1 addition & 2 deletions invenio_administration/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ def get_context(self, pid_value=None):
serialized_schema = self._schema_to_json(schema)
fields = self.item_field_list
return {
"request_headers"
"name": name,
"request_headers" "name": name,
"resource_schema": serialized_schema,
"fields": fields,
"exclude_fields": self.item_field_exclude_list,
Expand Down
4 changes: 3 additions & 1 deletion 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-2018 CERN.
# Copyright (C) 2022 Graz University of Technology.
# Copyright (C) 2022-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 Down Expand Up @@ -60,6 +60,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

0 comments on commit 13b3468

Please sign in to comment.