Skip to content

Commit

Permalink
subcommunities: add invitation request
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Nov 18, 2024
1 parent 6230fca commit bf7c379
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
4 changes: 4 additions & 0 deletions invenio.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ from zenodo_rdm.queryparser import word_communities, word_doi
from zenodo_rdm.subcommunities import (
ZenodoSubCommunityRequest,
ZenodoSubcommunityRequestSchema,
ZenodoSubCommunityInvitationRequest
)
from zenodo_rdm.tokens import RATSubjectSchema
from zenodo_rdm.views import frontpage_view_function
Expand Down Expand Up @@ -1035,6 +1036,9 @@ COMMUNITIES_SUB_SERVICE_SCHEMA = ZenodoSubcommunityRequestSchema
COMMUNITIES_SUB_REQUEST_CLS = ZenodoSubCommunityRequest
"""Request type for subcommunities."""

COMMUNITIES_SUB_INVITATION_REQUEST_CLS = ZenodoSubCommunityInvitationRequest
"""Request type for subcommunity invitations."""

ANNOSTOR_COMMUNITIES = {}
"""Annostor communities configuration.
Expand Down
30 changes: 30 additions & 0 deletions site/zenodo_rdm/legacy/requests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from .community_manage_record import CommunityManageRecord
from .record_upgrade import LegacyRecordUpgrade
from ...subcommunities.request import ZenodoSubCommunityInvitationRequest


def exists(identity, record):
Expand Down Expand Up @@ -107,3 +108,32 @@ def submit_community_manage_record_request(user_id, uow, comment=None):
return current_requests_service.execute_action(
system_identity, request_item._request.id, "submit", data=comment, uow=uow
)


@unit_of_work()
def submit_join_as_subcommunity_request(
parent_community_uuid, community_uuid, uow, comment=None
):
"""Create and submit a SubCommunityInvitation request."""
type_ = current_request_type_registry.lookup(ZenodoSubCommunityInvitationRequest.type_id)
creator = ResolverRegistry.resolve_entity_proxy(
{"community": parent_community_uuid}
).resolve()
receiver = ResolverRegistry.resolve_entity_proxy(
{"community": community_uuid}
).resolve()
expires_at = datetime.utcnow() + timedelta(days=30)

request_item = current_requests_service.create(
system_identity,
{},
type_,
receiver,
creator=creator,
expires_at=expires_at,
uow=uow,
)

return current_requests_service.execute_action(
system_identity, request_item._request.id, "submit", data=comment, uow=uow
)
2 changes: 2 additions & 0 deletions site/zenodo_rdm/subcommunities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"""Subcommunities implementation."""

from .request import ZenodoSubCommunityRequest
from .request import ZenodoSubCommunityInvitationRequest
from .schema import ZenodoSubcommunityRequestSchema

__all__ = (
"ZenodoSubcommunityRequestSchema",
"ZenodoSubCommunityRequest",
"ZenodoSubCommunityInvitationRequest",
)
68 changes: 68 additions & 0 deletions site/zenodo_rdm/subcommunities/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
AcceptSubcommunity,
DeclineSubcommunity,
SubCommunityRequest,
SubCommunityInvitationRequest,
)
from invenio_rdm_records.proxies import (
current_community_records_service,
Expand Down Expand Up @@ -113,8 +114,75 @@ class ZenodoSubCommunityRequest(SubCommunityRequest):

available_actions = {
"delete": actions.DeleteAction,
"cancel": actions.CancelAction,
# Custom implemented actions
"create": SubcommunityCreateAction,
"accept": SubcommunityAcceptAction,
"decline": DeclineSubcommunity,
}


class SubCommunityInvitationAcceptAction(AcceptSubcommunity):
"""Represents an accept action used to accept a subcommunity.
Zenodo re-implementation of the accept action, to also move the records.
"""

def _get_community_records(self, community_id):
"""Get the records of a community."""
return current_community_records_service.search(
system_identity, community_id=community_id
)

def execute(self, identity, uow):
"""Execute approve action."""
to_be_moved = self.request.topic.resolve().id
move_to = self.request.receiver.resolve().id

# Move records
records = self._get_community_records(to_be_moved)
current_rdm_records.record_communities_service.bulk_add(
system_identity, move_to, (x["id"] for x in records), uow=uow
)
super().execute(identity, uow)


class SubcommunityInvitationSubmitAction(actions.SubmitAction):
"""Submit action."""

def execute(self, identity, uow):
"""Execute the submit action."""
self.request["title"] = "Invitation to join the EU Open Research Repository"

# example: "May 11, 2024"
expires_at = self.request.expires_at.strftime("%B %d, %Y")
self.request["description"] = (
"<p><br><br>If you do not perform any action by <b>{expires_at}</b>, the permission for "
"community curators, managers and owners to manage your records will be automatically granted.</p>"
)

super().execute(identity, uow)


class SubcommunityInvitationExpireAction(actions.ExpireAction):
"""Expire action."""

def execute(self, identity, uow):
"""Accepts the request."""

super().execute(identity, uow)


class ZenodoSubCommunityInvitationRequest(SubCommunityInvitationRequest):
"""Request from a Zenodo community to add a child community."""

available_actions = {
"create": actions.CreateAction,
"delete": actions.DeleteAction,
"cancel": actions.CancelAction,
# Custom implemented actions
"submit": SubcommunityInvitationSubmitAction,
"accept": SubcommunityAcceptAction,
"decline": DeclineSubcommunity,
"expire": SubcommunityInvitationExpireAction,
}

0 comments on commit bf7c379

Please sign in to comment.