Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
121 lines (89 loc) · 3.66 KB
/
Copy pathmodels.py
File metadata and controls
121 lines (89 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from functools import partial
from content_editor.models import Region, create_plugin_base
from django import forms
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django.utils.translation import gettext_lazy as _
from feincms3_forms import models as forms_models
class ConfiguredForm(forms_models.ConfiguredForm):
FORMS = [
forms_models.FormType(
key="contact",
label=_("contact form"),
regions=[Region(key="form", title=_("form"))],
validate="testapp.forms.validate_contact_form",
process="testapp.forms.process_contact_form",
),
forms_models.FormType(
key="other-fields",
label=_("other fields"),
regions=[],
form_class="testapp.forms.OtherFieldsForm",
),
]
ConfiguredFormPlugin = create_plugin_base(ConfiguredForm)
class PlainText(ConfiguredFormPlugin):
text = models.TextField(_("text"))
class Meta:
verbose_name = _("text")
def __str__(self):
return self.text[:40]
class SimpleField(forms_models.SimpleFieldBase, ConfiguredFormPlugin):
pass
Text = SimpleField.proxy(SimpleField.Type.TEXT)
Email = SimpleField.proxy(SimpleField.Type.EMAIL)
URL = SimpleField.proxy(SimpleField.Type.URL)
Date = SimpleField.proxy(SimpleField.Type.DATE)
Integer = SimpleField.proxy(SimpleField.Type.INTEGER)
Textarea = SimpleField.proxy(SimpleField.Type.TEXTAREA)
Checkbox = SimpleField.proxy(SimpleField.Type.CHECKBOX)
Select = SimpleField.proxy(SimpleField.Type.SELECT)
Radio = SimpleField.proxy(SimpleField.Type.RADIO, verbose_name="Listen to the radio")
SelectMultiple = SimpleField.proxy(SimpleField.Type.SELECT_MULTIPLE)
CheckboxSelectMultiple = SimpleField.proxy(SimpleField.Type.CHECKBOX_SELECT_MULTIPLE)
Anything = SimpleField.proxy("anything")
def clean_duration(form, data, *, name):
from_name = f"{name}_from"
until_name = f"{name}_until"
if (f := data.get(from_name)) and (u := data.get(until_name)) and f > u:
form.add_error(until_name, "Until has to be later than from.")
return data
class Duration(forms_models.FormFieldBase, ConfiguredFormPlugin):
label_from = models.CharField(_("from label"), max_length=1000)
label_until = models.CharField(_("until label"), max_length=1000)
class Meta:
verbose_name = _("duration")
def __str__(self):
return f"{self.label_from} - {self.label_until}"
def get_fields(self, **kwargs):
return {
f"{self.name}_from": forms.DateField(
label=self.label_from,
required=True,
widget=forms.DateInput(attrs={"type": "date"}),
),
f"{self.name}_until": forms.DateField(
label=self.label_until,
required=True,
widget=forms.DateInput(attrs={"type": "date"}),
),
}
def get_cleaners(self):
return [partial(clean_duration, name=self.name)]
class HoneypotField(forms.CharField):
widget = forms.HiddenInput
def validate(self, value):
super().validate(value)
if value:
raise forms.ValidationError(f"Invalid honeypot value {value!r}")
class Honeypot(forms_models.FormFieldBase, ConfiguredFormPlugin):
name = forms_models.NameField(default="honeypot")
class Meta:
verbose_name = _("honeypot")
def get_fields(self, **kwargs):
return {self.name: HoneypotField(required=False)}
class Log(models.Model):
configured_form = models.ForeignKey(ConfiguredForm, on_delete=models.CASCADE)
data = models.JSONField(encoder=DjangoJSONEncoder)
def __str__(self):
return ""