import django forms from django forms forms import BoundField from dja

  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
import django.forms
from django.forms.forms import BoundField
from django.utils.html import conditional_escape
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
from django.utils.safestring import mark_safe
# Close you eyes, nightmare begins
def custom_html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
"Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
output, hidden_fields = [], []
for name, field in self.fields.items():
bf = BoundField(self, field, name)
bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
if bf.is_hidden:
if bf_errors:
top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
hidden_fields.append(unicode(bf))
else:
if errors_on_separate_row and bf_errors:
output.append(error_row % force_unicode(bf_errors))
if bf.label:
label = conditional_escape(force_unicode(bf.label))
# Only add the suffix if the label does not end in
# punctuation.
if self.label_suffix:
if label[-1] not in ':?.!':
label += self.label_suffix
label = bf.label_tag(label) or ''
else:
label = ''
if field.help_text:
help_text = help_text_html % force_unicode(field.help_text)
else:
help_text = u''
output.append(normal_row(**{'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text}))
if top_errors:
output.insert(0, error_row % force_unicode(top_errors))
if hidden_fields: # Insert any hidden fields in the last row.
str_hidden = u''.join(hidden_fields)
if output:
last_row = output[-1]
# Chop off the trailing row_ender (e.g. '</td></tr>') and
# insert the hidden fields.
if not last_row.endswith(row_ender):
# This can happen in the as_p() case (and possibly others
# that users write): if there are only top errors, we may
# not be able to conscript the last row for our purposes,
# so insert a new, empty row.
last_row = normal_row(**{'errors': '', 'label': '', 'field': '', 'help_text': ''})
output.append(last_row)
output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
else:
# If there aren't any rows in the output, just append the
# hidden fields.
output.append(str_hidden)
return mark_safe(u'\n'.join(output))
def as_custom(self):
"""
Returns this form rendered as HTML <p>s.
This is modified version of original as_p.
Actually it wraps content into fieldset tag.
"""
try:
def row_callback(errors='', label='', field='', help_text=''):
if errors:
start = '<fieldset class="error">'
else:
start = '<fieldset>'
row = start + u'<label>%(label)s</label>%(field)s<div class="note">%(help_text)s</div>%(errors)s </fieldset>'
return row % {'errors': errors, 'label': label, 'field': field, 'help_text': help_text}
return custom_html_output(self, row_callback, u'%s', '</fieldset>', u' <span class="note">%s</span>', False)
except Exception, ex:
return ex
else:
return 'ok'
django.forms.BaseForm.as_custom = as_custom
def text_input_init(self, *args, **kwargs):
kwargs.setdefault('attrs', {}).setdefault('class', '')
kwargs['attrs']['class'] += ' text'
self.attrs = kwargs['attrs']
#Disabled because produce "maximum recursion depth exceeded in cmp" Exception
#super(self.__class__, self).__init__(*args, **kwargs)
def file_input_init(self, *args, **kwargs):
kwargs.setdefault('attrs', {}).setdefault('class', '')
kwargs['attrs']['class'] += ' file'
super(self.__class__, self).__init__(*args, **kwargs)
django.forms.widgets.TextInput.__init__ = text_input_init
django.forms.widgets.FileInput.__init__ = file_input_init