Skip to content

Commit

Permalink
Restructure FIELD_TYPES to prepare it for future expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Aug 5, 2019
1 parent 9f2099e commit debd35d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 23 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Change log
Next version
------------

* Restructured the ``FIELD_TYPES`` data structure to use a dictionary
instead of a tuple to allow for future expansion.


0.14
----
Expand Down
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ In ``your_project.form_designer_config.py`` something like::
from django.utils.translation import ugettext_lazy as _

FIELD_TYPES = [
('text', _('text'), forms.CharField),
('email', _('e-mail address'), forms.EmailField),
{"type": "text", "verbose_name": _("text"), "field": forms.CharField},
{"type": "email", "verbose_name": _("e-mail address"), "field": forms.EmailField},
]


Expand Down
56 changes: 39 additions & 17 deletions form_designer/default_field_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,40 @@


FIELD_TYPES = [
("text", _("text"), forms.CharField),
("email", _("e-mail address"), forms.EmailField),
("longtext", _("long text"), partial(forms.CharField, widget=forms.Textarea)),
("checkbox", _("checkbox"), partial(forms.BooleanField, required=False)),
("select", _("select"), partial(forms.ChoiceField, required=False)),
("radio", _("radio"), partial(forms.ChoiceField, widget=forms.RadioSelect)),
(
"multiple-select",
_("multiple select"),
partial(forms.MultipleChoiceField, widget=forms.CheckboxSelectMultiple),
),
("hidden", _("hidden"), partial(forms.CharField, widget=forms.HiddenInput)),
{"type": "text", "verbose_name": _("text"), "field": forms.CharField},
{"type": "email", "verbose_name": _("e-mail address"), "field": forms.EmailField},
{
"type": "longtext",
"verbose_name": _("long text"),
"field": partial(forms.CharField, widget=forms.Textarea),
},
{
"type": "checkbox",
"verbose_name": _("checkbox"),
"field": partial(forms.BooleanField, required=False),
},
{
"type": "select",
"verbose_name": _("select"),
"field": partial(forms.ChoiceField, required=False),
},
{
"type": "radio",
"verbose_name": _("radio"),
"field": partial(forms.ChoiceField, widget=forms.RadioSelect),
},
{
"type": "multiple-select",
"verbose_name": _("multiple select"),
"field": partial(
forms.MultipleChoiceField, widget=forms.CheckboxSelectMultiple
),
},
{
"type": "hidden",
"verbose_name": _("hidden"),
"field": partial(forms.CharField, widget=forms.HiddenInput),
},
]

# Add recaptcha field if available
Expand All @@ -31,9 +53,9 @@
pass
else:
FIELD_TYPES.append(
(
"recaptcha",
_("recaptcha"),
partial(ReCaptchaField, widget=ReCaptchaV2Checkbox),
)
{
"type": "recaptcha",
"verbose_name": _("recaptcha"),
"field": partial(ReCaptchaField, widget=ReCaptchaV2Checkbox),
}
)
19 changes: 17 additions & 2 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ def process(self, form, request):
"form_designer.default_field_types.FIELD_TYPES",
)
)
for index, field_type in enumerate(FIELD_TYPES[:]):
if isinstance(field_type, dict):
continue
warnings.warn(
"Form designer field type %r still uses the old configuration format."
% (field_type,),
DeprecationWarning,
)
FIELD_TYPES[index] = {
"type": field_type[0],
"verbose_name": field_type[1],
"field": field_type[2],
}


@python_2_unicode_compatible
Expand All @@ -154,7 +167,9 @@ class FormField(models.Model):
title = models.CharField(_("title"), max_length=100)
name = models.CharField(_("name"), max_length=100)
type = models.CharField(
_("type"), max_length=20, choices=[type[:2] for type in FIELD_TYPES]
_("type"),
max_length=20,
choices=[(type["type"], type["verbose_name"]) for type in FIELD_TYPES],
)
choices = models.CharField(
_("choices"), max_length=1024, blank=True, help_text=_("Comma-separated")
Expand Down Expand Up @@ -209,7 +224,7 @@ def get_tuple(value):
return tuple(choices)

def get_type(self, **kwargs):
types = dict((r[0], r[2]) for r in FIELD_TYPES)
types = {type["type"]: type["field"] for type in FIELD_TYPES}
return types[self.type](**kwargs)

def add_formfield(self, fields, form):
Expand Down
4 changes: 2 additions & 2 deletions tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ def test_admin(self):
data["fields-{}-ordering".format(i)] = (i + 1) * 10
data["fields-{}-title".format(i)] = "title-{}".format(i)
data["fields-{}-name".format(i)] = "name-{}".format(i)
data["fields-{}-type".format(i)] = FIELD_TYPES[i][0]
data["fields-{}-type".format(i)] = FIELD_TYPES[i]["type"]
data["fields-{}-choices".format(i)] = ""

# Validation failure because of missing choices
response = self.client.post("/admin/form_designer/form/add/", data)
self.assertEqual(response.status_code, 200)

for i in range(7):
if FIELD_TYPES[i][0] in {"select", "radio", "multiple-select"}:
if FIELD_TYPES[i]["type"] in {"select", "radio", "multiple-select"}:
data["fields-{}-choices".format(i)] = "a,b,c,d"

data["fields-0-choices"] = "invalid"
Expand Down

0 comments on commit debd35d

Please sign in to comment.