Ticket #6790: error_dict_list_fixes_and_overrides_4.diff

File error_dict_list_fixes_and_overrides_4.diff, 6.5 KB (added by oyvind, 17 years ago)

Fix one thing at the time, just overrides and no labels in errordict

  • django/newforms/forms.py

    diff --git a/django/newforms/forms.py b/django/newforms/forms.py
    index 2c481e4..c01a59b 100644
    a b class BaseForm(StrAndUnicode):  
    6464    # information. Any improvements to the form API should be made to *this*
    6565    # class, not to the Form class.
    6666    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    67                  initial=None, error_class=ErrorList, label_suffix=':'):
     67                 initial=None, error_class=ErrorDict, label_suffix=':'):
    6868        self.is_bound = data is not None or files is not None
    6969        self.data = data or {}
    7070        self.files = files or {}
    class BaseForm(StrAndUnicode):  
    126126        output, hidden_fields = [], []
    127127        for name, field in self.fields.items():
    128128            bf = BoundField(self, field, name)
    129             bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
     129            bf_errors = bf.errors # Escape and cache in local variable.
    130130            if bf.is_hidden:
    131131                if bf_errors:
    132132                    top_errors.extend([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
    class BaseForm(StrAndUnicode):  
    182182        field -- i.e., from Form.clean(). Returns an empty ErrorList if there
    183183        are none.
    184184        """
    185         return self.errors.get(NON_FIELD_ERRORS, self.error_class())
     185        return self.errors.get(NON_FIELD_ERRORS)
    186186
    187187    def full_clean(self):
    188188        """
    189189        Cleans all of self.data and populates self._errors and
    190190        self.cleaned_data.
    191191        """
    192         self._errors = ErrorDict()
     192        self._errors = self.error_class()
    193193        if not self.is_bound: # Stop further processing.
    194194            return
    195195        self.cleaned_data = {}
    class BoundField(StrAndUnicode):  
    269269        Returns an ErrorList for this field. Returns an empty ErrorList
    270270        if there are none.
    271271        """
    272         return self.form.errors.get(self.name, self.form.error_class())
     272        return self.form.errors.get(self.name)
    273273    errors = property(_errors)
    274274
    275275    def as_widget(self, widget=None, attrs=None):
  • django/newforms/util.py

    diff --git a/django/newforms/util.py b/django/newforms/util.py
    index b3edf41..a7f2162 100644
    a b def flatatt(attrs):  
    1212    """
    1313    return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
    1414
    15 class ErrorDict(dict, StrAndUnicode):
     15class ErrorList(list, StrAndUnicode):
    1616    """
    1717    A collection of errors that knows how to display itself in various formats.
    18 
    19     The dictionary keys are the field names, and the values are the errors.
    2018    """
    2119    def __unicode__(self):
    2220        return self.as_ul()
    class ErrorDict(dict, StrAndUnicode):  
    2422    def as_ul(self):
    2523        if not self: return u''
    2624        return mark_safe(u'<ul class="errorlist">%s</ul>'
    27                 % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
    28                     for k, v in self.items()]))
     25                % ''.join([u'<li>%s</li>' % escape(e) for e in self]))
    2926
    3027    def as_text(self):
    31         return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u'  * %s' % force_unicode(i) for i in v])) for k, v in self.items()])
     28        if not self: return u''
     29        return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
    3230
    33 class ErrorList(list, StrAndUnicode):
     31    def __repr__(self):
     32        return repr([force_unicode(e) for e in self])
     33
     34class ErrorDict(dict, StrAndUnicode):
    3435    """
    3536    A collection of errors that knows how to display itself in various formats.
     37
     38    The dictionary keys are the field names, and the values are the errors.
    3639    """
     40    error_class = ErrorList
     41
    3742    def __unicode__(self):
    3843        return self.as_ul()
    3944
     45    def __getitem__(self, key):
     46        value = super(ErrorDict, self).__getitem__(key)
     47        if value:
     48            return self.error_class(value)
     49        return self.error_class()
     50
     51    def get(self, key, default=None):
     52        value = super(ErrorDict, self).get(key, default)
     53        if value:
     54            return self.error_class(value)
     55        return self.error_class()
     56
    4057    def as_ul(self):
    4158        if not self: return u''
    4259        return mark_safe(u'<ul class="errorlist">%s</ul>'
    43                 % ''.join([u'<li>%s</li>' % force_unicode(e) for e in self]))
     60                % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
     61                    for k, v in self.items()]))
    4462
    4563    def as_text(self):
    46         if not self: return u''
    47         return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
    48 
    49     def __repr__(self):
    50         return repr([force_unicode(e) for e in self])
     64        return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u'  * %s' % force_unicode(i) for i in v])) for k, v in self.items()])
    5165
    5266class ValidationError(Exception):
    5367    def __init__(self, message):
  • django/test/_doctest.py

    diff --git a/django/test/_doctest.py b/django/test/_doctest.py
    index a56483c..6d66a8d 100644
    a b class DocTest:  
    503503        self.globs = globs.copy()
    504504        self.name = name
    505505        self.filename = filename
    506         self.lineno = lineno
     506        self.lineno = lineno or 0
    507507
    508508    def __repr__(self):
    509509        if len(self.examples) == 0:
  • tests/regressiontests/forms/extra.py

    diff --git a/tests/regressiontests/forms/extra.py b/tests/regressiontests/forms/extra.py
    index 9dff407..2391660 100644
    a b u'sirrobin'  
    357357# Test overriding ErrorList in a form #
    358358#######################################
    359359
    360 >>> from django.newforms.util import ErrorList
     360>>> from django.newforms.util import ErrorDict, ErrorList
    361361>>> class DivErrorList(ErrorList):
    362362...     def __unicode__(self):
    363363...         return self.as_divs()
    364364...     def as_divs(self):
    365365...         if not self: return u''
    366366...         return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % force_unicode(e) for e in self])
     367>>> class DivErrorDict(ErrorDict):
     368...     error_class = DivErrorList
    367369>>> class CommentForm(Form):
    368370...     name = CharField(max_length=50, required=False)
    369371...     email = EmailField()
    370372...     comment = CharField()
    371373>>> data = dict(email='invalid')
    372 >>> f = CommentForm(data, auto_id=False, error_class=DivErrorList)
     374>>> f = CommentForm(data, auto_id=False, error_class=DivErrorDict)
    373375>>> print f.as_p()
    374376<p>Name: <input type="text" name="name" maxlength="50" /></p>
    375377<div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div>
Back to Top