Django

Code

Ticket #23: warnings_as_list_partial.patch

File warnings_as_list_partial.patch, 2.5 kB (added by CollinAnderson, 6 months ago)

Patch against [6992] that has a partial implemtation of keeping track of Warnings as a list

  • newforms/forms.py

    old new  
    1111 
    1212from fields import Field 
    1313from widgets import TextInput, Textarea 
    14 from util import flatatt, ErrorDict, ErrorList, ValidationError 
     14from util import flatatt, WarningDict, WarningList, ErrorDict, ErrorList, ValidationError 
    1515 
    1616__all__ = ('BaseForm', 'Form') 
    1717 
     
    8787        return self._errors 
    8888    errors = property(_get_errors) 
    8989 
     90    def _get_warnings(self): 
     91        "Returns an WarningsDict for the data provided for the form" 
     92        if self._warnings is None: 
     93            self.full_clean() 
     94        return self._warnings 
     95    warnings = property(_get_warnings) 
     96 
    9097    def is_valid(self): 
    9198        """ 
    9299        Returns True if the form has no errors. Otherwise, False. If errors are 
     
    173180        self.cleaned_data. 
    174181        """ 
    175182        self._errors = ErrorDict() 
     183        self._warnings = ErrorDict() 
    176184        if not self.is_bound: # Stop further processing. 
    177185            return 
    178186        self.cleaned_data = {} 
     
    191199                self._errors[name] = e.messages 
    192200                if name in self.cleaned_data: 
    193201                    del self.cleaned_data[name] 
     202            self._warnings[name] = getattr(field, warnings, []) 
    194203        try: 
    195204            self.cleaned_data = self.clean() 
    196205        except ValidationError, e: 
  • newforms/util.py

    old new  
    4949    def __repr__(self): 
    5050        return repr([force_unicode(e) for e in self]) 
    5151 
     52class WarningDict(ErrorDict): 
     53    """ 
     54    A collection of warnings that knows how to display itself in various formats. 
     55 
     56    The dictionary keys are the field names, and the values are the warnings. 
     57    """ 
     58    def as_ul(self): 
     59        if not self: return u'' 
     60        return mark_safe(u'<ul class="warninglist">%s</ul>' 
     61                % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v)) 
     62                    for k, v in self.items()])) 
     63 
     64class WarningList(ErrorList): 
     65    """ 
     66    A collection of warnings that knows how to display itself in various formats. 
     67    """ 
     68    def as_ul(self): 
     69        if not self: return u'' 
     70        return mark_safe(u'<ul class="warninglist">%s</ul>' 
     71                % ''.join([u'<li>%s</li>' % force_unicode(e) for e in self])) 
     72 
    5273class ValidationError(Exception): 
    5374    def __init__(self, message): 
    5475        """