1 from __future__ import unicode_literals
3 from django.core.exceptions import ImproperlyConfigured
4 from django import forms
5 from django.forms.extras import SelectDateWidget
6 from django.utils.translation import ugettext_lazy as _
8 from forms_builder.forms.settings import USE_HTML5, EXTRA_FIELDS, EXTRA_WIDGETS
9 from forms_builder.forms.utils import html5_field, import_attr
12 # Constants for all available field types.
29 # Names for all available field types.
31 (TEXT, _("Single line text")),
32 (TEXTAREA, _("Multi line text")),
34 (NUMBER, _("Number")),
36 (CHECKBOX, _("Check box")),
37 (CHECKBOX_MULTIPLE, _("Check boxes")),
38 (SELECT, _("Drop down")),
39 (SELECT_MULTIPLE, _("Multi select")),
40 (RADIO_MULTIPLE, _("Radio buttons")),
41 (FILE, _("File upload")),
43 (DATE_TIME, _("Date/time")),
44 (DOB, _("Date of birth")),
45 (HIDDEN, _("Hidden")),
48 # Field classes for all available field types.
50 TEXT: forms.CharField,
51 TEXTAREA: forms.CharField,
52 EMAIL: forms.EmailField,
53 CHECKBOX: forms.BooleanField,
54 CHECKBOX_MULTIPLE: forms.MultipleChoiceField,
55 SELECT: forms.ChoiceField,
56 SELECT_MULTIPLE: forms.MultipleChoiceField,
57 RADIO_MULTIPLE: forms.ChoiceField,
58 FILE: forms.FileField,
59 DATE: forms.DateField,
60 DATE_TIME: forms.DateTimeField,
62 HIDDEN: forms.CharField,
63 NUMBER: forms.FloatField,
67 # Widgets for field types where a specialised widget is required.
69 TEXTAREA: forms.Textarea,
70 CHECKBOX_MULTIPLE: forms.CheckboxSelectMultiple,
71 RADIO_MULTIPLE: forms.RadioSelect,
72 DATE: SelectDateWidget,
73 DOB: SelectDateWidget,
74 HIDDEN: forms.HiddenInput,
77 # Some helper groupings of field types.
78 CHOICES = (CHECKBOX, SELECT, RADIO_MULTIPLE)
79 DATES = (DATE, DATE_TIME, DOB)
80 MULTIPLE = (CHECKBOX_MULTIPLE, SELECT_MULTIPLE)
85 DATE: html5_field("date", forms.DateInput),
86 DATE_TIME: html5_field("datetime", forms.DateTimeInput),
87 DOB: html5_field("date", forms.DateInput),
88 EMAIL: html5_field("email", forms.TextInput),
89 NUMBER: html5_field("number", forms.TextInput),
90 URL: html5_field("url", forms.TextInput),
93 # Add any custom fields defined.
94 for field_id, field_path, field_name in EXTRA_FIELDS:
95 if field_id in CLASSES:
96 err = "ID %s for field %s in FORMS_EXTRA_FIELDS already exists"
97 raise ImproperlyConfigured(err % (field_id, field_name))
98 CLASSES[field_id] = import_attr(field_path)
99 NAMES += ((field_id, _(field_name)),)
101 # Add/update custom widgets.
102 for field_id, widget_path in EXTRA_WIDGETS:
103 if field_id not in CLASSES:
104 err = "ID %s in FORMS_EXTRA_WIDGETS does not match a field"
105 raise ImproperlyConfigured(err % field_id)
106 WIDGETS[field_id] = import_attr(widget_path)