27 from copy
import deepcopy
30 from django
import forms
31 from django.forms.util
import flatatt
32 from django.utils.safestring
import mark_safe
33 from django.utils.translation
import ugettext_lazy
as _
35 from wi.utils.regexp
import regexp, regexp_text
38 attrs_dict = {
'class':
'required'}
51 if any([v.required
and int(self.cleaned_data[k]) == -1
for k, v
in self.fields.iteritems()]):
52 raise forms.ValidationError(_(
'Please fill in all required form fields.'))
53 return self.cleaned_data
65 return iter((n, n)
for n
in range(1, self.
max_choices))
73 new_password = forms.RegexField(regex=regexp[
'password'],
75 widget=forms.PasswordInput(attrs=dict(attrs_dict)),
77 error_messages={
'invalid': regexp_text[
'password']})
79 password2 = forms.RegexField(regex=regexp[
'password'],
81 widget=forms.PasswordInput(attrs=dict(attrs_dict)),
82 label=_(
"Password confirmation"),
83 error_messages={
'invalid': regexp_text[
'password']})
90 if 'new_password' in self.cleaned_data
and 'password2' in self.cleaned_data:
91 if self.cleaned_data[
'new_password'] != self.cleaned_data[
'password2']:
92 raise forms.ValidationError(_(
"The two password fields didn't match."))
94 self.cleaned_data[
'new_password'] = hashlib.sha1(self.cleaned_data[
'new_password']).hexdigest()
95 del self.cleaned_data[
'password2']
96 return self.cleaned_data
99 Copyright (c) 2008, Carl J Meyer
102 Redistribution and use in source and binary forms, with or without modification,
103 are permitted provided that the following conditions are met:
105 * Redistributions of source code must retain the above copyright notice,
106 this list of conditions and the following disclaimer.
107 * Redistributions in binary form must reproduce the above copyright notice,
108 this list of conditions and the following disclaimer in the documentation
109 and/or other materials provided with the distribution.
110 * The names of its contributors may not be used to endorse or promote products
111 derived from this software without specific prior written permission.
113 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
114 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
115 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
116 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
117 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
118 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
119 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
120 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
121 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123 Time-stamp: <2008-11-21 01:54:45 carljm forms.py>
133 def __init__(self, form, name, boundfields, legend=None, description=''):
144 yield _mark_row_attrs(bfi, self.
form)
147 return "%s('%s', %s, legend='%s', description='%s')" % (
148 self.__class__.__name__, self.
name,
162 self.
fieldsets = ((
'main', {
'fields': self.form.fields.keys(),
166 field_names = [n
for n
in options[
'fields']
167 if n
in self.form.fields]
169 raise ValueError(
"Fieldset definition must include 'fields' option.")
170 boundfields = [forms.forms.BoundField(self.
form, self.form.fields[n], n)
171 for n
in field_names]
173 options.get(
'legend',
None),
174 options.get(
'description',
''))
177 def _get_meta_attr(attrs, attr, default):
179 ret = getattr(attrs[
'Meta'], attr)
180 except (KeyError, AttributeError):
192 fieldsets = _get_meta_attr(attrs,
'fieldsets', ())
198 for bfs
in getattr(base,
'base_fieldsets', ()):
199 new_fieldsets[bfs[0]] = bfs
202 for bfs
in fieldsets:
203 new_fieldsets[bfs[0]] = bfs
204 if bfs[0]
not in order:
207 return [new_fieldsets[name]
for name
in order]
216 return _get_meta_attr(attrs,
'row_attrs', {})
219 def _mark_row_attrs(bf, form):
220 row_attrs = deepcopy(form._row_attrs.get(bf.name, {}))
221 if bf.field.required:
222 req_class =
'required'
224 req_class =
'optional'
225 if 'class' in row_attrs:
226 row_attrs[
'class'] = row_attrs[
'class'] +
' ' + req_class
228 row_attrs[
'class'] = req_class
229 bf.row_attrs = mark_safe(flatatt(row_attrs))
237 new_class = super(BetterFormBaseMetaclass,
238 cls).
__new__(cls, name, bases, attrs)
246 class BetterModelFormMetaclass(BetterFormBaseMetaclass, forms.models.ModelFormMetaclass):
303 self.
_fieldsets = deepcopy(self.base_fieldsets)
304 self.
_row_attrs = deepcopy(self.base_row_attrs)
305 super(BetterBaseForm, self).
__init__(*args, **kwargs)
312 for bf
in super(BetterBaseForm, self).
__iter__():
313 yield _mark_row_attrs(bf, self)
317 __metaclass__ = BetterFormMetaclass
318 __doc__ = BetterBaseForm.__doc__
322 __metaclass__ = BetterModelFormMetaclass
323 __doc__ = BetterBaseForm.__doc__