app/django/forms/util.py
changeset 323 ff1a9aa48cfd
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
       
     1 from django.utils.html import conditional_escape
       
     2 from django.utils.encoding import smart_unicode, StrAndUnicode, force_unicode
       
     3 from django.utils.safestring import mark_safe
       
     4 
       
     5 def flatatt(attrs):
       
     6     """
       
     7     Convert a dictionary of attributes to a single string.
       
     8     The returned string will contain a leading space followed by key="value",
       
     9     XML-style pairs.  It is assumed that the keys do not need to be XML-escaped.
       
    10     If the passed dictionary is empty, then return an empty string.
       
    11     """
       
    12     return u''.join([u' %s="%s"' % (k, conditional_escape(v)) for k, v in attrs.items()])
       
    13 
       
    14 class ErrorDict(dict, StrAndUnicode):
       
    15     """
       
    16     A collection of errors that knows how to display itself in various formats.
       
    17 
       
    18     The dictionary keys are the field names, and the values are the errors.
       
    19     """
       
    20     def __unicode__(self):
       
    21         return self.as_ul()
       
    22 
       
    23     def as_ul(self):
       
    24         if not self: return u''
       
    25         return mark_safe(u'<ul class="errorlist">%s</ul>'
       
    26                 % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
       
    27                     for k, v in self.items()]))
       
    28 
       
    29     def as_text(self):
       
    30         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()])
       
    31 
       
    32 class ErrorList(list, StrAndUnicode):
       
    33     """
       
    34     A collection of errors that knows how to display itself in various formats.
       
    35     """
       
    36     def __unicode__(self):
       
    37         return self.as_ul()
       
    38 
       
    39     def as_ul(self):
       
    40         if not self: return u''
       
    41         return mark_safe(u'<ul class="errorlist">%s</ul>'
       
    42                 % ''.join([u'<li>%s</li>' % force_unicode(e) for e in self]))
       
    43 
       
    44     def as_text(self):
       
    45         if not self: return u''
       
    46         return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
       
    47 
       
    48     def __repr__(self):
       
    49         return repr([force_unicode(e) for e in self])
       
    50 
       
    51 class ValidationError(Exception):
       
    52     def __init__(self, message):
       
    53         """
       
    54         ValidationError can be passed any object that can be printed (usually
       
    55         a string) or a list of objects.
       
    56         """
       
    57         if isinstance(message, list):
       
    58             self.messages = ErrorList([smart_unicode(msg) for msg in message])
       
    59         else:
       
    60             message = smart_unicode(message)
       
    61             self.messages = ErrorList([message])
       
    62 
       
    63     def __str__(self):
       
    64         # This is needed because, without a __str__(), printing an exception
       
    65         # instance would result in this:
       
    66         # AttributeError: ValidationError instance has no attribute 'args'
       
    67         # See http://www.python.org/doc/current/tut/node10.html#handling
       
    68         return repr(self.messages)