Skip to content

Commit cb779b9

Browse files
committed
Add tests
1 parent 58c38c1 commit cb779b9

File tree

4 files changed

+68
-8
lines changed

4 files changed

+68
-8
lines changed

djangocms_frontend/component_pool.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import warnings
55

6+
from django import forms
67
from django.apps import apps
78
from django.template import loader
89
from django.utils.module_loading import autodiscover_modules
@@ -54,6 +55,8 @@ def component_factory(component: tuple, fields: list[tuple], template: str) -> C
5455
(name,) = args
5556

5657
kwargs["render_template"] = template
58+
print(kwargs)
59+
print(fields)
5760
meta = type("Meta", (), kwargs)
5861
cls = type(
5962
name,
@@ -64,7 +67,7 @@ def component_factory(component: tuple, fields: list[tuple], template: str) -> C
6467
**{
6568
# Django template engine instantiates objects -- re-instantiate them here
6669
args[0]: args[1].__class__(**kwargs)
67-
for args, kwargs in fields
70+
for args, kwargs in fields if isinstance(args[1], forms.Field)
6871
},
6972
},
7073
)

djangocms_frontend/fields.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django import forms
2+
from django.apps import apps
23
from django.core.exceptions import ValidationError
34
from django.db import models
45
from django.utils.safestring import mark_safe
@@ -9,6 +10,19 @@
910
from .helpers import first_choice
1011

1112

13+
if apps.is_installed("djangocms_text"):
14+
from djangocms_text.fields import HTMLFormField # noqa F401
15+
16+
HTMLsanitized = True
17+
elif apps.is_installed("djangocms_text_ckeditor"):
18+
from djangocms_text_ckeditor.fields import HTMLFormField # noqa F401
19+
20+
HTMLsanitized = True
21+
else:
22+
HTMLFormField = forms.CharField
23+
HTMLsanitized = False
24+
25+
1226
class TemplateChoiceMixin:
1327
"""Mixin that hides the template field if only one template is available and is selected"""
1428

@@ -170,10 +184,3 @@ def __init__(self, *args, **kwargs):
170184
super().__init__(*args, **kwargs)
171185

172186

173-
try:
174-
from djangocms_text_ckeditor.fields import HTMLFormField # noqa
175-
176-
HTMLsanitized = True
177-
except ModuleNotFoundError:
178-
HTMLFormField = forms.CharField
179-
HTMLsanitized = False
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 "AutoHero" name=_("My 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 %}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from cms.test_utils.testcases import CMSTestCase
2+
3+
from tests.fixtures import TestFixture
4+
5+
6+
class AutoComponentTestCase(TestFixture, CMSTestCase):
7+
def test_autocomponents_are_auto_detected(self):
8+
from cms.plugin_pool import plugin_pool
9+
10+
self.assertIn("AutoHeroPlugin", plugin_pool.plugins)
11+
12+
plugin = plugin_pool.get_plugin("AutoHeroPlugin")
13+
model = plugin.model
14+
form = plugin.form
15+
16+
self.assertEqual(plugin.name, "My Hero Auto Component")
17+
self.assertEqual(model.__name__, "AutoHero")
18+
self.assertIn("title", form.base_fields)
19+
self.assertIn("slogan", form.base_fields)
20+
self.assertIn("hero_image", form.base_fields)
21+
self.assertIn("config", form.base_fields) # Inherited from djangocms_frontend.models.FrontendUIItem
22+
23+
self.assertTrue(plugin.allow_children)

0 commit comments

Comments
 (0)