Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update request title #34

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions invenio_curations/services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,25 @@ def delete_draft(self, identity, draft=None, record=None, force=False):
# Delete draft for a published record.
# Since only one request per record should exist, it is not deleted. Instead, put it back to accepted.
current_requests_service.execute_action(
system_identity, request["id"], "cancel"
system_identity, request["id"], "cancel", uow=self.uow
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, good catch 🎣

)

def _check_update_request(
self, identity, request, data=None, record=None, errors=None
):
"""Update request title if record title has changed."""
updated_draft_title = (data or {}).get("metadata", {}).get("title")
current_draft_title = (record or {}).get("metadata", {}).get("title")
if current_draft_title != updated_draft_title:
request["title"] = "RDM Curation: {title}".format(
title=updated_draft_title or record["id"]
)
# Using system identity, to not have to update the default request can_update permission.
# Data will be checked in the requests service.
current_requests_service.update(
system_identity, request["id"], request, uow=self.uow
)

def update_draft(self, identity, data=None, record=None, errors=None):
"""Update draft handler."""
has_published_record = record is not None and record.is_published
Expand All @@ -97,6 +113,14 @@ def update_draft(self, identity, data=None, record=None, errors=None):
)
return

current_draft = self.service.draft_cls.pid.resolve(
record["id"], registered_only=False
)

self._check_update_request(
identity, request, data=data, record=current_draft, errors=errors
)

# TODO: Should updates be disallowed if the record/request is currently being reviewed?
# It could be possible that the record gets updated while a curator performs a review. The curator would be looking at an outdated record and the review might not be correct.

Expand All @@ -105,9 +129,6 @@ def update_draft(self, identity, data=None, record=None, errors=None):
return

# Compare metadata of current draft and updated draft.
current_draft = self.service.draft_cls.pid.resolve(
record["id"], registered_only=False
)

# Sometimes the metadata differs between the passed `record` and resolved
# `current_draft` in references (e.g. in the `record` object, the creator's
Expand Down Expand Up @@ -140,4 +161,6 @@ def update_draft(self, identity, data=None, record=None, errors=None):

# Request is closed but draft was updated with new data. Put back for review
if diff_list:
current_requests_service.execute_action(identity, request["id"], "resubmit")
current_requests_service.execute_action(
identity, request["id"], "resubmit", uow=self.uow
)
30 changes: 24 additions & 6 deletions invenio_curations/services/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from flask_principal import RoleNeed
from invenio_access.permissions import system_identity
from invenio_pidstore.errors import PIDDoesNotExistError
from invenio_records_permissions.generators import ConditionalGenerator, Generator

from ..proxies import current_curations_service
Expand Down Expand Up @@ -73,22 +74,32 @@ def __init__(self, permission_name, **kwargs):
assert self.entity_field is not None, "Subclass must define entity_field."
super().__init__()

def _get_permission(self, request):
def _get_permission(self, entity):
"""Get the specified permission from the request entity service config."""
entity = getattr(request, self.entity_field)
permission_policy_cls = (
entity.get_resolver().get_service().config.permission_policy_cls
)

return getattr(permission_policy_cls, self.permission_name)

def _get_entity(self, request):
"""Get the specified entity of the request."""
return getattr(request, self.entity_field)

def needs(self, request=None, **kwargs):
"""Set of needs granting permission."""
if request is None:
return set()

permission = self._get_permission(request)
record = request.topic.resolve()
entity = self._get_entity(request)
permission = self._get_permission(entity)
try:
record = entity.resolve()
except PIDDoesNotExistError:
# Could not resolve topic. This may happen when trying to serialize a request and checking its permissions.
# The referenced entity could be deleted, which would result in not being able to serialize instead. Instead,
# an empty set is returned for this permission.
return set()
popped_record = kwargs.pop("record")
needs = [g.needs(record=record, **kwargs) for g in permission]

Expand All @@ -100,8 +111,15 @@ def excludes(self, request=None, **kwargs):
if request is None:
return set()

permission = self._get_permission(request)
record = request.topic.resolve()
entity = self._get_entity(request)
permission = self._get_permission(entity)
try:
record = entity.resolve()
except PIDDoesNotExistError:
# Could not resolve topic. This may happen when trying to serialize a request and checking its permissions.
# The referenced entity could be deleted, which would result in not being able to serialize instead. Instead,
# an empty set is returned for this permission.
return set()
popped_record = kwargs.pop("record")
excludes = [g.excludes(record=record, **kwargs) for g in permission]

Expand Down
1 change: 1 addition & 0 deletions invenio_curations/services/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class CurationRDMRequestsPermissionPolicy(RDMRequestsPermissionPolicy):
Creator(),
Receiver(),
TopicPermission(permission_name="can_review"),
SystemProcess(),
],
else_=RDMRequestsPermissionPolicy.can_read,
)
Expand Down