|
24 | 24 | register = template.Library() |
25 | 25 |
|
26 | 26 |
|
| 27 | +def is_registering_component(context: template.Context) -> bool: |
| 28 | + return ( |
| 29 | + "_cms_components" in context |
| 30 | + and "cms_component" in context["_cms_components"] |
| 31 | + and len(context["_cms_components"]["cms_component"]) == 1 |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +def update_component_properties(context: template.Context, key: str, value: typing.Any, append: bool = False) -> None: |
| 36 | + """"Adds or appends the value to the property "key" of a component during delcaration""" |
| 37 | + args, kwargs = context["_cms_components"]["cms_component"][0] |
| 38 | + if append: |
| 39 | + # Populate slots with plugin_type and verbose_name |
| 40 | + if key in kwargs: |
| 41 | + kwargs[key].append(value) |
| 42 | + else: |
| 43 | + kwargs[key] = [value] |
| 44 | + else: |
| 45 | + kwargs[key] = value |
| 46 | + context["_cms_components"]["cms_component"][0] = (args, kwargs) |
| 47 | + |
| 48 | + |
| 49 | + |
27 | 50 | @register.simple_tag |
28 | 51 | def get_attributes(attribute_field, *add_classes): |
29 | 52 | """Joins a list of classes with an attributes field and returns all html attributes""" |
@@ -237,21 +260,13 @@ class RenderChildPluginsTag(Tag): |
237 | 260 | ) |
238 | 261 |
|
239 | 262 | def render_tag(self, context, instance, plugin_type, verbose_name, nodelist): |
240 | | - if ( |
241 | | - "_cms_components" in context |
242 | | - and "cms_component" in context["_cms_components"] |
243 | | - and len(context["_cms_components"]["cms_component"]) == 1 |
244 | | - ): |
| 263 | + if is_registering_component(context): |
245 | 264 | args, kwargs = context["_cms_components"]["cms_component"][0] |
246 | 265 | if plugin_type is None: |
247 | 266 | # If tag is used, default to allow_children=True |
248 | 267 | kwargs.setdefault("allow_children", True) |
249 | 268 | if plugin_type and verbose_name: |
250 | | - # Populate slots with plugin_type and verbose_name |
251 | | - if "slots" in kwargs: |
252 | | - kwargs["slots"].append((plugin_type, verbose_name)) |
253 | | - else: |
254 | | - kwargs["slots"] = [(plugin_type, verbose_name)] |
| 269 | + update_component_properties(context, "slots", (plugin_type, verbose_name), append=True) |
255 | 270 | context["_cms_components"]["cms_component"][0] = (args, kwargs) |
256 | 271 |
|
257 | 272 | if not instance: |
@@ -290,16 +305,19 @@ class InlineField(CMSEditableObject): |
290 | 305 | Argument("varname", required=False, resolve=False), |
291 | 306 | ) |
292 | 307 |
|
293 | | - def render_tag(self, context, **kwargs): |
294 | | - if ( |
| 308 | + def render_tag(self, context, instance, attribute, **kwargs): |
| 309 | + if is_registering_component(context) and attribute: |
| 310 | + update_component_properties(context, "frontend_editable_fields", attribute, append=True) |
| 311 | + |
| 312 | + elif ( |
295 | 313 | context["request"].session.get("inline_editing", True) |
296 | | - and isinstance(kwargs["instance"], CMSPlugin) |
297 | | - and kwargs["instance"].pk |
| 314 | + and isinstance(instance, CMSPlugin) |
| 315 | + and instance.pk |
298 | 316 | ): |
299 | 317 | # Only allow inline field to be rendered if inline editing is active and the instance is a CMSPlugin |
300 | 318 | # DummyPlugins of the ``plugin`` tag are cannot be edited (they have no pk in their model class) |
301 | | - kwargs["edit_fields"] = kwargs["attribute"] |
302 | | - return super().render_tag(context, **kwargs) |
| 319 | + kwargs["edit_fields"] = attribute |
| 320 | + return super().render_tag(context, instance=instance, attribute=attribute, **kwargs) |
303 | 321 | else: |
304 | 322 | return getattr(kwargs["instance"], kwargs["attribute"], "") |
305 | 323 |
|
|
0 commit comments