Skip to content

Commit

Permalink
Optionally include HTML in placeholder responses
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed May 24, 2024
1 parent aa0edcb commit 9a12a36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
8 changes: 4 additions & 4 deletions djangocms_rest/serializers/pageserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion djangocms_rest/serializers/placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
9 changes: 7 additions & 2 deletions djangocms_rest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 9a12a36

Please sign in to comment.