Skip to content

Commit 9cf117d

Browse files
committed
Fix overwrite side-eddect
1 parent badc1a6 commit 9cf117d

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

djangocms_frontend/component_pool.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ class CMSAutoComponentDiscovery:
3535
}
3636

3737
def __init__(self, register_to):
38+
self.default_field_context.update(settings.COMPONENT_FIELDS)
3839
templates = find_cms_component_templates()
3940
auto_components = self.scan_templates_for_component_declaration(templates)
4041
for component in auto_components:
4142
register_to.register(component)
4243

4344
def get_field_context(self) -> dict:
4445
field_context = {}
45-
self.default_field_context.update(settings.COMPONENT_FIELDS)
4646
for key, value in self.default_field_context.items():
4747
if apps.is_installed(key) and "." in value:
4848
module, field_name = value.rsplit(".", 1)
@@ -56,7 +56,7 @@ def component_factory(module, component: tuple, fields: list[tuple], template: s
5656

5757
kwargs["render_template"] = template
5858
meta = type("Meta", (), kwargs)
59-
cls = type(
59+
return type(
6060
name,
6161
(CMSFrontendComponent,),
6262
{
@@ -70,22 +70,20 @@ def component_factory(module, component: tuple, fields: list[tuple], template: s
7070
},
7171
},
7272
)
73-
return cls
7473

7574
def scan_templates_for_component_declaration(self, templates: list[tuple[str, str]]) -> list[CMSFrontendComponent]:
7675
from django.forms import fields
7776

78-
components = []
7977
field_context = self.get_field_context()
8078
for module, template_name in templates:
8179
# Create a new context for each template
8280
context = {"_cms_components": defaultdict(list), "forms": fields, "instance": {}, **field_context}
8381
try:
8482
loader.render_to_string(template_name, context)
8583
cms_component = context["_cms_components"].get("cms_component", [])
86-
fields = context["_cms_components"].get("fields", [])
84+
discovered_fields = context["_cms_components"].get("fields", [])
8785
if len(cms_component) == 1:
88-
components.append(self.component_factory(module, cms_component[0], fields, template_name))
86+
yield self.component_factory(module, cms_component[0], discovered_fields, template_name)
8987
elif len(cms_component) > 1: # pragma: no cover
9088
raise ValueError(f"Multiple cms_component tags found in {template_name}")
9189
except Exception: # pragma: no cover
@@ -97,7 +95,6 @@ def scan_templates_for_component_declaration(self, templates: list[tuple[str, st
9795
f"Error rendering template {template_name} to scan for cms frontend components", exc_info=True
9896
)
9997
pass
100-
return components
10198

10299

103100
class Components:

tests/test_autocomponent.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,31 @@ def test_render_autocomponent(self):
4141
self.assertContains(response, "My Hero")
4242
self.assertContains(response, "My Hero Slogan")
4343

44-
def test_split_template_tag(self):
45-
from djangocms_frontend.templatetags.cms_component import split
46-
47-
self.assertEqual(split("hero.html"), ["hero.html"])
48-
self.assertEqual(split("Mixin1|Mixin2"), ["Mixin1", "Mixin2"])
49-
self.assertEqual(split("hero.html"), ["hero.html"])
50-
self.assertEqual(split("hero.html, Mixin1, Mixin2", ", "), ["hero.html", "Mixin1", "Mixin2"])
51-
5244
def test_autocomponents_slots_are_created(self):
5345
from cms.plugin_pool import plugin_pool
5446

5547
self.assertIn("AutoHeroWithSlotsPlugin", plugin_pool.plugins)
5648

5749
plugin = plugin_pool.get_plugin("AutoHeroWithSlotsPlugin")
5850
model = plugin.model
59-
51+
form = plugin.form
6052
self.assertEqual(model.__name__, "AutoHeroWithSlots")
53+
self.assertIn("title", form.declared_fields)
54+
self.assertIn("slogan", form.declared_fields)
55+
self.assertIn("hero_image", form.declared_fields)
56+
self.assertIn("config", form.declared_fields) # Inherited from djangocms_frontend.models.FrontendUIItem
6157
self.assertTrue(plugin.allow_children)
6258
self.assertEqual(plugin.child_classes, ["AutoHeroWithSlotsButtonsPlugin"])
6359

6460
self.assertIn("AutoHeroWithSlotsButtonsPlugin", plugin_pool.plugins)
6561
slot_plugin = plugin_pool.get_plugin("AutoHeroWithSlotsButtonsPlugin")
6662

6763
self.assertEqual(slot_plugin.parent_classes, ["AutoHeroWithSlotsPlugin"])
64+
65+
def test_split_template_tag(self):
66+
from djangocms_frontend.templatetags.cms_component import split
67+
68+
self.assertEqual(split("hero.html"), ["hero.html"])
69+
self.assertEqual(split("Mixin1|Mixin2"), ["Mixin1", "Mixin2"])
70+
self.assertEqual(split("hero.html"), ["hero.html"])
71+
self.assertEqual(split("hero.html, Mixin1, Mixin2", ", "), ["hero.html", "Mixin1", "Mixin2"])

0 commit comments

Comments
 (0)