Skip to content

Commit 492ffff

Browse files
committed
Fix linting
1 parent 61e6153 commit 492ffff

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

djangocms_rest/plugin_rendering.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Iterable, Optional, TypeVar
1+
from typing import Any, Iterable, Optional, TypeVar
22

33
from django.contrib.sites.shortcuts import get_current_site
44
from django.db import models
@@ -10,7 +10,10 @@
1010

1111
from rest_framework import serializers
1212
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+
)
1417

1518

1619
base_exclude = {
@@ -54,8 +57,8 @@ def get_auto_model_serializer(model_class: type[ModelType]) -> type:
5457

5558

5659
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]]:
5962
if not instance or not hasattr(instance, "get_plugin_instance"):
6063
return None
6164

@@ -82,7 +85,7 @@ def serialize_cms_plugin(
8285

8386
# Template for a collapsable object/list
8487
OBJ_TEMPLATE = (
85-
'<details open><summary>{open}</summary>'
88+
"<details open><summary>{open}</summary>"
8689
'<div class="indent">{value}</div></details>{close}<span class="sep">,</span>'
8790
)
8891

@@ -92,23 +95,21 @@ def serialize_cms_plugin(
9295
)
9396

9497
# 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+
98100

99101
def escapestr(s: str) -> str:
100102
"""
101103
Escape a string for safe HTML rendering.
102104
"""
103-
return escape(s).replace('&quot;', '&bsol;&quot;').replace('\n', '&bsol;n')
105+
return escape(s).replace("&quot;", "&bsol;&quot;").replace("\n", "&bsol;n")
104106

105107

106108
def highlight_data(json_data: Any) -> str:
107109
"""
108110
Highlight JSON data using Pygments.
109111
"""
110112
if isinstance(json_data, str):
111-
ellipsis = ""
112113
if len(json_data) > 60:
113114
return f'<span class="str">"<span class="ellipsis">{escapestr(json_data)}</span>"</span>'
114115
return f'<span class="str">"{escapestr(json_data)}"</span>'
@@ -119,15 +120,15 @@ def highlight_data(json_data: Any) -> str:
119120
if json_data is None:
120121
return '<span class="null">null</span>'
121122
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 "{}"
123124
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 "[]"
125126

126127
return f'<span class="obj">{json_data}</span>'
127128

128129

129130
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"
131132
) -> dict[str, str]:
132133
has_children = children is not None
133134
if field in json_data:
@@ -137,9 +138,11 @@ def highlight_json(
137138
DETAILS_TEMPLATE.format(
138139
key=escape(key),
139140
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(
143146
key=escape(key),
144147
value=highlight_data(value),
145148
)
@@ -149,24 +152,24 @@ def highlight_json(
149152
items.append(
150153
DETAILS_TEMPLATE.format(
151154
key=escape(field),
152-
value=''.join(children),
153-
open='[',
154-
close=']',
155+
value="".join(children),
156+
open="[",
157+
close="]",
155158
)
156159
)
157160
return {
158-
"open": '{',
159-
"close": '}',
161+
"open": "{",
162+
"close": "}",
160163
"value": "<br>".join(items),
161164
}
162165

163166

164167
def highlight_list(json_data: list) -> dict[str, str]:
165168
items = [highlight_data(item) for item in json_data]
166169
return {
167-
"open": '[',
168-
"close": ']',
169-
"value": ''.join(items),
170+
"open": "[",
171+
"close": "]",
172+
"value": "".join(items),
170173
}
171174

172175

@@ -175,6 +178,7 @@ class RESTRenderer(ContentRenderer):
175178
A custom renderer that uses the serialize_cms_plugin function to render
176179
CMS plugins in a RESTful way.
177180
"""
181+
178182
placeholder_edit_template = "{content}{plugin_js}{placeholder_js}"
179183

180184
def render_plugin(
@@ -227,12 +231,12 @@ def render_plugins(
227231
placeholder, language, context, editable=editable, template=template
228232
),
229233
field="content",
230-
)
234+
),
231235
)
232236
yield "</div>"
233237

234238
def get_plugins_and_placeholder_lot(
235-
self, placeholder, language, context, editable=False, template=None
239+
self, placeholder, language, context, editable=False, template=None
236240
) -> Iterable[str]:
237241
yield from super().render_plugins(
238242
placeholder, language, context, editable=editable, template=template

0 commit comments

Comments
 (0)