Skip to content

Commit c36762a

Browse files
committed
feat(RecordCommunitiesService): Add get_record_requests service method
* Moved from invenio_app_rdm/records_ui/views/records.py * Since the function checks for permissions and also access the API layer, it should be a service method following the existing methods in this service class also accessing the communities API
1 parent 39eb8d1 commit c36762a

File tree

1 file changed

+54
-1
lines changed
  • invenio_rdm_records/services/communities

1 file changed

+54
-1
lines changed

invenio_rdm_records/services/communities/service.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""RDM Record Communities Service."""
1111

1212
from flask import current_app
13+
from flask_principal import AnonymousIdentity
1314
from invenio_access.permissions import system_identity
1415
from invenio_communities.proxies import current_communities
1516
from invenio_drafts_resources.services.records.uow import ParentRecordCommitOp
@@ -34,7 +35,7 @@
3435

3536
from ...notifications.builders import CommunityInclusionSubmittedNotificationBuilder
3637
from ...proxies import current_rdm_records, current_rdm_records_service
37-
from ...requests import CommunityInclusion
38+
from ...requests import CommunityInclusion, CommunitySubmission
3839
from ..errors import (
3940
CannotRemoveCommunityError,
4041
CommunityAlreadyExists,
@@ -493,3 +494,55 @@ def bulk_add(self, identity, community_id, record_ids, set_default=False, uow=No
493494
)
494495
)
495496
return errors
497+
498+
def get_record_requests(self, identity, record):
499+
"""
500+
Get all accepted community requests of the given record.
501+
502+
:param identity: The identity performing the action.
503+
:param record: The record (RecordItem) to get the community requests for.
504+
505+
Output: {<Community-UUID>: <Request-UUID>}
506+
"""
507+
self.require_permission(identity, "review", record=record._record)
508+
if type(identity) is AnonymousIdentity:
509+
return {} # secret link users do not have permissions to search requests
510+
511+
# Get all accepted requests that led to the record being added to the community
512+
parent = record._record.parent
513+
community_requests = parent.communities.get_requests()
514+
community_requests = {
515+
str(request.community_id): str(request.request_id)
516+
for request in community_requests
517+
}
518+
519+
# Get the requests that concern only the current record, i.e. submission or inclusion regardless of their status
520+
# This takes precedence over only considering accepted requests because there may be an unaccepted request in this
521+
# version of the record that should be shown instead of the accepted request.
522+
record_requests = current_requests_service.search(
523+
identity,
524+
extra_filter=dsl.Q(
525+
"bool",
526+
must=[
527+
dsl.Q("term", **{"topic.record": record.id}),
528+
dsl.Q(
529+
"terms",
530+
**{
531+
"type": [
532+
CommunityInclusion.type_id,
533+
CommunitySubmission.type_id,
534+
]
535+
},
536+
),
537+
],
538+
),
539+
params={"sort": "oldest"},
540+
)
541+
record_requests = {
542+
request["receiver"]["community"]: request["id"]
543+
for request in record_requests
544+
}
545+
546+
community_requests.update(record_requests)
547+
# Return a dictionary with the community id mapped to the request id
548+
return community_requests

0 commit comments

Comments
 (0)