Skip to content

Commit

Permalink
services: fix set parent to None case
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba authored and slint committed Feb 19, 2024
1 parent 8a31535 commit 5f29dda
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion invenio_communities/communities/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class CommunityParentSchema(BaseCommunitySchema):
class CommunitySchema(BaseCommunitySchema):
"""Community schema."""

parent = NestedAttribute(CommunityParentSchema)
parent = NestedAttribute(CommunityParentSchema, allow_none=True)

@post_dump
def post_dump(self, data, many, **kwargs):
Expand Down
10 changes: 6 additions & 4 deletions invenio_communities/communities/services/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,12 @@ def update(self, identity, data=None, record=None, errors=None, **kwargs):
class CommunityParentComponent(ServiceComponent):
"""Service Component for Community parent."""

def _validate_and_get_parent(self, parent_id, child):
def _validate_and_get_parent(self, parent_data, child):
"""Validate and return parent community."""
if not parent_data:
return None
try:
parent = self.service.record_cls.pid.resolve(parent_id)
parent = self.service.record_cls.pid.resolve(parent_data["id"])
if parent.parent:
raise ValidationError(
"Assigned parent community cannot also have a parent."
Expand All @@ -323,14 +325,14 @@ def create(self, identity, data=None, record=None, **kwargs):
"""Inject parsed theme to the record."""
if "parent" in data:
self.service.require_permission(identity, "update_parent", record=record)
parent = self._validate_and_get_parent(data["parent"]["id"], record)
parent = self._validate_and_get_parent(data["parent"], record)
record.parent = parent

def update(self, identity, data=None, record=None, **kwargs):
"""Update parent community of a community."""
if "parent" in data:
self.service.require_permission(identity, "update_parent", record=record)
parent = self._validate_and_get_parent(data["parent"]["id"], record)
parent = self._validate_and_get_parent(data["parent"], record)
record.parent = parent


Expand Down
5 changes: 3 additions & 2 deletions tests/communities/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,13 +656,14 @@ def test_parent_update(community_service, comm):
def test_parent_remove(community_service, comm):
parent = comm
child = community_service.create(
identity=system_identity, data={**comm.data, "slug": "child2"}
identity=system_identity,
data={**comm.data, "slug": "child2", "parent": {"id": str(parent.id)}},
)

child = community_service.update(
identity=system_identity, id_=str(child.id), data={**child.data, "parent": None}
)
assert str(child._obj.parent.id) == parent.id
assert child._obj.parent == None


def test_update_parent_community_not_exists(community_service, comm):
Expand Down

0 comments on commit 5f29dda

Please sign in to comment.