-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathforms.py
More file actions
executable file
·219 lines (170 loc) · 5.72 KB
/
Copy pathforms.py
File metadata and controls
executable file
·219 lines (170 loc) · 5.72 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Enhancements to wagtail.contrib.forms.
"""
import csv
import os
from django import forms
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.db import models
from django.http import HttpResponse
from django.utils.translation import gettext_lazy as _
from wagtail.contrib.forms.forms import FormBuilder
from wagtail.contrib.forms.models import AbstractFormField
from wagtail.contrib.forms.views import (
SubmissionsListView as WagtailSubmissionsListView,
)
from coderedcms.settings import crx_settings
from coderedcms.utils import attempt_protected_media_value_conversion
FORM_FIELD_CHOICES = (
(
_("Text"),
(
("singleline", _("Single line text")),
("multiline", _("Multi-line text")),
("email", _("Email")),
("number", _("Number - only allows integers")),
("url", _("URL")),
),
),
(
_("Choice"),
(
("checkboxes", _("Checkboxes")),
("dropdown", _("Drop down")),
("radio", _("Radio buttons")),
("multiselect", _("Multiple select")),
("checkbox", _("Single checkbox")),
),
),
(
_("Date & Time"),
(
("date", _("Date")),
("time", _("Time")),
("datetime", _("Date and time")),
),
),
(
_("File Upload"),
(("file", _("Secure File - login required to access uploaded files")),),
),
(
_("Other"),
(("hidden", _("Hidden field")),),
),
)
# Files
class SecureFileField(forms.FileField):
custom_error_messages = {
"blacklist_file": _("Submitted file is not allowed."),
"whitelist_file": _("Submitted file is not allowed."),
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.error_messages.update(self.custom_error_messages)
def validate(self, value):
super(SecureFileField, self).validate(value)
if value:
self._check_whitelist(value)
self._check_blacklist(value)
def _check_whitelist(self, value):
if crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_WHITELIST:
if (
os.path.splitext(value.name)[1].lower()
not in crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_WHITELIST
):
raise ValidationError(self.error_messages["whitelist_file"])
def _check_blacklist(self, value):
if crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_BLACKLIST:
if (
os.path.splitext(value.name)[1].lower()
in crx_settings.CRX_PROTECTED_MEDIA_UPLOAD_BLACKLIST
):
raise ValidationError(self.error_messages["blacklist_file"])
# Date
class CoderedDateInput(forms.DateInput):
template_name = "coderedcms/formfields/date.html"
class CoderedDateField(forms.DateField):
widget = CoderedDateInput()
# Datetime
class CoderedDateTimeInput(forms.DateTimeInput):
template_name = "coderedcms/formfields/datetime.html"
class CoderedDateTimeField(forms.DateTimeField):
widget = CoderedDateTimeInput()
input_formats = [
"%Y-%m-%dT%H:%M",
"%m/%d/%Y %I:%M %p",
"%m/%d/%Y %I:%M%p",
"%m/%d/%Y %H:%M",
]
# Time
class CoderedTimeInput(forms.TimeInput):
template_name = "coderedcms/formfields/time.html"
class CoderedTimeField(forms.TimeField):
widget = CoderedTimeInput()
input_formats = ["%H:%M", "%I:%M %p", "%I:%M%p"]
class CoderedFormBuilder(FormBuilder):
"""
Enhance wagtail FormBuilder with additional custom fields.
"""
def create_file_field(self, field, options):
return SecureFileField(**options)
def create_date_field(self, field, options):
return CoderedDateField(**options)
def create_datetime_field(self, field, options):
return CoderedDateTimeField(**options)
def create_time_field(self, field, options):
return CoderedTimeField(**options)
class CoderedSubmissionsListView(WagtailSubmissionsListView):
def get_csv_response(self, context):
filename = self.get_csv_filename()
response = HttpResponse(content_type="text/csv; charset=utf-8")
response["Content-Disposition"] = "attachment;filename={}".format(
filename
)
writer = csv.writer(response)
writer.writerow(context["data_headings"])
for data_row in context["data_rows"]:
modified_data_row = []
for cell in data_row:
modified_cell = attempt_protected_media_value_conversion(
self.request, cell
)
modified_data_row.append(modified_cell)
writer.writerow(modified_data_row)
return response
class CoderedFormField(AbstractFormField):
class Meta:
abstract = True
field_type = models.CharField(
verbose_name=_("field type"),
max_length=16,
choices=FORM_FIELD_CHOICES,
blank=False,
default="singleline",
)
class SearchForm(forms.Form):
s = forms.CharField(
max_length=255,
required=False,
label=_("Search"),
)
t = forms.CharField(
widget=forms.HiddenInput,
max_length=255,
required=False,
label=_("Page type"),
)
def get_page_model_choices():
"""
Returns a list of tuples of all creatable Codered pages
in the format of (app_label:model, "Verbose Name")
"""
from coderedcms.models import get_page_models
rval = []
for page in get_page_models():
if page.is_creatable:
ct = ContentType.objects.get_for_model(page)
rval.append((f"{ct.app_label}:{ct.model}", ct.name))
return rval