Skip to content

Commit

Permalink
Unified edit template params
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed Dec 16, 2023
1 parent 54f2e29 commit e720601
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
46 changes: 24 additions & 22 deletions oarepo_ui/resources/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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):
Expand Down
23 changes: 15 additions & 8 deletions oarepo_ui/resources/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
)
Expand Down Expand Up @@ -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)
)
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -343,6 +346,7 @@ def create(self):

self.run_components(
"form_config",
api_record=None,
record=None,
data=empty_record,
form_config=form_config,
Expand All @@ -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,
Expand All @@ -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={},
Expand Down
1 change: 1 addition & 0 deletions tests/test_ui_resource_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={},
Expand Down

0 comments on commit e720601

Please sign in to comment.