web/lib/django/forms/util.py
author ymh <ymh.work@gmail.com>
Wed, 02 Jun 2010 18:57:35 +0200
changeset 38 77b6da96e6f1
permissions -rw-r--r--
update django
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
38
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from django.utils.html import conditional_escape
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from django.utils.encoding import StrAndUnicode, force_unicode
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.utils.safestring import mark_safe
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
# Import ValidationError so that it can be imported from this
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
# module to maintain backwards compatibility.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.core.exceptions import ValidationError
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
def flatatt(attrs):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    Convert a dictionary of attributes to a single string.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    The returned string will contain a leading space followed by key="value",
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
    XML-style pairs.  It is assumed that the keys do not need to be XML-escaped.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    If the passed dictionary is empty, then return an empty string.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    return u''.join([u' %s="%s"' % (k, conditional_escape(v)) for k, v in attrs.items()])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
class ErrorDict(dict, StrAndUnicode):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    A collection of errors that knows how to display itself in various formats.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    The dictionary keys are the field names, and the values are the errors.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
    def __unicode__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        return self.as_ul()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    def as_ul(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        if not self: return u''
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        return mark_safe(u'<ul class="errorlist">%s</ul>'
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
                % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
                    for k, v in self.items()]))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    def as_text(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        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()])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
class ErrorList(list, StrAndUnicode):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    A collection of errors that knows how to display itself in various formats.
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    """
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
    def __unicode__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        return self.as_ul()
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    def as_ul(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        if not self: return u''
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        return mark_safe(u'<ul class="errorlist">%s</ul>'
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
                % ''.join([u'<li>%s</li>' % conditional_escape(force_unicode(e)) for e in self]))
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
    def as_text(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        if not self: return u''
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        return u'\n'.join([u'* %s' % force_unicode(e) for e in self])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
    def __repr__(self):
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        return repr([force_unicode(e) for e in self])
77b6da96e6f1 update django
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54