Skip to content

Commit e0c0acd

Browse files
feat: simpler component configuration (#279)
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
1 parent 434ec8a commit e0c0acd

File tree

6 files changed

+37
-12
lines changed

6 files changed

+37
-12
lines changed

djangocms_frontend/cms_plugins.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from cms.plugin_pool import plugin_pool
24

35
from .ui_plugin_base import CMSUIPluginBase
@@ -21,6 +23,8 @@ def update_plugin_pool():
2123
# Loop through the slot plugins associated with the current plugin
2224
for slot_plugin in slot_plugins:
2325
# Add the slot plugin to the global namespace
24-
globals()[slot_plugin.__name__] = slot_plugin
25-
# Register the slot plugin with the plugin pool
26-
plugin_pool.register_plugin(slot_plugin)
26+
# Register slot plugins, checking for name conflicts
27+
for slot_plugin in slot_plugins:
28+
if slot_plugin.__name__ not in plugin_pool.plugins:
29+
globals()[slot_plugin.__name__] = slot_plugin
30+
plugin_pool.register_plugin(slot_plugin)

djangocms_frontend/component_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def plugin_factory(cls) -> type:
174174
if hasattr(cls, "get_render_template")
175175
else {}
176176
),
177-
"__module__": cls.__module__,
177+
"__module__": "djangocms_frontend.cms_plugins",
178178
},
179179
)
180180
return cls._plugin

djangocms_frontend/plugin_tag.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
"ui_item",
2626
)
2727

28-
allowed_plugin_types = tuple(
29-
getattr(importlib.import_module(cls.rsplit(".", 1)[0]), cls.rsplit(".", 1)[-1]) if isinstance(cls, str) else cls
30-
for cls in getattr(settings, "CMS_COMPONENT_PLUGINS", [])
31-
)
32-
3328

3429
def _get_plugindefaults(instance):
3530
defaults = {
@@ -65,7 +60,21 @@ def patch_template(template):
6560
return copied_template if patch else template
6661

6762

63+
def get_plugin_class(settings_string: str | type) -> type:
64+
"""Get the plugin class from the settings string or import it if it's a dotted path."""
65+
if isinstance(settings_string, str):
66+
if "." in settings_string:
67+
# import the class if a dotted path is given - raise can exception if not found
68+
module_name, class_name = settings_string.rsplit(".", 1)
69+
return getattr(importlib.import_module(module_name), class_name)
70+
# Get the plugin class from the plugin pool by its name
71+
return plugin_pool.get_plugin(settings_string)
72+
return settings_string
73+
74+
6875
def setup():
76+
allowed_plugin_types = tuple(get_plugin_class(cls) for cls in getattr(settings, "CMS_COMPONENT_PLUGINS", []))
77+
6978
for plugin in plugin_pool.get_all_plugins():
7079
if not issubclass(plugin, allowed_plugin_types):
7180
continue

docs/source/how-to/use-frontend-as-component.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ repetition.
2222

2323
To make plugins available as components, ensure that the
2424
``CMS_COMPONENT_PLUGINS`` setting in your project's ``settings.py``
25-
includes the necessary plugin classes and their subclasses. This setting
26-
allows you to specify which plugins can be used directly in templates
25+
is a list that includes the necessary plugin names or dotted path to
26+
a plugin parent class. Only plugins named in the listing or their
27+
child classes can be used directly in templates
2728
without creating database entries.
2829

2930
* To include all ``djangocms-frontend`` plugins, use

tests/test_plugin_tag.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,13 @@ def test_non_frontend_plugin(self):
119119
result = template.render({"request": None})
120120

121121
self.assertInHTML(expected_result, result)
122+
123+
def test_autohero_component_registered_for_plugin_tag(self):
124+
from cms.plugin_pool import plugin_pool
125+
from djangocms_frontend.plugin_tag import plugin_tag_pool
126+
127+
# Check that the AutoHero plugin is registered
128+
self.assertIn("AutoHeroPlugin", plugin_pool.plugins)
129+
130+
# Check for the AutoHero plugin registration in the plugin_tag_pool
131+
self.assertIn("autohero", plugin_tag_pool)

tests/test_settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,6 @@
129129

130130
CMS_COMPONENT_PLUGINS = [
131131
"djangocms_frontend.cms_plugins.CMSUIPlugin",
132-
"djangocms_text.cms_plugins.TextPlugin",
132+
"djangocms_frontend.cms_plugins.AutoHeroPlugin",
133+
"TextPlugin",
133134
]

0 commit comments

Comments
 (0)