From e80dc2c0d83ac3ad7ffd804e1037a805d2f84b26 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Sat, 25 May 2024 01:37:47 +0200 Subject: [PATCH] Code improvements --- djangocms_rest/serializers/pageserializer.py | 106 ++++++++----------- djangocms_rest/views.py | 6 +- 2 files changed, 45 insertions(+), 67 deletions(-) diff --git a/djangocms_rest/serializers/pageserializer.py b/djangocms_rest/serializers/pageserializer.py index 8f62522..096d73c 100644 --- a/djangocms_rest/serializers/pageserializer.py +++ b/djangocms_rest/serializers/pageserializer.py @@ -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 @@ -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__() @@ -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 ) diff --git a/djangocms_rest/views.py b/djangocms_rest/views.py index b5ce2f8..3f48058 100644 --- a/djangocms_rest/views.py +++ b/djangocms_rest/views.py @@ -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) @@ -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)