-
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 branch 'main' into stojanovic/fe-251-deposit-form-publish
- Loading branch information
Showing
23 changed files
with
399 additions
and
146 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 |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from invenio_access.permissions import system_identity | ||
from invenio_requests import ( | ||
current_events_service, | ||
current_request_type_registry, | ||
current_requests_service, | ||
) | ||
from invenio_requests.records import Request | ||
from invenio_requests.resolvers.registry import ResolverRegistry | ||
|
||
from oarepo_requests.utils import _reference_query_term | ||
|
||
|
||
def _str_from_ref(ref): | ||
k, v = list(ref.items())[0] | ||
return f"{k}.{v}" | ||
|
||
|
||
def _get_topic_ref_with_requests(topic): | ||
topic_ref = ResolverRegistry.reference_entity(topic) | ||
requests_with_topic = current_requests_service.scan( | ||
system_identity, extra_filter=_reference_query_term("topic", topic_ref) | ||
) | ||
return requests_with_topic, topic_ref | ||
|
||
|
||
def _create_event(cur_request, payload, event_type, uow): | ||
data = {"payload": payload} | ||
current_events_service.create( | ||
system_identity, | ||
cur_request.id, | ||
data, | ||
event_type=event_type, | ||
uow=uow, | ||
) | ||
|
||
|
||
def update_topic(request, old_topic, new_topic, uow): | ||
from oarepo_requests.types.events import TopicUpdateEventType | ||
|
||
requests_with_topic, old_topic_ref = _get_topic_ref_with_requests(old_topic) | ||
new_topic_ref = ResolverRegistry.reference_entity(new_topic) | ||
for request_from_search in requests_with_topic: | ||
request_type = current_request_type_registry.lookup( | ||
request_from_search["type"], quiet=True | ||
) | ||
if hasattr(request_type, "topic_change"): | ||
cur_request = ( | ||
Request.get_record(request_from_search["id"]) | ||
if request_from_search["id"] != str(request.id) | ||
else request | ||
) # request on which the action is executed is recommited later, the change must be done on the same instance | ||
request_type.topic_change(cur_request, new_topic_ref, uow) | ||
if ( | ||
cur_request.topic.reference_dict != old_topic_ref | ||
): # what if we don't change topic but still do some event we want to log, ie. cancelling the request because it does not apply to published record | ||
payload = { | ||
"old_topic": _str_from_ref(old_topic_ref), | ||
"new_topic": _str_from_ref(new_topic_ref), | ||
} | ||
_create_event(cur_request, payload, TopicUpdateEventType, uow) | ||
|
||
|
||
def cancel_requests_on_topic_delete(request, topic, uow): | ||
from oarepo_requests.types.events import TopicDeleteEventType | ||
|
||
requests_with_topic, topic_ref = _get_topic_ref_with_requests(topic) | ||
for request_from_search in requests_with_topic: | ||
request_type = current_request_type_registry.lookup( | ||
request_from_search["type"], quiet=True | ||
) | ||
if hasattr(request_type, "on_topic_delete"): | ||
if request_from_search["id"] == str(request.id): | ||
continue | ||
cur_request = Request.get_record(request_from_search["id"]) | ||
if cur_request.is_open: | ||
request_type.on_topic_delete( | ||
cur_request, uow | ||
) # possibly return message to save on event payload? | ||
payload = {"topic": _str_from_ref(topic_ref)} | ||
_create_event(cur_request, payload, TopicDeleteEventType, 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
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,11 +1,20 @@ | ||
from oarepo_runtime.datastreams.utils import get_record_service_for_record | ||
from oarepo_runtime.i18n import lazy_gettext as _ | ||
|
||
from .generic import OARepoAcceptAction | ||
from .cascade_events import cancel_requests_on_topic_delete | ||
from .generic import OARepoAcceptAction, OARepoDeclineAction | ||
|
||
|
||
class DeletePublishedRecordAcceptAction(OARepoAcceptAction): | ||
name = _("Permanently delete") | ||
|
||
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.delete(identity, topic["id"], uow=uow, *args, **kwargs) | ||
cancel_requests_on_topic_delete(self.request, topic, uow) | ||
|
||
|
||
class DeletePublishedRecordDeclineAction(OARepoDeclineAction): | ||
name = _("Keep the 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
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
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
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,5 +1,4 @@ | ||
from .topic_delete import TopicDeleteEventType | ||
from .topic_update import TopicUpdateEventType | ||
|
||
__all__ = [ | ||
"TopicUpdateEventType", | ||
] | ||
__all__ = ["TopicUpdateEventType", "TopicDeleteEventType"] |
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,16 @@ | ||
from invenio_requests.customizations.event_types import EventType | ||
from marshmallow import fields | ||
|
||
from oarepo_requests.types.events.validation import _serialized_topic_validator | ||
|
||
|
||
class TopicDeleteEventType(EventType): | ||
"""Comment event type.""" | ||
|
||
type_id = "D" | ||
|
||
payload_schema = dict( | ||
topic=fields.Str(validate=[_serialized_topic_validator]), | ||
) | ||
|
||
payload_required = True |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
def _serialized_topic_validator(value): | ||
if len(value.split(".")) != 2: | ||
raise ValueError( | ||
"Serialized topic must be a string with model and id separated by a single dot." | ||
) | ||
return value |
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
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.