From b4a67359a34c77e2e40f1ab8e30b96278a063007 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 7 Dec 2006 06:59:07 +0000 Subject: [PATCH] newforms: Abstracted common code in Form as_table(), as_ul() and as_p() into Form._html_output(). It's slightly obscure but saves on a lot of redundancy and code bloat. git-svn-id: http://code.djangoproject.com/svn/django/trunk@4179 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 80 +++++++++++----------------------------- 1 file changed, 22 insertions(+), 58 deletions(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 15b24b39fe..265668bbab 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -72,9 +72,9 @@ class Form(StrAndUnicode): """ return not self.ignore_errors and not bool(self.errors) - def as_table(self): - "Returns this form rendered as HTML s -- excluding the
." - top_errors = self.non_field_errors() + def _html_output(self, normal_row, error_row, row_ender, 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) @@ -84,72 +84,36 @@ class Form(StrAndUnicode): top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) hidden_fields.append(unicode(bf)) else: - if bf_errors: - output.append(u'%s' % bf_errors) - output.append(u'%s%s' % (bf.label_tag(escape(bf.verbose_name+':')), bf)) + label = bf.label_tag(escape(bf.verbose_name+':')) + if errors_on_separate_row: + if bf_errors: + output.append(error_row % bf_errors) + output.append(normal_row % (label, bf)) + else: + output.append(normal_row % ((bf_errors, label, bf))) if top_errors: - output.insert(0, u'%s' % top_errors) - if hidden_fields: # Insert any hidden fields in the last . + output.insert(0, error_row % top_errors) + if hidden_fields: # Insert any hidden fields in the last row. str_hidden = u''.join(hidden_fields) if output: - last_td = output[-1] - # Chop off the trailing '' and insert the hidden fields. - output[-1] = last_td[:-10] + str_hidden + '' - else: # If there aren't any ''s in the output, just append the hidden fields. + last_row = output[-1] + # Chop off the trailing row_ender (e.g. '') and insert the hidden fields. + 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 u'\n'.join(output) + def as_table(self): + "Returns this form rendered as HTML s -- excluding the
." + return self._html_output(u'%s%s', u'%s', '', True) + def as_ul(self): "Returns this form rendered as HTML
  • s -- excluding the ." - top_errors = self.non_field_errors() - output, hidden_fields = [], [] - for name, field in self.fields.items(): - bf = BoundField(self, field, name) - if bf.is_hidden: - new_errors = bf.errors # Cache in local variable. - if new_errors: - top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in new_errors]) - hidden_fields.append(unicode(bf)) - else: - output.append(u'
  • %s%s %s
  • ' % (bf.errors, bf.label_tag(escape(bf.verbose_name+':')), bf)) - if top_errors: - output.insert(0, u'
  • %s
  • ' % top_errors) - if hidden_fields: # Insert any hidden fields in the last
  • . - str_hidden = u''.join(hidden_fields) - if output: - last_li = output[-1] - # Chop off the trailing '
  • ' and insert the hidden fields. - output[-1] = last_li[:-5] + str_hidden + '' - else: # If there aren't any '
  • 's in the output, just append the hidden fields. - output.append(str_hidden) - return u'\n'.join(output) + return self._html_output(u'
  • %s%s %s
  • ', u'
  • %s
  • ', '', False) def as_p(self): "Returns this form rendered as HTML

    s." - top_errors = self.non_field_errors() - output, hidden_fields = [], [] - for name, field in self.fields.items(): - bf = BoundField(self, field, name) - bf_errors = bf.errors # Cache in local variable. - if bf.is_hidden: - if bf_errors: - top_errors.extend(['(Hidden field %s) %s' % (name, e) for e in bf_errors]) - hidden_fields.append(unicode(bf)) - else: - if bf_errors: - output.append(u'

    %s

    ' % bf_errors) - output.append(u'

    %s %s

    ' % (bf.label_tag(escape(bf.verbose_name+':')), bf)) - if top_errors: - output.insert(0, u'

    %s

    ' % top_errors) - if hidden_fields: # Insert any hidden fields in the last

    . - str_hidden = u''.join(hidden_fields) - if output: - last_td = output[-1] - # Chop off the trailing '

    ' and insert the hidden fields. - output[-1] = last_td[:-4] + str_hidden + '

    ' - else: # If there aren't any '

    's in the output, just append the hidden fields. - output.append(str_hidden) - return u'\n'.join(output) + return self._html_output(u'

    %s %s

    ', u'

    %s

    ', '

    ', True) def non_field_errors(self): """