From 9a12a36861b9e449c500a06794554e930ab59893 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Sat, 25 May 2024 00:17:43 +0200 Subject: [PATCH] Optionally include HTML in placeholder responses --- djangocms_rest/serializers/pageserializer.py | 8 ++++---- djangocms_rest/serializers/placeholder.py | 12 +++++++++++- djangocms_rest/views.py | 9 +++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/djangocms_rest/serializers/pageserializer.py b/djangocms_rest/serializers/pageserializer.py index adae8d9..8f62522 100644 --- a/djangocms_rest/serializers/pageserializer.py +++ b/djangocms_rest/serializers/pageserializer.py @@ -50,8 +50,8 @@ def __init__( 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 = page_content.creation_date - self.changed_date: datetime.datetime = page_content.changed_date + 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 = "" @@ -66,8 +66,8 @@ def __init__( self.xframe_options: str = "" self.limit_visibility_in_menu: int = 0 self.language: str = page_content.language - self.creation_date: datetime.datetime = page_content.creation_date - self.changed_date: datetime.datetime = page_content.changed_date + self.creation_date: datetime.datetime | None = None + self.changed_date: datetime.datetime | None = None self.absolute_url: str = page.get_absolute_url(language) self.path: str = page.get_path(language) diff --git a/djangocms_rest/serializers/placeholder.py b/djangocms_rest/serializers/placeholder.py index 53bc70a..983ec11 100644 --- a/djangocms_rest/serializers/placeholder.py +++ b/djangocms_rest/serializers/placeholder.py @@ -5,10 +5,11 @@ _get_placeholder_cache_version_key, ) from cms.models import Placeholder -from cms.plugin_rendering import BaseRenderer +from cms.plugin_rendering import BaseRenderer, ContentRenderer from cms.utils.conf import get_cms_setting from cms.utils.plugins import get_plugins from django.contrib.sites.shortcuts import get_current_site +from django.template.context import Context from django.template.defaulttags import now from rest_framework import serializers from rest_framework.request import Request @@ -210,6 +211,15 @@ def __init__( language=language, use_cache=True, ) + if request.GET.get("html", False): + content_renderer = ContentRenderer(request) + placeholder.html = content_renderer.render_placeholder( + placeholder, + context=Context({"request": request, "LANGUAGE_CODE": language}), + language=language, + use_cache=True, + ) + self.fields["html"] = serializers.CharField() placeholder.label = placeholder.get_label() placeholder.language = language super().__init__(placeholder, *args, **kwargs) diff --git a/djangocms_rest/views.py b/djangocms_rest/views.py index b0f27df..f2f0c82 100644 --- a/djangocms_rest/views.py +++ b/djangocms_rest/views.py @@ -16,6 +16,8 @@ class APIView(DRFAPIView): class LanguageList(APIView): + """List of languages available for the site.""" + def get(self, request, format=None): languages = get_languages().get(get_current_site(request).id, None) if languages is None: @@ -29,7 +31,7 @@ def get(self, request, format=None): class PageList(APIView): """ - List all pages, or create a new page. + List of all pages on this site for a given language. """ def get(self, request, language, format=None): @@ -47,7 +49,8 @@ def get(self, request, language, format=None): class PageDetail(APIView): """ - Retrieve, update or delete a page instance. + Retrieve a page instance. The page instance includes the placeholders and + their links to retrieve dynamic content. """ def get_object(self, site, path): @@ -78,6 +81,8 @@ def get(self, request, language, path="", format=None): class PlaceholderDetail(APIView): + """Placeholder contain the dynamic content.""" + def get_placeholder(self, content_type_id, object_id, slot): try: placeholder = Placeholder.objects.get(