From e720601701384cb2092d892dfd9cb6039ef92fcd Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Sat, 16 Dec 2023 11:17:02 +0100 Subject: [PATCH] Unified edit template params --- oarepo_ui/resources/components.py | 46 ++++++++++++++++--------------- oarepo_ui/resources/resource.py | 23 ++++++++++------ tests/test_ui_resource_config.py | 1 + 3 files changed, 40 insertions(+), 30 deletions(-) diff --git a/oarepo_ui/resources/components.py b/oarepo_ui/resources/components.py index c679c352..08f091d3 100644 --- a/oarepo_ui/resources/components.py +++ b/oarepo_ui/resources/components.py @@ -22,9 +22,9 @@ class UIResourceComponent: the resource configuration. Naming convention for parameters: - * record - the record being displayed, always is an instance of RecordItem + * api_record - the record being displayed, always is an instance of RecordItem + * record - UI serialization of the record as comes from the ui serializer. A dictionary * data - data serialized by the API service serializer. A dictionary - * ui_data - UI serialization of the record as comes from the ui serializer. A dictionary * empty_data - empty record data, compatible with the API service serializer. A dictionary """ @@ -57,8 +57,8 @@ def fill_jinja_context(self, *, context: Dict, **kwargs): def before_ui_detail( self, *, - record: RecordItem, - ui_data: Dict, + api_record: RecordItem, + record: Dict, identity: Identity, args: Dict, view_args: Dict, @@ -69,8 +69,8 @@ def before_ui_detail( """ Called before the detail page is rendered. - :param record: the record being displayed - :param ui_data: UI serialization of the record + :param api_record: the record being displayed + :param record: UI serialization of the record :param identity: the current user identity :param args: query parameters :param view_args: view arguments @@ -100,7 +100,8 @@ def before_ui_search(self, *, """ def form_config(self, *, - record: RecordItem, + api_record: RecordItem, + record: Dict, data: Dict, identity: Identity, form_config: Dict, @@ -112,7 +113,8 @@ def form_config(self, *, """ Called to fill form_config for the create/edit page. - :param record: the record being edited. Can be None if creating a new record. + :param api_record: the record being edited. Can be None if creating a new record. + :param record: UI serialization of the record :param data: data serialized by the API service serializer. If a record is being edited, this is the serialized record data. If a new record is being created, this is empty_data after being processed by the empty_record method on registered UI components. @@ -125,9 +127,9 @@ def form_config(self, *, """ def before_ui_edit(self, *, - record: RecordItem, + api_record: RecordItem, + record: Dict, data: Dict, - ui_data: Dict, identity: Identity, form_config: Dict, args: Dict, @@ -138,9 +140,9 @@ def before_ui_edit(self, *, """ Called before the edit page is rendered, after form_config has been filled. - :param record: the record being edited + :param api_record: the API record being edited :param data: data serialized by the API service serializer. This is the serialized record data. - :param ui_data: UI serialization of the record (localized). The ui data can be used in the edit + :param record: UI serialization of the record (localized). The ui data can be used in the edit template to display, for example, the localized record title. :param identity: the current user identity :param form_config: form configuration dictionary @@ -192,11 +194,11 @@ def form_config( class PermissionsComponent(UIResourceComponent): - def before_ui_detail(self, *, record, extra_context, identity, **kwargs): - self.fill_permissions(record.data, extra_context, identity) + def before_ui_detail(self, *, api_record, extra_context, identity, **kwargs): + self.fill_permissions(api_record.data, extra_context, identity) - def before_ui_edit(self, *, record, extra_context, identity, **kwargs): - self.fill_permissions(record.data, extra_context, identity) + def before_ui_edit(self, *, api_record, extra_context, identity, **kwargs): + self.fill_permissions(api_record.data, extra_context, identity) def before_ui_create(self, *, extra_context, identity, **kwargs): self.fill_permissions(None, extra_context, identity) @@ -215,14 +217,14 @@ def before_ui_search( search_options["permissions"] = extra_context["permissions"] def form_config( - self, *, form_config, record, identity, **kwargs + self, *, form_config, api_record, identity, **kwargs ): - self.fill_permissions(record, form_config, identity) + self.fill_permissions(api_record.data if api_record else None, form_config, identity) def get_record_permissions(self, actions, service, identity, data): """Helper for generating (default) record action permissions.""" return { - f"can_{action}": service.check_permission(identity, action, record=data) + f"can_{action}": service.check_permission(identity, action, record=data or {}) for action in actions } @@ -240,15 +242,15 @@ def fill_permissions(self, data, extra_context, identity): class FilesComponent(UIResourceComponent): - def before_ui_edit(self, *, record, extra_context, identity, **kwargs): + def before_ui_edit(self, *, api_record, extra_context, identity, **kwargs): from .resource import RecordsUIResource if not isinstance(self.resource, RecordsUIResource): return file_service = get_file_service_for_record_service( - self.resource.api_service, record=record + self.resource.api_service, record=api_record ) - files = file_service.list_files(identity, record["id"]) + files = file_service.list_files(identity, api_record["id"]) extra_context["files"] = files.to_dict() def before_ui_detail(self, **kwargs): diff --git a/oarepo_ui/resources/resource.py b/oarepo_ui/resources/resource.py index 0fe808c8..95cec12c 100644 --- a/oarepo_ui/resources/resource.py +++ b/oarepo_ui/resources/resource.py @@ -153,8 +153,8 @@ def detail(self): self.run_components( "before_ui_detail", - record=record, - ui_data=ui_data, + api_record=record, + record=ui_data, identity=g.identity, extra_context=extra_context, args=resource_requestctx.args, @@ -167,6 +167,7 @@ def detail(self): metadata=metadata, ui=dict(ui_data.get("ui", ui_data)), record=ui_data, + api_record=record, extra_context=extra_context, ui_links=ui_links, ) @@ -282,7 +283,7 @@ def get_template_def(self, template_type): def edit(self): record = self._get_record(resource_requestctx, allow_draft=True) data = record.to_dict() - ui_record = self.config.ui_serializer.dump_obj(record.to_dict()) + ui_data = self.config.ui_serializer.dump_obj(record.to_dict()) form_config = self.config.form_config( identity=g.identity, updateUrl=record.links.get("self", None) ) @@ -293,8 +294,9 @@ def edit(self): self.run_components( "form_config", - record=record, + api_record=record, data=data, + ui_data=ui_data, identity=g.identity, form_config=form_config, args=resource_requestctx.args, @@ -304,8 +306,8 @@ def edit(self): ) self.run_components( "before_ui_edit", - resource=self, - record=ui_record, + api_record=record, + ui_data=ui_data, data=data, form_config=form_config, args=resource_requestctx.args, @@ -315,14 +317,15 @@ def edit(self): extra_context=extra_context, ) - ui_record["extra_links"] = { + ui_data["extra_links"] = { "ui_links": ui_links, "search_link": self.config.url_prefix, } return current_oarepo_ui.catalog.render( self.get_template_def("edit"), - record=ui_record, + record=ui_data, + api_record=record, form_config=form_config, extra_context=extra_context, ui_links=ui_links, @@ -343,6 +346,7 @@ def create(self): self.run_components( "form_config", + api_record=None, record=None, data=empty_record, form_config=form_config, @@ -354,6 +358,8 @@ def create(self): self.run_components( "before_ui_create", data=empty_record, + record=None, + api_record=None, form_config=form_config, args=resource_requestctx.args, view_args=resource_requestctx.view_args, @@ -364,6 +370,7 @@ def create(self): return current_oarepo_ui.catalog.render( self.get_template_def("create"), record=empty_record, + api_record=None, form_config=form_config, extra_context=extra_context, ui_links={}, diff --git a/tests/test_ui_resource_config.py b/tests/test_ui_resource_config.py index 12bc907d..400237d3 100644 --- a/tests/test_ui_resource_config.py +++ b/tests/test_ui_resource_config.py @@ -12,6 +12,7 @@ def test_ui_resource_form_config(app, record_ui_resource): form_config=fc, layout="", resource=record_ui_resource, + api_record=None, record={}, data={}, args={},