Skip to content

Commit

Permalink
fix: Common base class for custom components created
Browse files Browse the repository at this point in the history
  • Loading branch information
fsbraun committed Feb 3, 2025
1 parent b85bf8c commit ccebb38
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion djangocms_frontend/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from djangocms_frontend import settings

from .attributes import AttributesMixin, AttributesFormMixin
from .attributes import AttributesFormMixin, AttributesMixin
from .title import TitleFormMixin, TitleMixin

__common = {
Expand Down
8 changes: 4 additions & 4 deletions djangocms_frontend/component_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.utils.translation import gettext_lazy as _
from entangled.forms import EntangledModelForm

from .ui_plugin_base import CMSUIPluginBase
from .ui_plugin_base import CMSUIComponent


def _import_or_empty(module, name):
Expand Down Expand Up @@ -134,7 +134,7 @@ def plugin_factory(cls) -> type:
(
*mixins,
*cls._plugin_mixins,
CMSUIPluginBase,
CMSUIComponent,
),
{
"name": getattr(cls._component_meta, "name", cls.__name__),
Expand All @@ -143,7 +143,7 @@ def plugin_factory(cls) -> type:
"form": cls.admin_form_factory(),
"allow_children": slots or getattr(cls._component_meta, "allow_children", False),
"child_classes": getattr(cls._component_meta, "child_classes", []) + list(slots.keys()),
"render_template": getattr(cls._component_meta, "render_template", CMSUIPluginBase.render_template),
"render_template": getattr(cls._component_meta, "render_template", CMSUIComponent.render_template),
"fieldsets": getattr(cls, "fieldsets", cls._generate_fieldset()),
"change_form_template": "djangocms_frontend/admin/base.html",
"slots": slots,
Expand Down Expand Up @@ -207,7 +207,7 @@ def get_short_description(self) -> str:
def save_model(self, request, obj, form: forms.Form, change: bool) -> None:
"""Auto-creates slot plugins upon creation of component plugin instance"""

super(CMSUIPluginBase, self).save_model(request, obj, form, change)
super(CMSUIComponent, self).save_model(request, obj, form, change)
if not change:
for slot in self.slots.keys():
add_plugin(obj.placeholder, slot, obj.language, target=obj)
4 changes: 4 additions & 0 deletions djangocms_frontend/ui_plugin_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ def _get_object_for_single_field(self, object_id, language):
from .models import FrontendUIItem

return FrontendUIItem.objects.get(pk=object_id)


class CMSUIComponent(CMSUIPluginBase):
pass
2 changes: 1 addition & 1 deletion docs/source/custom_components.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.. _custom_components: Custom Components
.. _custom_components:

#################
Custom Components
Expand Down
17 changes: 11 additions & 6 deletions docs/source/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,17 @@ repetition.

.. note::

To make plugins available as components, ensure that the
``CMS_COMPONENT_PLUGINS`` setting in your project's ``settings.py``
includes the necessary plugin classes and their subclasses. This setting
allows you to specify which plugins can be used directly in templates
without creating database entries. To include all djangocms-frontend
plugins, use ``djangocms_frontend.cms_plugins.CMSUIPlugin`` in the setting.
To make plugins available as components, ensure that the
``CMS_COMPONENT_PLUGINS`` setting in your project's ``settings.py``
includes the necessary plugin classes and their subclasses. This setting
allows you to specify which plugins can be used directly in templates
without creating database entries.

* To include all djangocms-frontend plugins, use
``djangocms_frontend.cms_plugins.CMSUIPlugin`` in the setting.

* To include all :ref:`custom_components`, use
``djangocms_frontend.cms_plugins.CMSUIComponent`` in the setting.

To use a frontend plugin in a template you need to load the ``frontend`` tags
and then use the ``plugin`` template tag to render a frontend plugin.
Expand Down
3 changes: 2 additions & 1 deletion tests/test_plugin_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ def test_non_existing_plugin(self):
This should not be rendered.
{% endplugin %}
""")
expected_result = "<!-- Plugin \"nonexisting\" not found in pool for plugins usable with {% plugin %} -->"
expected_result = ('<!-- To use "nonexisting" with the {% plugin %} template tag, add its plugin class to the '
'CMS_COMPONENT_PLUGINS setting -->')

result = template.render({"request": None})

Expand Down

0 comments on commit ccebb38

Please sign in to comment.