2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.conf import settings
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import format_html, format_html_join
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import StrAndUnicode, force_text
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-01-21 04:33:23 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
# Import ValidationError so that it can be imported from this
|
|
|
|
# module to maintain backwards compatibility.
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
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.
|
2012-07-03 07:31:14 +08:00
|
|
|
|
|
|
|
The result is passed through 'mark_safe'.
|
2007-05-20 02:49:35 +08:00
|
|
|
"""
|
2012-07-06 06:23:03 +08:00
|
|
|
return format_html_join('', ' {0}="{1}"', 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):
|
2012-06-08 00:08:47 +08:00
|
|
|
if not self: return ''
|
2012-07-06 06:23:03 +08:00
|
|
|
return format_html('<ul class="errorlist">{0}</ul>',
|
2012-07-03 07:31:14 +08:00
|
|
|
format_html_join('', '<li>{0}{1}</li>',
|
2012-07-21 16:00:10 +08:00
|
|
|
((k, force_text(v))
|
2012-07-03 07:31:14 +08:00
|
|
|
for k, v in self.items())
|
|
|
|
))
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
2012-07-21 16:00:10 +08:00
|
|
|
return '\n'.join(['* %s\n%s' % (k, '\n'.join([' * %s' % force_text(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):
|
2012-06-08 00:08:47 +08:00
|
|
|
if not self: return ''
|
2012-07-06 06:23:03 +08:00
|
|
|
return format_html('<ul class="errorlist">{0}</ul>',
|
|
|
|
format_html_join('', '<li>{0}</li>',
|
2012-07-21 16:00:10 +08:00
|
|
|
((force_text(e),) for e in self)
|
2012-07-03 07:31:14 +08:00
|
|
|
)
|
|
|
|
)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
2012-06-08 00:08:47 +08:00
|
|
|
if not self: return ''
|
2012-07-21 16:00:10 +08:00
|
|
|
return '\n'.join(['* %s' % force_text(e) for e in self])
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2007-10-28 13:40:26 +08:00
|
|
|
def __repr__(self):
|
2012-07-21 16:00:10 +08:00
|
|
|
return repr([force_text(e) for e in self])
|
2007-10-28 13:40:26 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
# Utilities for time zone support in DateTimeField et al.
|
|
|
|
|
|
|
|
def from_current_timezone(value):
|
|
|
|
"""
|
|
|
|
When time zone support is enabled, convert naive datetimes
|
|
|
|
entered in the current time zone to aware datetimes.
|
|
|
|
"""
|
|
|
|
if settings.USE_TZ and value is not None and timezone.is_naive(value):
|
|
|
|
current_timezone = timezone.get_current_timezone()
|
|
|
|
try:
|
|
|
|
return timezone.make_aware(value, current_timezone)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Exception:
|
2011-11-18 21:01:06 +08:00
|
|
|
raise ValidationError(_('%(datetime)s couldn\'t be interpreted '
|
|
|
|
'in time zone %(current_timezone)s; it '
|
|
|
|
'may be ambiguous or it may not exist.')
|
|
|
|
% {'datetime': value,
|
|
|
|
'current_timezone': current_timezone})
|
|
|
|
return value
|
|
|
|
|
|
|
|
def to_current_timezone(value):
|
|
|
|
"""
|
|
|
|
When time zone support is enabled, convert aware datetimes
|
|
|
|
to naive dateimes in the current time zone for display.
|
|
|
|
"""
|
|
|
|
if settings.USE_TZ and value is not None and timezone.is_aware(value):
|
|
|
|
current_timezone = timezone.get_current_timezone()
|
|
|
|
return timezone.make_naive(value, current_timezone)
|
|
|
|
return value
|