|
10 | 10 | """RDM Record Communities Service.""" |
11 | 11 |
|
12 | 12 | from flask import current_app |
| 13 | +from flask_principal import AnonymousIdentity |
13 | 14 | from invenio_access.permissions import system_identity |
14 | 15 | from invenio_communities.proxies import current_communities |
15 | 16 | from invenio_drafts_resources.services.records.uow import ParentRecordCommitOp |
|
34 | 35 |
|
35 | 36 | from ...notifications.builders import CommunityInclusionSubmittedNotificationBuilder |
36 | 37 | from ...proxies import current_rdm_records, current_rdm_records_service |
37 | | -from ...requests import CommunityInclusion |
| 38 | +from ...requests import CommunityInclusion, CommunitySubmission |
38 | 39 | from ..errors import ( |
39 | 40 | CannotRemoveCommunityError, |
40 | 41 | CommunityAlreadyExists, |
@@ -493,3 +494,55 @@ def bulk_add(self, identity, community_id, record_ids, set_default=False, uow=No |
493 | 494 | ) |
494 | 495 | ) |
495 | 496 | 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