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