Skip to content

Commit

Permalink
schema: use post_load to load parent info
Browse files Browse the repository at this point in the history
  • Loading branch information
yashlamba authored and slint committed Feb 19, 2024
1 parent 5f29dda commit 96c2bd3
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions invenio_communities/communities/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

"""Community schema."""
import re
from copy import deepcopy
from functools import partial
from uuid import UUID

Expand Down Expand Up @@ -252,16 +253,11 @@ def post_dump(self, data, many, **kwargs):
class CommunityParentSchema(BaseCommunitySchema):
"""Community parent schema."""

id = fields.String(required=True)
slug = SanitizedUnicode()
metadata = NestedAttribute(CommunityMetadataSchema)
access = NestedAttribute(CommunityAccessSchema)


class CommunitySchema(BaseCommunitySchema):
"""Community schema."""

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

@post_dump
def post_dump(self, data, many, **kwargs):
Expand All @@ -271,11 +267,15 @@ def post_dump(self, data, many, **kwargs):
data.pop("parent", None)
return data

@post_load
def filter_parent_id(self, in_data, **kwargs):
@post_load(pass_original=True)
def filter_parent_id(self, in_data, original_data, **kwargs):
"""Simply keep the parent id."""
if in_data.get("parent"):
in_data["parent"] = dict(id=in_data.get("parent", {}).get("id"))
if "parent" in original_data:
in_data["parent"] = (
dict(id=original_data["parent"]["id"])
if original_data["parent"]
else None
)
return in_data

@pre_load
Expand Down

0 comments on commit 96c2bd3

Please sign in to comment.