2007-01-21 04:33:23 +08:00
|
|
|
from django.utils.html import escape
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.encoding import smart_unicode, StrAndUnicode
|
2007-09-14 06:35:18 +08:00
|
|
|
from django.utils.functional import Promise
|
2007-01-21 04:33:23 +08:00
|
|
|
|
2007-05-20 02:49:35 +08:00
|
|
|
def flatatt(attrs):
|
|
|
|
"""
|
|
|
|
Convert a dictionary of attributes to a single string.
|
|
|
|
The returned string will contain a leading space followed by key="value",
|
|
|
|
XML-style pairs. It is assumed that the keys do not need to be XML-escaped.
|
|
|
|
If the passed dictionary is empty, then return an empty string.
|
|
|
|
"""
|
|
|
|
return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
class ErrorDict(dict, StrAndUnicode):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
A collection of errors that knows how to display itself in various formats.
|
|
|
|
|
|
|
|
The dictionary keys are the field names, and the values are the errors.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-10-29 04:34:37 +08:00
|
|
|
return self.as_ul()
|
|
|
|
|
|
|
|
def as_ul(self):
|
|
|
|
if not self: return u''
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s%s</li>' % (k, smart_unicode(v)) for k, v in self.items()])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'\n'.join([u'* %s\n%s' % (k, u'\n'.join([u' * %s' % smart_unicode(i) for i in v])) for k, v in self.items()])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
class ErrorList(list, StrAndUnicode):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
A collection of errors that knows how to display itself in various formats.
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-10-29 04:34:37 +08:00
|
|
|
return self.as_ul()
|
|
|
|
|
|
|
|
def as_ul(self):
|
|
|
|
if not self: return u''
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'<ul class="errorlist">%s</ul>' % ''.join([u'<li>%s</li>' % smart_unicode(e) for e in self])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
|
|
|
if not self: return u''
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return u'\n'.join([u'* %s' % smart_unicode(e) for e in self])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
class ValidationError(Exception):
|
|
|
|
def __init__(self, message):
|
|
|
|
"ValidationError can be passed a string or a list."
|
|
|
|
if isinstance(message, list):
|
|
|
|
self.messages = ErrorList([smart_unicode(msg) for msg in message])
|
|
|
|
else:
|
2007-09-14 06:35:18 +08:00
|
|
|
assert isinstance(message, (basestring, Promise)), ("%s should be a basestring or lazy translation" % repr(message))
|
2006-10-29 04:34:37 +08:00
|
|
|
message = smart_unicode(message)
|
|
|
|
self.messages = ErrorList([message])
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
# This is needed because, without a __str__(), printing an exception
|
|
|
|
# instance would result in this:
|
|
|
|
# AttributeError: ValidationError instance has no attribute 'args'
|
|
|
|
# See http://www.python.org/doc/current/tut/node10.html#handling
|
|
|
|
return repr(self.messages)
|