diff options
Diffstat (limited to 'django/newforms/forms.py')
| -rw-r--r-- | django/newforms/forms.py | 120 |
1 files changed, 83 insertions, 37 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py index e0b3d500b5..7e29941bef 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -2,11 +2,13 @@ Form classes """ -from django.utils.datastructures import SortedDict +from django.utils.datastructures import SortedDict, MultiValueDict from django.utils.html import escape from fields import Field -from widgets import TextInput, Textarea, HiddenInput -from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError +from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput +from util import flatatt, StrAndUnicode, ErrorDict, ErrorList, ValidationError + +__all__ = ('BaseForm', 'Form') NON_FIELD_ERRORS = '__all__' @@ -24,24 +26,35 @@ class SortedDictFromList(SortedDict): self.keyOrder = [d[0] for d in data] dict.__init__(self, dict(data)) + def copy(self): + return SortedDictFromList(self.items()) + class DeclarativeFieldsMetaclass(type): - "Metaclass that converts Field attributes to a dictionary called 'fields'." + "Metaclass that converts Field attributes to a dictionary called 'base_fields'." def __new__(cls, name, bases, attrs): - fields = [(name, attrs.pop(name)) for name, obj in attrs.items() if isinstance(obj, Field)] + fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) - attrs['fields'] = SortedDictFromList(fields) + attrs['base_fields'] = SortedDictFromList(fields) return type.__new__(cls, name, bases, attrs) -class Form(StrAndUnicode): - "A collection of Fields, plus their associated data." - __metaclass__ = DeclarativeFieldsMetaclass - - def __init__(self, data=None, auto_id=False): # TODO: prefix stuff - self.ignore_errors = data is None +class BaseForm(StrAndUnicode): + # This is the main implementation of all the Form logic. Note that this + # class is different than Form. See the comments by the Form class for more + # information. Any improvements to the form API should be made to *this* + # class, not to the Form class. + def __init__(self, data=None, auto_id='id_%s', prefix=None, initial=None): + self.is_bound = data is not None self.data = data or {} self.auto_id = auto_id - self.clean_data = None # Stores the data after clean() has been called. + self.prefix = prefix + self.initial = initial or {} self.__errors = None # Stores the errors after clean() has been called. + # The base_fields class attribute is the *class-wide* definition of + # fields. Because a particular *instance* of the class might want to + # alter self.fields, we create self.fields here by copying base_fields. + # Instances should always modify self.fields; they should not modify + # self.base_fields. + self.fields = self.base_fields.copy() def __unicode__(self): return self.as_table() @@ -70,9 +83,18 @@ class Form(StrAndUnicode): Returns True if the form has no errors. Otherwise, False. If errors are being ignored, returns False. """ - return not self.ignore_errors and not bool(self.errors) + return self.is_bound and not bool(self.errors) + + def add_prefix(self, field_name): + """ + Returns the field name with a prefix appended, if this Form has a + prefix set. + + Subclasses may wish to override. + """ + return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name - def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row): + def _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 = [], [] @@ -86,7 +108,12 @@ class Form(StrAndUnicode): else: if errors_on_separate_row and bf_errors: output.append(error_row % bf_errors) - output.append(normal_row % {'errors': bf_errors, 'label': bf.label_tag(escape(bf.verbose_name+':')), 'field': bf}) + label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' + if field.help_text: + help_text = help_text_html % field.help_text + else: + help_text = u'' + output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) if top_errors: output.insert(0, error_row % top_errors) if hidden_fields: # Insert any hidden fields in the last row. @@ -101,15 +128,15 @@ class Form(StrAndUnicode): def as_table(self): "Returns this form rendered as HTML <tr>s -- excluding the <table></table>." - return self._html_output(u'<tr><td>%(label)s</td><td>%(field)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', True) + return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False) def as_ul(self): "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." - return self._html_output(u'<li>%(errors)s%(label)s %(field)s</li>', u'<li>%s</li>', '</li>', False) + return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False) def as_p(self): "Returns this form rendered as HTML <p>s." - return self._html_output(u'<p>%(label)s %(field)s</p>', u'<p>%s</p>', '</p>', True) + return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True) def non_field_errors(self): """ @@ -123,16 +150,16 @@ class Form(StrAndUnicode): """ Cleans all of self.data and populates self.__errors and self.clean_data. """ - self.clean_data = {} errors = ErrorDict() - if self.ignore_errors: # Stop further processing. + if not self.is_bound: # Stop further processing. self.__errors = errors return + self.clean_data = {} for name, field in self.fields.items(): # value_from_datadict() gets the data from the dictionary. # Each widget type knows how to retrieve its own data, because some # widgets split data over several HTML fields. - value = field.widget.value_from_datadict(self.data, name) + value = field.widget.value_from_datadict(self.data, self.add_prefix(name)) try: value = field.clean(value) self.clean_data[name] = value @@ -146,7 +173,7 @@ class Form(StrAndUnicode): except ValidationError, e: errors[NON_FIELD_ERRORS] = e.messages if errors: - self.clean_data = None + delattr(self, 'clean_data') self.__errors = errors def clean(self): @@ -158,12 +185,26 @@ class Form(StrAndUnicode): """ return self.clean_data +class Form(BaseForm): + "A collection of Fields, plus their associated data." + # This is a separate class from BaseForm in order to abstract the way + # self.fields is specified. This class (Form) is the one that does the + # fancy metaclass stuff purely for the semantic sugar -- it allows one + # to define a form using declarative syntax. + # BaseForm itself has no way of designating self.fields. + __metaclass__ = DeclarativeFieldsMetaclass + class BoundField(StrAndUnicode): "A Field plus data" def __init__(self, form, field, name): self.form = form self.field = field self.name = name + self.html_name = form.add_prefix(name) + if self.field.label is None: + self.label = pretty_name(name) + else: + self.label = self.field.label def __unicode__(self): "Renders this field as an HTML widget." @@ -190,7 +231,11 @@ class BoundField(StrAndUnicode): auto_id = self.auto_id if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'): attrs['id'] = auto_id - return widget.render(self.name, self.data, attrs=attrs) + if not self.form.is_bound: + data = self.form.initial.get(self.name, self.field.initial) + else: + data = self.data + return widget.render(self.html_name, data, attrs=attrs) def as_text(self, attrs=None): """ @@ -206,28 +251,29 @@ class BoundField(StrAndUnicode): """ Returns a string of HTML for representing this as an <input type="hidden">. """ - return self.as_widget(HiddenInput(), attrs) + return self.as_widget(self.field.hidden_widget(), attrs) def _data(self): - "Returns the data for this BoundField, or None if it wasn't given." - return self.form.data.get(self.name, None) + """ + Returns the data for this BoundField, or None if it wasn't given. + """ + return self.field.widget.value_from_datadict(self.form.data, self.html_name) data = property(_data) - def _verbose_name(self): - return pretty_name(self.name) - verbose_name = property(_verbose_name) - - def label_tag(self, contents=None): + def label_tag(self, contents=None, attrs=None): """ Wraps the given contents in a <label>, if the field has an ID attribute. Does not HTML-escape the contents. If contents aren't given, uses the - field's HTML-escaped verbose_name. + field's HTML-escaped label. + + If attrs are given, they're used as HTML attributes on the <label> tag. """ - contents = contents or escape(self.verbose_name) + contents = contents or escape(self.label) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id if id_: - contents = '<label for="%s">%s</label>' % (widget.id_for_label(id_), contents) + attrs = attrs and flatatt(attrs) or '' + contents = '<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, contents) return contents def _is_hidden(self): @@ -242,8 +288,8 @@ class BoundField(StrAndUnicode): """ auto_id = self.form.auto_id if auto_id and '%s' in str(auto_id): - return str(auto_id) % self.name + return str(auto_id) % self.html_name elif auto_id: - return self.name + return self.html_name return '' auto_id = property(_auto_id) |
