1
- from typing import Any , Dict , Iterable , Optional , TypeVar
1
+ from typing import Any , Iterable , Optional , TypeVar
2
2
3
3
from django .contrib .sites .shortcuts import get_current_site
4
4
from django .db import models
10
10
11
11
from rest_framework import serializers
12
12
from djangocms_rest .serializers .placeholders import PlaceholderSerializer
13
- from djangocms_rest .serializers .utils .cache import get_placeholder_rest_cache , set_placeholder_rest_cache
13
+ from djangocms_rest .serializers .utils .cache import (
14
+ get_placeholder_rest_cache ,
15
+ set_placeholder_rest_cache ,
16
+ )
14
17
15
18
16
19
base_exclude = {
@@ -54,8 +57,8 @@ def get_auto_model_serializer(model_class: type[ModelType]) -> type:
54
57
55
58
56
59
def serialize_cms_plugin (
57
- instance : Optional [Any ], context : Dict [str , Any ]
58
- ) -> Optional [Dict [str , Any ]]:
60
+ instance : Optional [Any ], context : dict [str , Any ]
61
+ ) -> Optional [dict [str , Any ]]:
59
62
if not instance or not hasattr (instance , "get_plugin_instance" ):
60
63
return None
61
64
@@ -82,7 +85,7 @@ def serialize_cms_plugin(
82
85
83
86
# Template for a collapsable object/list
84
87
OBJ_TEMPLATE = (
85
- ' <details open><summary>{open}</summary>'
88
+ " <details open><summary>{open}</summary>"
86
89
'<div class="indent">{value}</div></details>{close}<span class="sep">,</span>'
87
90
)
88
91
@@ -92,23 +95,21 @@ def serialize_cms_plugin(
92
95
)
93
96
94
97
# Tempalte for a single line key-value pair
95
- SIMPLE_TEMPLATE = (
96
- '<span class="key">"{key}"</span>: {value}<span class="sep">,</span>'
97
- )
98
+ SIMPLE_TEMPLATE = '<span class="key">"{key}"</span>: {value}<span class="sep">,</span>'
99
+
98
100
99
101
def escapestr (s : str ) -> str :
100
102
"""
101
103
Escape a string for safe HTML rendering.
102
104
"""
103
- return escape (s ).replace (' "' , ' \"' ).replace (' \n ' , ' \n' )
105
+ return escape (s ).replace (" "" , " \"" ).replace (" \n " , " \n" )
104
106
105
107
106
108
def highlight_data (json_data : Any ) -> str :
107
109
"""
108
110
Highlight JSON data using Pygments.
109
111
"""
110
112
if isinstance (json_data , str ):
111
- ellipsis = ""
112
113
if len (json_data ) > 60 :
113
114
return f'<span class="str">"<span class="ellipsis">{ escapestr (json_data )} </span>"</span>'
114
115
return f'<span class="str">"{ escapestr (json_data )} "</span>'
@@ -119,15 +120,15 @@ def highlight_data(json_data: Any) -> str:
119
120
if json_data is None :
120
121
return '<span class="null">null</span>'
121
122
if isinstance (json_data , dict ):
122
- return highlight_json (json_data ).get ("value" , "" ) if json_data else '{}'
123
+ return highlight_json (json_data ).get ("value" , "" ) if json_data else "{}"
123
124
if isinstance (json_data , list ):
124
- return highlight_list (json_data ).get ("value" , "" ) if json_data else '[]'
125
+ return highlight_list (json_data ).get ("value" , "" ) if json_data else "[]"
125
126
126
127
return f'<span class="obj">{ json_data } </span>'
127
128
128
129
129
130
def highlight_json (
130
- json_data : Dict [str , Any ], children : Iterable | None = None , field : str = "children"
131
+ json_data : dict [str , Any ], children : Iterable | None = None , field : str = "children"
131
132
) -> dict [str , str ]:
132
133
has_children = children is not None
133
134
if field in json_data :
@@ -137,9 +138,11 @@ def highlight_json(
137
138
DETAILS_TEMPLATE .format (
138
139
key = escape (key ),
139
140
value = highlight_data (value ),
140
- open = '{' if isinstance (value , dict ) else '[' ,
141
- close = '}' if isinstance (value , dict ) else ']' ,
142
- ) if isinstance (value , (dict , list )) and value else SIMPLE_TEMPLATE .format (
141
+ open = "{" if isinstance (value , dict ) else "[" ,
142
+ close = "}" if isinstance (value , dict ) else "]" ,
143
+ )
144
+ if isinstance (value , (dict , list )) and value
145
+ else SIMPLE_TEMPLATE .format (
143
146
key = escape (key ),
144
147
value = highlight_data (value ),
145
148
)
@@ -149,24 +152,24 @@ def highlight_json(
149
152
items .append (
150
153
DETAILS_TEMPLATE .format (
151
154
key = escape (field ),
152
- value = '' .join (children ),
153
- open = '[' ,
154
- close = ']' ,
155
+ value = "" .join (children ),
156
+ open = "[" ,
157
+ close = "]" ,
155
158
)
156
159
)
157
160
return {
158
- "open" : '{' ,
159
- "close" : '}' ,
161
+ "open" : "{" ,
162
+ "close" : "}" ,
160
163
"value" : "<br>" .join (items ),
161
164
}
162
165
163
166
164
167
def highlight_list (json_data : list ) -> dict [str , str ]:
165
168
items = [highlight_data (item ) for item in json_data ]
166
169
return {
167
- "open" : '[' ,
168
- "close" : ']' ,
169
- "value" : '' .join (items ),
170
+ "open" : "[" ,
171
+ "close" : "]" ,
172
+ "value" : "" .join (items ),
170
173
}
171
174
172
175
@@ -175,6 +178,7 @@ class RESTRenderer(ContentRenderer):
175
178
A custom renderer that uses the serialize_cms_plugin function to render
176
179
CMS plugins in a RESTful way.
177
180
"""
181
+
178
182
placeholder_edit_template = "{content}{plugin_js}{placeholder_js}"
179
183
180
184
def render_plugin (
@@ -227,12 +231,12 @@ def render_plugins(
227
231
placeholder , language , context , editable = editable , template = template
228
232
),
229
233
field = "content" ,
230
- )
234
+ ),
231
235
)
232
236
yield "</div>"
233
237
234
238
def get_plugins_and_placeholder_lot (
235
- self , placeholder , language , context , editable = False , template = None
239
+ self , placeholder , language , context , editable = False , template = None
236
240
) -> Iterable [str ]:
237
241
yield from super ().render_plugins (
238
242
placeholder , language , context , editable = editable , template = template
0 commit comments