Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed May 24, 2024
1 parent 43697db commit e80dc2c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 67 deletions.
106 changes: 42 additions & 64 deletions djangocms_rest/serializers/pageserializer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime

from cms.models import Page
from cms.models import PageContent
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from rest_framework import serializers
Expand All @@ -9,71 +9,51 @@

class RESTPage:
def __init__(
self, request: Request, page: Page, language: str | None = None
self, request: Request, page_content: PageContent
) -> None:
host = f"{request.scheme}://{request.get_host()}"
page_content = page.get_content_obj(language, fallback=True)
if page_content:
self.title: str = page_content.title
self.page_title: str = page_content.page_title or self.title
self.menu_title: str = page_content.menu_title or self.title
self.meta_description: str = page_content.meta_description
self.redirect: str = page_content.redirect
placeholders = page.get_placeholders(language)
declared_slots = [
placeholder.slot for placeholder in page.get_declared_placeholders()
]
content_type_id = ContentType.objects.get_for_model(
page_content.__class__
).pk
self.placeholders: dict[str, str] = (
{
placeholder.slot: host
+ reverse(
"placeholder-detail",
args=(
language,
content_type_id,
page_content.pk,
placeholder.slot,
),
)
for placeholder in placeholders
if placeholder.slot in declared_slots
}
if page_content
else {}
)
self.in_navigation: bool = page_content.in_navigation
self.soft_root: bool = page_content.soft_root
self.template: str = page_content.template
self.xframe_options: str = page_content.xframe_options
self.limit_visibility_in_menu: int = page_content.limit_visibility_in_menu
self.language: str = page_content.language
self.creation_date: datetime.datetime | None = page_content.creation_date
self.changed_date: datetime.datetime | None = page_content.changed_date
else:
self.title: str = ""
self.page_title: str = ""
self.menu_title: str = ""
self.meta_description: str = ""
self.redirect: str = ""
self.placeholders: dict[str, str] = {}
self.title: str = page_content.title
self.page_title: str = page_content.page_title or self.title
self.menu_title: str = page_content.menu_title or self.title
self.meta_description: str = page_content.meta_description
self.redirect: str = page_content.redirect
placeholders = page_content.page.get_placeholders(page_content.language)
declared_slots = [
placeholder.slot for placeholder in page_content.page.get_declared_placeholders()
]
content_type_id = ContentType.objects.get_for_model(
page_content.__class__
).pk
self.placeholders: dict[str, str] = (
{
placeholder.slot: host
+ reverse(
"placeholder-detail",
args=(
page_content.language,
content_type_id,
page_content.pk,
placeholder.slot,
),
)
for placeholder in placeholders
if placeholder.slot in declared_slots
}
if page_content
else {}
)
self.in_navigation: bool = page_content.in_navigation
self.soft_root: bool = page_content.soft_root
self.template: str = page_content.template
self.xframe_options: str = page_content.xframe_options
self.limit_visibility_in_menu: int = page_content.limit_visibility_in_menu
self.language: str = page_content.language

self.in_navigation: bool = False
self.soft_root: bool = False
self.template: str = ""
self.xframe_options: str = ""
self.limit_visibility_in_menu: int = 0
self.language: str = page_content.language
self.creation_date: datetime.datetime | None = None
self.changed_date: datetime.datetime | None = None
self.absolute_url: str = page_content.page.get_absolute_url(page_content.language)
self.path: str = page_content.page.get_path(page_content.language)

self.absolute_url: str = page.get_absolute_url(language)
self.path: str = page.get_path(language)

self.is_home: bool = page.is_home
self.languages: list[str] = page.languages.split(",")
self.is_home: bool = page_content.page.is_home
self.languages: list[str] = page_content.page.languages.split(",")
super().__init__()


Expand All @@ -96,8 +76,6 @@ class PageSerializer(serializers.Serializer):
limit_visibility_in_menu = serializers.BooleanField()

language = serializers.CharField(max_length=10)
creation_date = serializers.DateTimeField()
changed_date = serializers.DateTimeField()
languages = serializers.ListSerializer(
child=serializers.CharField(), allow_empty=True, required=False
)
6 changes: 3 additions & 3 deletions djangocms_rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def get(self, request, language, format=None):
if request.user.is_anonymous:
qs = qs.filter(login_required=False)
pages = (
RESTPage(request, page, language=language)
RESTPage(request, page.get_content_obj(language, fallback=True))
for page in qs
if user_can_view_page(request.user, page)
if user_can_view_page(request.user, page) and page.get_content_obj(language, fallback=True)
)
serializer = PageSerializer(pages, many=True, read_only=True)
return Response(serializer.data)
Expand Down Expand Up @@ -87,7 +87,7 @@ def get(self, request, language, path="", format=None):
raise Http404

serializer = PageSerializer(
RESTPage(request, page, language=language), read_only=True
RESTPage(request, page.get_content_obj(language, fallback=True)), read_only=True
)
return Response(serializer.data)

Expand Down

0 comments on commit e80dc2c

Please sign in to comment.