-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from oarepo/krist/be-377-bridge-workflows-to-o…
…arepo-requests Krist/be 377 bridge workflows to oarepo requests
- Loading branch information
Showing
51 changed files
with
1,044 additions
and
385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,7 @@ dist/ | |
|
||
tests/thesis | ||
thesis | ||
thesis_workflows/ | ||
|
||
todo.md | ||
node_modules | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import abc | ||
import contextlib | ||
from typing import Generator | ||
|
||
from invenio_requests.customizations import RequestActions | ||
from invenio_requests.errors import CannotExecuteActionError | ||
|
||
from oarepo_requests.services.permissions.identity import request_active | ||
|
||
|
||
class RequestActionComponent: | ||
@abc.abstractmethod | ||
def apply( | ||
self, identity, request_type, action, topic, uow, *args, **kwargs | ||
) -> Generator: | ||
""" | ||
:param action: | ||
:param identity: | ||
:param uow: | ||
:param args: | ||
:param kwargs: | ||
:return: | ||
""" | ||
|
||
|
||
class RequestIdentityComponent(RequestActionComponent): | ||
@contextlib.contextmanager | ||
def apply(self, identity, request_type, action, topic, uow, *args, **kwargs): | ||
|
||
identity.provides.add(request_active) | ||
try: | ||
yield | ||
finally: | ||
if request_active in identity.provides: | ||
identity.provides.remove(request_active) | ||
|
||
|
||
class WorkflowTransitionComponent(RequestActionComponent): | ||
|
||
@contextlib.contextmanager | ||
def apply(self, identity, request_type, action, topic, uow, *args, **kwargs): | ||
from oarepo_workflows.proxies import current_oarepo_workflows | ||
|
||
yield | ||
transitions = ( | ||
current_oarepo_workflows.get_workflow(topic) | ||
.requests()[request_type.type_id] | ||
.transitions | ||
) | ||
target_state = transitions[action.status_to] | ||
if ( | ||
target_state and not topic.model.is_deleted | ||
): # commit doesn't work on deleted record? | ||
current_oarepo_workflows.set_state( | ||
identity, | ||
topic, | ||
target_state, | ||
request=action.request, | ||
uow=uow, | ||
) | ||
|
||
|
||
class AutoAcceptComponent(RequestActionComponent): | ||
@contextlib.contextmanager | ||
def apply(self, identity, request_type, action, topic, uow, *args, **kwargs): | ||
yield | ||
if action.request.status != "submitted": | ||
return | ||
receiver_ref = action.request.receiver # this is <x>proxy, not dict | ||
if not receiver_ref.reference_dict.get("auto_approve"): | ||
return | ||
|
||
action_obj = RequestActions.get_action(action.request, "accept") | ||
if not action_obj.can_execute(): | ||
raise CannotExecuteActionError("accept") | ||
action_obj.execute(identity, uow, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,12 @@ | ||
from invenio_requests.customizations import actions | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
|
||
from ..utils import get_matching_service_for_record | ||
from .generic import OARepoAcceptAction | ||
|
||
|
||
class DeleteTopicAcceptAction(actions.AcceptAction): | ||
log_event = True | ||
class DeleteTopicAcceptAction(OARepoAcceptAction): | ||
|
||
def execute(self, identity, uow, *args, **kwargs): | ||
topic = self.request.topic.resolve() | ||
topic_service = get_matching_service_for_record(topic) | ||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
topic_service = get_record_service_for_record(topic) | ||
if not topic_service: | ||
raise KeyError(f"topic {topic} service not found") | ||
# uow.register(RecordDeleteOp(topic, topic_service.indexer, index_refresh=True)) | ||
topic_service.delete(identity, topic["id"], uow=uow, *args, **kwargs) | ||
super().execute(identity, uow) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
from invenio_requests.customizations import actions | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
|
||
from ..utils import get_matching_service_for_record | ||
from .generic import OARepoAcceptAction | ||
|
||
|
||
class EditTopicAcceptAction(actions.AcceptAction): | ||
log_event = True | ||
|
||
def execute(self, identity, uow): | ||
topic = self.request.topic.resolve() | ||
topic_service = get_matching_service_for_record(topic) | ||
class EditTopicAcceptAction(OARepoAcceptAction): | ||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
topic_service = get_record_service_for_record(topic) | ||
if not topic_service: | ||
raise KeyError(f"topic {topic} service not found") | ||
topic_service.edit(identity, topic["id"], uow=uow) | ||
super().execute(identity, uow) | ||
edit_ret = topic_service.edit(identity, topic["id"], uow=uow) | ||
return edit_ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,50 @@ | ||
from functools import cached_property | ||
|
||
from invenio_requests.customizations import actions | ||
from invenio_requests.customizations.actions import RequestActions | ||
from invenio_requests.errors import CannotExecuteActionError | ||
|
||
from oarepo_requests.proxies import current_oarepo_requests | ||
|
||
|
||
class OARepoGenericActionMixin: | ||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
pass | ||
|
||
def _execute_with_components( | ||
self, components, identity, request_type, topic, uow, *args, **kwargs | ||
): | ||
if not components: | ||
self.apply(identity, request_type, topic, uow, *args, **kwargs) | ||
super().execute(identity, uow, *args, **kwargs) | ||
else: | ||
with components[0].apply( | ||
identity, request_type, self, topic, uow, *args, **kwargs | ||
): | ||
self._execute_with_components( | ||
components[1:], identity, request_type, topic, uow, *args, **kwargs | ||
) | ||
|
||
@cached_property | ||
def components(self): | ||
return [ | ||
component_cls() | ||
for component_cls in current_oarepo_requests.action_components(self) | ||
] | ||
|
||
def execute(self, identity, uow, *args, **kwargs): | ||
request_type = self.request.type | ||
topic = self.request.topic.resolve() | ||
self._execute_with_components( | ||
self.components, identity, request_type, topic, uow, *args, **kwargs | ||
) | ||
|
||
|
||
class OARepoSubmitAction(OARepoGenericActionMixin, actions.SubmitAction): | ||
"""""" | ||
|
||
|
||
class OARepoDeclineAction(OARepoGenericActionMixin, actions.DeclineAction): | ||
"""""" | ||
|
||
class AutoAcceptSubmitAction(actions.SubmitAction): | ||
log_event = True | ||
|
||
def execute(self, identity, uow): | ||
super().execute(identity, uow) | ||
action_obj = RequestActions.get_action(self.request, "accept") | ||
if not action_obj.can_execute(): | ||
raise CannotExecuteActionError("accept") | ||
action_obj.execute(identity, uow) | ||
class OARepoAcceptAction(OARepoGenericActionMixin, actions.AcceptAction): | ||
"""""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
from invenio_requests.customizations import actions | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
|
||
from ..utils import get_matching_service_for_record | ||
from .generic import OARepoAcceptAction | ||
|
||
|
||
def publish_draft(draft, identity, uow, *args, **kwargs): | ||
topic_service = get_matching_service_for_record(draft) | ||
if not topic_service: | ||
raise KeyError(f"topic {draft} service not found") | ||
id_ = draft["id"] | ||
return topic_service.publish(identity, id_, uow=uow, expand=False, *args, **kwargs) | ||
|
||
|
||
class PublishDraftAcceptAction(actions.AcceptAction): | ||
log_event = True | ||
|
||
def execute(self, identity, uow, *args, **kwargs): | ||
topic = self.request.topic.resolve() | ||
record = publish_draft(topic, identity, uow, *args, **kwargs) | ||
super().execute(identity, uow, *args, **kwargs) | ||
return record._record | ||
class PublishDraftAcceptAction(OARepoAcceptAction): | ||
def apply(self, identity, request_type, topic, uow, *args, **kwargs): | ||
topic_service = get_record_service_for_record(topic) | ||
if not topic_service: | ||
raise KeyError(f"topic {topic} service not found") | ||
id_ = topic["id"] | ||
return topic_service.publish( | ||
identity, id_, uow=uow, expand=False, *args, **kwargs | ||
)._record |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.