11import json
2- from typing import Any , TypeVar
2+ from typing import Any
33from collections .abc import Iterable
44
55from django .contrib .sites .shortcuts import get_current_site
66from django .core .exceptions import ValidationError
77from django .core .validators import URLValidator
8- from django .db import models
98from django .utils .html import escape , mark_safe
109
1110from cms .models import Placeholder
1211from cms .plugin_rendering import ContentRenderer
1312from cms .utils .plugins import get_plugins
1413
1514from djangocms_rest .serializers .placeholders import PlaceholderSerializer
16- from djangocms_rest .serializers .plugins import GenericPluginSerializer , base_exclude
15+ from djangocms_rest .serializers .plugins import (
16+ get_auto_model_serializer ,
17+ resolve_plugin_serializer ,
18+ )
1719from djangocms_rest .serializers .utils .cache import (
1820 get_placeholder_rest_cache ,
1921 set_placeholder_rest_cache ,
2022)
2123
2224
23- ModelType = TypeVar ("ModelType" , bound = models .Model )
24-
25-
26- def get_auto_model_serializer (model_class : type [ModelType ]) -> type :
27- """
28- Build (once) a generic ModelSerializer subclass that excludes
29- common CMS bookkeeping fields.
30- """
31-
32- opts = model_class ._meta
33- real_fields = {f .name for f in opts .get_fields ()}
34- exclude = tuple (base_exclude & real_fields )
35-
36- meta_class = type (
37- "Meta" ,
38- (),
39- {
40- "model" : model_class ,
41- "exclude" : exclude ,
42- },
43- )
44- return type (
45- f"{ model_class .__name__ } AutoSerializer" ,
46- (GenericPluginSerializer ,),
47- {
48- "Meta" : meta_class ,
49- },
50- )
51-
52-
53- def serialize_cms_plugin (
54- instance : Any | None , context : dict [str , Any ]
55- ) -> dict [str , Any ] | None :
25+ def serialize_cms_plugin (instance : Any | None , context : dict [str , Any ]) -> dict [str , Any ] | None :
5626 if not instance or not hasattr (instance , "get_plugin_instance" ):
5727 return None
5828 plugin_instance , plugin = instance .get_plugin_instance ()
5929
6030 model_cls = plugin_instance .__class__
61- serializer_cls = getattr (plugin , "serializer_class" , None )
31+ serializer_cls = resolve_plugin_serializer (plugin , model_cls )
6232 serializer_cls = serializer_cls or get_auto_model_serializer (model_cls )
63- plugin .__class__ .serializer_class = serializer_cls
6433
6534 return serializer_cls (plugin_instance , context = context ).data
6635
@@ -72,10 +41,7 @@ def serialize_cms_plugin(
7241)
7342
7443# Template for a collapsable object/list
75- OBJ_TEMPLATE = (
76- "<details open><summary>{open}</summary>"
77- '<div class="indent">{value}</div></details>{close}'
78- )
44+ OBJ_TEMPLATE = "<details open><summary>{open}</summary>" '<div class="indent">{value}</div></details>{close}'
7945
8046# Tempalte for a non-collasable object/list
8147FIXED_TEMPLATE = '{open}<div class="indent">{value}</div>{close}'
@@ -207,17 +173,13 @@ class RESTRenderer(ContentRenderer):
207173
208174 placeholder_edit_template = "{content}{plugin_js}{placeholder_js}"
209175
210- def render_plugin (
211- self , instance , context , placeholder = None , editable : bool = False
212- ):
176+ def render_plugin (self , instance , context , placeholder = None , editable : bool = False ):
213177 """
214178 Render a CMS plugin instance using the serialize_cms_plugin function.
215179 """
216180 data = serialize_cms_plugin (instance , context ) or {}
217181 children = [
218- self .render_plugin (
219- child , context , placeholder = placeholder , editable = editable
220- )
182+ self .render_plugin (child , context , placeholder = placeholder , editable = editable )
221183 for child in getattr (instance , "child_plugin_instances" , [])
222184 ] or None
223185 content = OBJ_TEMPLATE .format (** highlight_json (data , children = children ))
@@ -229,15 +191,11 @@ def render_plugin(
229191 content = content ,
230192 position = instance .position ,
231193 )
232- placeholder_cache = self ._rendered_plugins_by_placeholder .setdefault (
233- placeholder .pk , {}
234- )
194+ placeholder_cache = self ._rendered_plugins_by_placeholder .setdefault (placeholder .pk , {})
235195 placeholder_cache .setdefault ("plugins" , []).append (instance )
236196 return mark_safe (content )
237197
238- def render_plugins (
239- self , placeholder , language , context , editable = False , template = None
240- ):
198+ def render_plugins (self , placeholder , language , context , editable = False , template = None ):
241199 yield "<div class='rest-placeholder' data-placeholder='{placeholder}' data-language='{language}'>" .format (
242200 placeholder = placeholder .slot ,
243201 language = language ,
@@ -265,9 +223,7 @@ def render_plugins(
265223 def get_plugins_and_placeholder_lot (
266224 self , placeholder , language , context , editable = False , template = None
267225 ) -> Iterable [str ]:
268- yield from super ().render_plugins (
269- placeholder , language , context , editable = editable , template = template
270- )
226+ yield from super ().render_plugins (placeholder , language , context , editable = editable , template = template )
271227
272228 def serialize_placeholder (self , placeholder , context , language , use_cache = True ):
273229 context .update ({"request" : self .request })
@@ -312,9 +268,7 @@ def serialize_placeholder(self, placeholder, context, language, use_cache=True):
312268
313269 return plugin_content
314270
315- def serialize_plugins (
316- self , placeholder : Placeholder , language : str , context : dict
317- ) -> list :
271+ def serialize_plugins (self , placeholder : Placeholder , language : str , context : dict ) -> list :
318272 plugins = get_plugins (
319273 self .request ,
320274 placeholder = placeholder ,
@@ -327,9 +281,7 @@ def serialize_children(child_plugins):
327281 for child_plugin in child_plugins :
328282 child_content = serialize_cms_plugin (child_plugin , context )
329283 if getattr (child_plugin , "child_plugin_instances" , None ):
330- child_content ["children" ] = serialize_children (
331- child_plugin .child_plugin_instances
332- )
284+ child_content ["children" ] = serialize_children (child_plugin .child_plugin_instances )
333285 if child_content :
334286 children_list .append (child_content )
335287 return children_list
@@ -338,10 +290,7 @@ def serialize_children(child_plugins):
338290 for plugin in plugins :
339291 plugin_content = serialize_cms_plugin (plugin , context )
340292 if getattr (plugin , "child_plugin_instances" , None ):
341- plugin_content ["children" ] = serialize_children (
342- plugin .child_plugin_instances
343- )
293+ plugin_content ["children" ] = serialize_children (plugin .child_plugin_instances )
344294 if plugin_content :
345295 results .append (plugin_content )
346296 return results
347-
0 commit comments