Skip to content

Commit 204e57a

Browse files
committed
Add slot tests
1 parent 7253a9e commit 204e57a

File tree

6 files changed

+54
-7
lines changed

6 files changed

+54
-7
lines changed

djangocms_frontend/component_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def slot_plugin_factory(cls) -> list[type]:
195195
"edit_disabled": True,
196196
"is_local": False,
197197
"show_add_form": False,
198-
"parent_classes": cls.__name__ + "Plugin",
198+
"parent_classes": [cls.__name__ + "Plugin"],
199199
"render_template": cls.slot_template,
200200
**slot.kwargs,
201201
},

djangocms_frontend/component_pool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def scan_templates_for_component_declaration(self, templates: list[tuple[str, st
8383
fields = context["_cms_components"].get("fields", [])
8484
if len(cms_component) == 1:
8585
components.append(self.component_factory(module, cms_component[0], fields, template_name))
86-
elif len(cms_component) > 1:
86+
elif len(cms_component) > 1: # pragma: no cover
8787
raise ValueError(f"Multiple cms_component tags found in {template_name}")
8888
return components
8989

@@ -93,7 +93,7 @@ class Components:
9393
_discovered: bool = False
9494

9595
def register(self, component):
96-
if component.__name__ in self._registry:
96+
if component.__name__ in self._registry: # pragma: no cover
9797
warnings.warn(f"Component {component.__name__} already registered", stacklevel=2)
9898
return component
9999
self._registry[component.__name__] = component.get_registration()

djangocms_frontend/templatetags/frontend.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class RenderChildPluginsTag(Tag):
219219
{% endchildplugins %}
220220
221221
Keyword arguments:
222-
name -- the name of the placeholder
222+
instance -- instance of the plugin whose children are to be rendered
223223
plugin_type -- optional argument which if given will result in filtering
224224
the direct child plugin types that are rendered.
225225
or -- optional argument which if given will make the template tag a block
@@ -230,7 +230,7 @@ class RenderChildPluginsTag(Tag):
230230
options = Options(
231231
# PlaceholderOptions parses until the "endchildplugins" tag is found if
232232
# the "or" option is given
233-
Argument("instance", required=True),
233+
Argument("instance", required=False),
234234
Argument("plugin_type", required=False),
235235
Argument("verbose_name", required=False),
236236
blocks=[("endchildplugins", "nodelist")],
@@ -254,6 +254,9 @@ def render_tag(self, context, instance, plugin_type, verbose_name, nodelist):
254254
kwargs["slots"] = [(plugin_type, verbose_name)]
255255
context["_cms_components"]["cms_component"][0] = (args, kwargs)
256256

257+
if not instance:
258+
instance = context.get("instance", None)
259+
257260
context.push()
258261
context["parent"] = instance
259262
content = []

tests/test_app/templates/test_app/cms_components/hero.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1 class="max-w-2xl mb-4 text-4xl font-extrabold tracking-tight leading-none md
1616
<p class="max-w-2xl mb-6 font-light text-gray-500 lg:mb-8 md:text-lg lg:text-xl dark:text-gray-400">
1717
{{ slogan }}
1818
</p>
19-
{% childplugins instance %}
19+
{% childplugins %}
2020
Add buttons here
2121
{% endchildplugins %}
2222
</div>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% load cms_tags frontend cms_component %}
2+
3+
{# Declare component - template tags are evaluated at project startup and will render empty #}
4+
{% cms_component "AutoHeroWithSlots" name=_("My Slotted Hero Auto Component") %}
5+
{% field "title" forms.CharField required=True %}
6+
{% field "slogan" forms.CharField required=True widget=forms.Textarea %}
7+
{% field "hero_image" ImageFormField required=True help_text=_("At least 1024px wide image") %}
8+
9+
{# Actual template - when rendering declared fields are available in the context #}
10+
<section class="bg-white dark:bg-gray-900">
11+
<div class="grid max-w-screen-xl px-4 py-8 mx-auto lg:gap-8 xl:gap-0 lg:py-16 lg:grid-cols-12">
12+
<div class="mr-auto place-self-center lg:col-span-7">
13+
<h1 class="max-w-2xl mb-4 text-4xl font-extrabold tracking-tight leading-none md:text-5xl xl:text-6xl dark:text-white">
14+
{{ title }}
15+
</h1>
16+
<p class="max-w-2xl mb-6 font-light text-gray-500 lg:mb-8 md:text-lg lg:text-xl dark:text-gray-400">
17+
{{ slogan }}
18+
</p>
19+
{% childplugins instance "buttons" _("Buttons") %}
20+
Add buttons here
21+
{% endchildplugins %}
22+
</div>
23+
<div class="hidden lg:mt-0 lg:col-span-5 lg:flex">
24+
<img src="{{ hero_image.url }}">
25+
</div>
26+
</div>
27+
</section>

tests/test_autocomponent.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,21 @@ def test_split_template_tag(self):
4747
self.assertEqual(split("hero.html"), ["hero.html"])
4848
self.assertEqual(split("Mixin1|Mixin2"), ["Mixin1", "Mixin2"])
4949
self.assertEqual(split("hero.html"), ["hero.html"])
50-
self.assertEqual(split("hero.html, Mixin1, Mixin2", ", "), ["hero.html", "Mixin1", "Mixin2"])
50+
self.assertEqual(split("hero.html, Mixin1, Mixin2", ", "), ["hero.html", "Mixin1", "Mixin2"])
51+
52+
def test_autocomponents_slots_are_created(self):
53+
from cms.plugin_pool import plugin_pool
54+
55+
self.assertIn("AutoHeroWithSlotsPlugin", plugin_pool.plugins)
56+
57+
plugin = plugin_pool.get_plugin("AutoHeroWithSlotsPlugin")
58+
model = plugin.model
59+
60+
self.assertEqual(model.__name__, "AutoHeroWithSlots")
61+
self.assertTrue(plugin.allow_children)
62+
self.assertEqual(plugin.child_classes, ["AutoHeroWithSlotsButtonsPlugin"])
63+
64+
self.assertIn("AutoHeroWithSlotsButtonsPlugin", plugin_pool.plugins)
65+
slot_plugin = plugin_pool.get_plugin("AutoHeroWithSlotsButtonsPlugin")
66+
67+
self.assertEqual(slot_plugin.parent_classes, ["AutoHeroWithSlotsPlugin"])

0 commit comments

Comments
 (0)