Skip to content

Commit

Permalink
making ui components api consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed Dec 16, 2023
1 parent 6f9931b commit 2cd2b8e
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 143 deletions.
6 changes: 0 additions & 6 deletions oarepo_requests/components/__init__.py

This file was deleted.

130 changes: 0 additions & 130 deletions oarepo_requests/components/requests.py

This file was deleted.

Empty file.
56 changes: 56 additions & 0 deletions oarepo_requests/resources/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import copy

from invenio_records_resources.services.errors import PermissionDeniedError
from invenio_requests import current_requests_service, current_request_type_registry
from invenio_requests.customizations import RequestActions


class AllowedRequestsComponent(UIResourceComponent):
"""Service component which sets all data in the record."""

def _add_available_requests(self, identity, record, dict_to_save_result, kwargs):
# todo discriminate requests from other stuff which can be on parent in the future
# todo what to throw if parent doesn't exist

if not record:
return

available_requests = {}
kwargs[dict_to_save_result]["allowed_requests"] = available_requests

parent_copy = copy.deepcopy(record.data["parent"])
requests = {
k: v
for k, v in parent_copy.items()
if isinstance(v, dict) and "receiver" in v
} # todo more sensible request identification

for request_name, request_dict in requests.items():
request = current_requests_service.record_cls.get_record(request_dict["id"])
request_type = current_request_type_registry.lookup(request_dict["type"])
for action_name, action in request_type.available_actions.items():
try:
current_requests_service.require_permission(
identity, f"action_{action_name}", request=request
)
except PermissionDeniedError:
continue
action = RequestActions.get_action(request, action_name)
if not action.can_execute():
continue
if request_name not in available_requests:
saved_request_data = copy.deepcopy(request_dict)
saved_request_data["actions"] = [action_name]
available_requests[request_name] = saved_request_data
else:
saved_request_data["actions"].append(action_name) # noqa we are sure that saved_request_data exists


def before_ui_detail(self, identity, record=None, errors=None, **kwargs):
self._add_available_requests(identity, record, "extra_context", kwargs)

def before_ui_edit(self, identity, record=None, errors=None, **kwargs):
self._add_available_requests(identity, record, "extra_context", kwargs)

def form_config(self, identity, record=None, errors=None, **kwargs):
self._add_available_requests(identity, record, "form_config", kwargs)
Empty file.
63 changes: 63 additions & 0 deletions oarepo_requests/services/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import functools

from invenio_records_resources.services.records.components import ServiceComponent
from invenio_records_resources.services.uow import RecordCommitOp
from invenio_requests import current_request_type_registry, current_requests_service, current_events_service
from invenio_requests.customizations import LogEventType


class PublishDraftComponentPrivate(ServiceComponent):
"""Service component for request integration."""

def __init__(self, publish_request_type, delete_request_type, *args, **kwargs):
super().__init__(*args, **kwargs)
self.publish_request_type = publish_request_type
self.delete_request_type = delete_request_type

def create(self, identity, data=None, record=None, **kwargs):
"""Create the review if requested."""
# topic and request_type in kwargs
if self.publish_request_type:
type_ = current_request_type_registry.lookup(
self.publish_request_type, quiet=True
)
request_item = current_requests_service.create(
identity, {}, type_, receiver=None, topic=record, uow=self.uow
)
setattr(record.parent, self.publish_request_type, request_item._request)
self.uow.register(RecordCommitOp(record.parent))

def publish(self, identity, data=None, record=None, **kwargs):
publish_request = getattr(record.parent, self.publish_request_type)

if publish_request is not None:
request = publish_request.get_object()
request_status = "accepted"
request.status = request_status
setattr(record.parent, self.publish_request_type, None)
event = LogEventType(
payload={
"event": request_status,
"content": "record was published through direct call without request",
}
)
_data = dict(payload=event.payload)
current_events_service.create(
identity, request.id, _data, event, uow=self.uow
)

if self.delete_request_type:
type_ = current_request_type_registry.lookup(
self.delete_request_type, quiet=True
)
request_item = current_requests_service.create(
identity, {}, type_, receiver=None, topic=record, uow=self.uow
)
setattr(record.parent, self.delete_request_type, request_item._request)
self.uow.register(RecordCommitOp(record.parent))


def PublishDraftComponent(publish_request_type, delete_request_type):
return functools.partial(
PublishDraftComponentPrivate, publish_request_type, delete_request_type
)
10 changes: 4 additions & 6 deletions tests/test_ui/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from oarepo_ui.resources.components import PermissionsComponent
from thesis.resources.records.ui import ThesisUIJSONSerializer

from oarepo_requests.components import AllowedRequestsComponent
from oarepo_requests.resources.components import AllowedRequestsComponent


class ModelUIResourceConfig(RecordsUIResourceConfig):
Expand All @@ -20,11 +20,9 @@ class ModelUIResourceConfig(RecordsUIResourceConfig):

templates = {
**RecordsUIResourceConfig.templates,
"detail": {"layout": "TestDetail.jinja", "blocks": {}},
"search": {
"layout": "TestDetail.jinja",
},
"edit": {"layout": "TestEdit.jinja"},
"detail": "TestDetail",
"search": "TestDetail",
"edit": "TestEdit",
}

components = [BabelComponent, PermissionsComponent, AllowedRequestsComponent]
Expand Down
2 changes: 1 addition & 1 deletion tests/thesis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ record:
components:
- PublishDraftComponent("publish_draft", "delete_record")
imports:
- import: oarepo_requests.components.requests.PublishDraftComponent
- import: oarepo_requests.services.components.PublishDraftComponent
draft:
requests:
publish-draft:
Expand Down

0 comments on commit 2cd2b8e

Please sign in to comment.