Skip to content

Commit

Permalink
subcommunities: check for parent children allow
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandromumo committed Jun 27, 2024
1 parent 70551f5 commit 0a5ccc3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
15 changes: 15 additions & 0 deletions invenio_communities/subcommunities/resources/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
# it under the terms of the MIT License; see LICENSE file for more details.
"""Subcommunity resource configuration."""
from flask_resources import (
HTTPJSONException,
JSONDeserializer,
JSONSerializer,
RequestBodyParser,
ResourceConfig,
ResponseHandler,
create_error_handler,
)
from invenio_records_resources.resources.records.headers import etag_headers
from invenio_records_resources.services.base.config import ConfiguratorMixin
from marshmallow import fields

from ..services.errors import ParentChildrenNotAllowed


class SubCommunityResourceConfig(ConfiguratorMixin, ResourceConfig):
"""Subcommunity resource configuration."""
Expand All @@ -41,3 +45,14 @@ class SubCommunityResourceConfig(ConfiguratorMixin, ResourceConfig):
"application/json": ResponseHandler(JSONSerializer(), headers=etag_headers)
}
default_accept_mimetype = "application/json"

# Error handling
error_handlers = {
# Parent community does not allow children
ParentChildrenNotAllowed: create_error_handler(
lambda e: HTTPJSONException(
code=400,
description=str(e),
)
),
}
12 changes: 11 additions & 1 deletion invenio_communities/subcommunities/services/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import invenio_communities.notifications.builders as notifications
from invenio_communities.proxies import current_communities, current_roles

from .errors import ParentChildrenNotAllowed

community_service = LocalProxy(lambda: current_communities.service)


Expand Down Expand Up @@ -76,6 +78,12 @@ def join(self, identity, id_, data, uow=None):
denied, the transaction will be rolled back.
"""
parent_community = community_service.record_cls.pid.resolve(id_)

if not parent_community.children.allow:
raise ParentChildrenNotAllowed(
_("Parent community does not accept subcommunities.")
)

data_, errors = self.schema.load(
data,
context={"identity": identity, "community": parent_community},
Expand Down Expand Up @@ -122,7 +130,9 @@ def join(self, identity, id_, data, uow=None):

is_owner_parent = self._is_owner_of(identity, str(id_))
if is_owner_child and is_owner_parent:
request = requests_service.execute_action(identity, request.id, "accept")
request = requests_service.execute_action(
identity, request.id, "accept", uow=uow
)

return request

Expand Down

0 comments on commit 0a5ccc3

Please sign in to comment.