-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
363 lines (291 loc) · 13.4 KB
/
forms.py
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
forms.py: Adapts Django's forms module for use with Pylons.
Copyright (c) 2009 Marcus Cavanaugh.
Snippets from Django <http://www.djangoproject.com/>
and Pylons <http://www.pylonshq.com/>
are copyright their respective owners.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Instructions:
1. Install Django. (`easy_install django`)
2. Read the Django Forms documentation.
3. Pretend this module is `django.forms` and import it.
"""
__version__ = "0.1.2"
###########################################################################
# The patches in the section below are required for django forms to work.
# Django uses a function called mark_safe for HTML strings;
# adapt it to use WebHelpers' literal() function instead.
from django.utils import safestring, html as utils_html
from webhelpers.html import literal
safestring.mark_safe = literal
# Make sure conditional escape works with webhelpers literals too
def _html_conditional_escape(html):
"""
Similar to escape(), except that it doesn't operate on pre-escaped strings.
"""
if hasattr(html, '__html__') or isinstance(html, utils_html.SafeData):
return html
else:
return utils_html.escape(html)
utils_html.conditional_escape = _html_conditional_escape
# Configure Django settings once, and only once.
from django.conf import settings
if not hasattr(settings, '__django_configured'):
settings.configure()
settings.__django_configured = True
# Wildcard because this module is a drop-in replacement for `django.forms`.
from django.forms import *
from django.forms import extras
from django.forms import formsets
# so that you can write ${form.field} in templates
forms.BoundField.__html__ = forms.BoundField.__unicode__
util.ErrorList.__html__ = forms.ErrorList.__unicode__
util.ErrorDict.__html__ = forms.ErrorDict.__unicode__
formsets.BaseFormSet.__html__ = formsets.BaseFormSet.__unicode__
forms.Form.__html__ = forms.Form.__unicode__
# Patch widgets.SelectMultiple to work with Paste's MultiDict;
# see http://code.djangoproject.com/ticket/10659 for discussion.
def value_from_datadict_select_multiple(self, data, files, name):
if isinstance(data, (MultiValueDict, MergeDict)):
return data.getlist(name)
if hasattr(data, 'getall'): # +++
return data.getall(name) # +++
return data.get(name, None)
widgets.SelectMultiple.value_from_datadict = value_from_datadict_select_multiple
# Patch fields.FileField to accommodate Pylons file handling,
# which uses cgi.FieldStorage in request.POST. This required a couple tweaks:
# First, bool(cgi.FieldStorage) is False, so you can't use "if not data" as
# FileField did. Second, cgi.FieldStorage uses data.filename and data.length
# rather than Django's data.name and data.size.
def _FileField_clean(self, data, initial=None):
super(FileField, self).clean(initial or data)
if not self.required and data in fields.EMPTY_VALUES:
return None
elif not hasattr(data, 'filename') and initial: # modified
return initial
# UploadedFile objects should have name and size attributes.
try:
file_name = data.filename # modified
file_size = data.length # modified
except AttributeError:
raise ValidationError(self.error_messages['invalid'])
if self.max_length is not None and len(file_name) > self.max_length:
error_values = {'max': self.max_length, 'length': len(file_name)}
raise ValidationError(self.error_messages['max_length'] % error_values)
if not file_name:
raise ValidationError(self.error_messages['invalid'])
if not file_size:
raise ValidationError(self.error_messages['empty'])
return data
FileField.clean = _FileField_clean
###########################################################################
# The patches in the section below are optional, but make life easier.
# cgi.FieldStorage evaluates to False when converted to bool. That makes
# code like ``if form.cleaned_data['upload']:`` evaluate to False even
# when a valid file was uploaded. This patch changes that behavior so that
# cgi.FieldStorage evaluates to True.
import cgi
cgi.FieldStorage.__nonzero__ = lambda self: True
def model_to_dict(*items, **kwds):
"""Convert an object (like an SQLAlchemy model) or dictionary-like object
into a dict suitable for use with Form's ``initial`` keyword.
Additional arguments are merged into the result. Objects' attributes or
keys beginning with an underscore, or in ``exclude``, are left out.
Typical usage::
c.form = ContactForm(
initial=forms.model_to_dict(user, {'comment_text': 'Hi'}))
"""
exclude = kwds.pop('exclude', [])
include = kwds.pop('include', [])
ret = {}
for item in items:
if hasattr(item, '__dict__'): # this is a class
item = item.__dict__
if not item:
continue
for attr in item:
if include and (attr not in include):
continue
if (attr not in exclude) and not attr.startswith('_'):
ret[attr] = item[attr]
return ret
def update_model(model, fields, **kwds):
"""Update an object (like an SQLAlchemy model) from a dictionary (like
that provided by form.cleaned_data). Since Django's ModelForm doesn't work
with SQLAlchemy, this is a reasonably quick alternative approach::
forms.update_model(user, form.cleaned_data, exclude=['password'])
Provide either ``exclude`` or ``include`` to further restrict which fields
are included. Fields with an underscore are also excluded.
"""
exclude = kwds.pop('exclude', [])
include = kwds.pop('include', [])
for k, v in fields.items():
if include and (k not in include):
continue
if hasattr(model, k) and not callable(getattr(model, k)) and k not in exclude:
setattr(model, k, v)
import formencode
from pylons import request, response
class HTMLForm(object):
"""A form that does no validation, but fills in form fields using htmlfill.
Use this to fill in initial HTML values without doing any validation.
See ``FormEncodeForm`` if you want to use FormEncode validation django-style.
Supply the ``html`` needed to render the form (usually via your framework's
``render()`` method) and then pretend this is a Django Form.
You can provide the ``html`` via HTMLForm's constructor, or you can
subclass HTMLForm and set ``html`` as a class attribute.
If you sublass HTMLForm, you can still override the ``clean()`` method to
raise a ValidationError.
Example Usage (in a controller)::
def edit(self, id):
class EditUserForm(forms.HTMLForm):
html = render('/edit_form.html')
if request.method == 'POST':
c.form = EditUserForm(request.POST)
if c.form.is_valid():
# The is_valid() call is unnecessary if you haven't
# overridden the HTMLForm.clean() method, since HTMLForm
# doesn't do any validation itself.
# process things here
else:
c.form = EditUserForm() # or EditUserForm(initial=...)
>>> data = {'foo': 'bar'}
>>> form = HTMLForm(data, html='<input name="foo">')
>>> unicode(form)
literal(u'<input name="foo" value="bar">')
>>> class MyRawHTMLForm(HTMLForm):
... html = '<input type="text" name="user" value="initial in template">'
>>> unicode(MyRawHTMLForm())
literal(u'<input type="text" name="user" value="initial in template">')
>>> unicode(MyRawHTMLForm(initial={'user':'john doe'}))
literal(u'<input type="text" name="user" value="john doe">')
"""
html = ''
def __init__(self, data=None, files=None, html=None, initial=None):
self.is_bound = data is not None or files is not None
self.data = data or {}
self.files = files or {}
self.initial = initial or {}
self._errors = None
if html is not None:
self.html = html # override the HTML for this instance instead
def is_valid(self):
self.cleaned_data = self.data
return self.is_bound and not bool(self.errors)
def clean(self):
return self.cleaned_data
def full_clean(self):
self._errors = {}
if not self.is_bound: # Stop further processing
return
self.cleaned_data = self.data
try:
self.cleaned_data = self.clean()
except ValidationError, e:
self._errors[forms.NON_FIELD_ERRORS] = e.messages
if self._errors:
delattr(self, 'cleaned_data')
def _get_errors(self):
"Returns an ErrorDict for the data provided for the form"
if self._errors is None:
self.full_clean()
return self._errors
errors = property(_get_errors)
def __html__(self):
return unicode(self)
def __unicode__(self):
# unbound forms should return the HTML unmodified. If this was passed
# through htmlfill instead, all form values would be nuked.
try:
response_charset = response.determine_charset()
except TypeError: # no pylons request is active
response_charset = 'utf-8'
if not self.is_bound:
defaults = self.initial.copy()
# using WebHelpers, boolean values cannot be True; they must be '1'
for key, value in defaults.items():
if value is True:
defaults[key] = '1'
return literal(formencode.htmlfill.render(
form=self.html,
defaults=defaults,
errors=self.errors,
encoding=response_charset # use the proper charset
))
else:
defaults = self.data.copy()
# using WebHelpers, boolean values cannot be True; they must be '1'
for key, value in defaults.items():
if value is True:
defaults[key] = '1'
return literal(formencode.htmlfill.render(
form=self.html,
defaults=defaults,
errors=self.errors,
encoding=response_charset # use the proper charset
))
class FormEncodeForm(formencode.Schema, HTMLForm):
"""A base class for FormEncode Schemas that allows them to be processed
just like Django Forms in your controller::
def edit(self, id):
class EditForm(forms.FormEncodeForm):
html = render('/edit_form.html')
text = formencode.validators.String()
if request.method == 'POST':
c.form = EditForm(request.POST)
if c.form.is_valid():
# process
else:
c.form = EditForm()
>>> class MySchema(FormEncodeForm):
... html = '<form><input name="foo"></form>'
... foo = formencode.validators.String(min=5)
>>> form = MySchema()
>>> unicode(form)
literal(u'<form><input name="foo" value=""></form>')
>>> form = MySchema(initial={'foo': 'hello'})
>>> unicode(form)
literal(u'<form><input name="foo" value="hello"></form>')
>>> form = MySchema({'foo': 'bar'})
>>> form.is_valid()
False
>>> '<input name="foo" class="error" value="bar">' in unicode(form)
True
"""
def __init__(self, *args, **kwds):
formencode.Schema.__init__(self)
HTMLForm.__init__(self, *args, **kwds)
def full_clean(self):
self._errors = {}
if not self.is_bound: # Stop further processing
return
self.cleaned_data = {}
try:
self.cleaned_data = self.to_python(self.data)
except formencode.validators.Invalid, e:
self._errors = e.unpack_errors()
try:
self.cleaned_data = self.clean()
except formencode.validators.Invalid, e:
self._errors[forms.NON_FIELD_ERRORS] = e.unpack_errors()
except ValidationError, e:
self._errors[forms.NON_FIELD_ERRORS] = e.messages
if self._errors:
delattr(self, 'cleaned_data')
if __name__ == '__main__':
import doctest
doctest.testmod()