Skip to content

Commit 2841b49

Browse files
authored
fix: Page serializer returned null for empty mets_description (#56)
1 parent 7723162 commit 2841b49

File tree

2 files changed

+13
-27
lines changed

2 files changed

+13
-27
lines changed

djangocms_rest/serializers/pages.py

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class BasePageSerializer(serializers.Serializer):
2525
xframe_options = serializers.CharField(max_length=50, allow_blank=True)
2626
limit_visibility_in_menu = serializers.BooleanField(default=False, allow_null=True)
2727
language = serializers.CharField(max_length=10)
28-
languages = serializers.ListSerializer(
29-
child=serializers.CharField(), allow_empty=True, required=False
30-
)
28+
languages = serializers.ListSerializer(child=serializers.CharField(), allow_empty=True, required=False)
3129
is_preview = serializers.BooleanField(default=False)
3230
application_namespace = serializers.CharField(max_length=200, allow_null=True)
3331
creation_date = serializers.DateTimeField()
@@ -37,17 +35,13 @@ class BasePageSerializer(serializers.Serializer):
3735
class BasePageContentMixin:
3836
@property
3937
def is_preview(self):
40-
return "preview" in self.request.GET and self.request.GET.get(
41-
"preview", ""
42-
).lower() not in ("0", "false")
38+
return "preview" in self.request.GET and self.request.GET.get("preview", "").lower() not in ("0", "false")
4339

4440
def get_base_representation(self, page_content: PageContent) -> dict:
4541
request = getattr(self, "request", None)
4642
path = page_content.page.get_path(page_content.language)
4743
absolute_url = get_absolute_frontend_url(request, path)
48-
api_endpoint = get_absolute_frontend_url(
49-
request, page_content.page.get_api_endpoint(page_content.language)
50-
)
44+
api_endpoint = get_absolute_frontend_url(request, page_content.page.get_api_endpoint(page_content.language))
5145
redirect = str(page_content.redirect or "")
5246
xframe_options = str(page_content.xframe_options or "")
5347
application_namespace = str(page_content.page.application_namespace or "")
@@ -57,8 +51,8 @@ def get_base_representation(self, page_content: PageContent) -> dict:
5751
"title": page_content.title,
5852
"page_title": page_content.page_title or page_content.title,
5953
"menu_title": page_content.menu_title or page_content.title,
60-
"meta_description": page_content.meta_description,
61-
"redirect": redirect,
54+
"meta_description": page_content.meta_description or "", # Model allows None, schema does not
55+
"redirect": redirect or "",
6256
"in_navigation": page_content.in_navigation,
6357
"soft_root": page_content.soft_root,
6458
"template": page_content.template,
@@ -89,9 +83,7 @@ def tree_to_representation(self, item: PageContent) -> dict:
8983
serialized_data = self.child.to_representation(item)
9084
serialized_data["children"] = []
9185
if item.page in self.tree:
92-
serialized_data["children"] = [
93-
self.tree_to_representation(child) for child in self.tree[item.page]
94-
]
86+
serialized_data["children"] = [self.tree_to_representation(child) for child in self.tree[item.page]]
9587
return serialized_data
9688

9789
def to_representation(self, data: dict) -> list[dict]:
@@ -100,9 +92,7 @@ def to_representation(self, data: dict) -> list[dict]:
10092

10193

10294
class PageMetaSerializer(BasePageSerializer, BasePageContentMixin):
103-
children = serializers.ListSerializer(
104-
child=serializers.DictField(), required=False, default=[]
105-
)
95+
children = serializers.ListSerializer(child=serializers.DictField(), required=False, default=[])
10696

10797
def __init__(self, *args, **kwargs):
10898
super().__init__(*args, **kwargs)
@@ -123,9 +113,7 @@ def many_init(cls, *args, **kwargs):
123113
try:
124114
parent = instance.page.parent
125115
except AttributeError:
126-
parent = (
127-
instance.page.parent_page
128-
) # TODO: Remove when django CMS 4.1 is no longer supported
116+
parent = instance.page.parent_page # TODO: Remove when django CMS 4.1 is no longer supported
129117
tree.setdefault(parent, []).append(instance)
130118

131119
# Prepare the child serializer with the proper context.
@@ -144,14 +132,9 @@ def __init__(self, *args, **kwargs):
144132
self.request = self.context.get("request")
145133

146134
def to_representation(self, page_content: PageContent) -> dict:
147-
declared_slots = [
148-
placeholder.slot
149-
for placeholder in page_content.page.get_declared_placeholders()
150-
]
135+
declared_slots = [placeholder.slot for placeholder in page_content.page.get_declared_placeholders()]
151136
placeholders = [
152-
placeholder
153-
for placeholder in page_content.placeholders.all()
154-
if placeholder.slot in declared_slots
137+
placeholder for placeholder in page_content.placeholders.all() if placeholder.slot in declared_slots
155138
]
156139

157140
placeholders_data = [

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ version = {attr = "djangocms_rest.__version__"}
7171
[tool.setuptools.packages.find]
7272
where = ["."]
7373
include = ["djangocms_rest*"]
74+
75+
[tool.ruff]
76+
line-length = 119

0 commit comments

Comments
 (0)