2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-11-30 03:38:13 +08:00
|
|
|
import json
|
|
|
|
import sys
|
2013-07-06 01:33:19 +08:00
|
|
|
|
2014-03-01 02:53:32 +08:00
|
|
|
try:
|
|
|
|
from collections import UserList
|
|
|
|
except ImportError: # Python 2
|
|
|
|
from UserList import UserList
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.conf import settings
|
2012-08-12 18:32:08 +08:00
|
|
|
from django.utils.encoding import force_text, python_2_unicode_compatible
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.html import format_html, format_html_join, escape
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2013-03-19 13:04:59 +08:00
|
|
|
from django.utils import six
|
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
|
|
|
|
|
2013-11-03 04:12:09 +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.
|
2012-07-03 07:31:14 +08:00
|
|
|
|
|
|
|
The result is passed through 'mark_safe'.
|
2007-05-20 02:49:35 +08:00
|
|
|
"""
|
2014-03-23 01:16:49 +08:00
|
|
|
boolean_attrs = []
|
|
|
|
for attr, value in list(attrs.items()):
|
|
|
|
if value is True:
|
|
|
|
boolean_attrs.append((attr,))
|
|
|
|
del attrs[attr]
|
|
|
|
elif value is False:
|
|
|
|
del attrs[attr]
|
|
|
|
|
|
|
|
return (
|
|
|
|
format_html_join('', ' {0}="{1}"', sorted(attrs.items())) +
|
|
|
|
format_html_join('', ' {0}', sorted(boolean_attrs))
|
|
|
|
)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
|
|
|
class ErrorDict(dict):
|
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.
|
|
|
|
"""
|
2013-11-30 03:38:13 +08:00
|
|
|
def as_data(self):
|
|
|
|
return {f: e.as_data() for f, e in self.items()}
|
|
|
|
|
2014-02-19 03:00:09 +08:00
|
|
|
def as_json(self, escape_html=False):
|
2014-03-01 02:53:32 +08:00
|
|
|
return json.dumps({f: e.get_json_data(escape_html) for f, e in self.items()})
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_ul(self):
|
2013-10-17 16:17:41 +08:00
|
|
|
if not self:
|
|
|
|
return ''
|
2013-12-09 01:20:06 +08:00
|
|
|
return format_html(
|
|
|
|
'<ul class="errorlist">{0}</ul>',
|
|
|
|
format_html_join('', '<li>{0}{1}</li>', ((k, force_text(v)) for k, v in self.items()))
|
|
|
|
)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
2013-11-30 03:38:13 +08:00
|
|
|
output = []
|
|
|
|
for field, errors in self.items():
|
|
|
|
output.append('* %s' % field)
|
|
|
|
output.append('\n'.join(' * %s' % e for e in errors))
|
|
|
|
return '\n'.join(output)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.as_ul()
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2012-08-12 18:32:08 +08:00
|
|
|
@python_2_unicode_compatible
|
2013-12-18 10:33:41 +08:00
|
|
|
class ErrorList(UserList, list):
|
2006-10-29 04:34:37 +08:00
|
|
|
"""
|
|
|
|
A collection of errors that knows how to display itself in various formats.
|
|
|
|
"""
|
2013-11-30 03:38:13 +08:00
|
|
|
def as_data(self):
|
2014-03-01 02:53:32 +08:00
|
|
|
return ValidationError(self.data).error_list
|
2013-11-30 03:38:13 +08:00
|
|
|
|
2014-03-01 02:53:32 +08:00
|
|
|
def get_json_data(self, escape_html=False):
|
2013-11-30 03:38:13 +08:00
|
|
|
errors = []
|
2014-03-01 02:53:32 +08:00
|
|
|
for error in self.as_data():
|
2014-02-19 03:00:09 +08:00
|
|
|
message = list(error)[0]
|
2013-11-30 03:38:13 +08:00
|
|
|
errors.append({
|
2014-02-19 03:00:09 +08:00
|
|
|
'message': escape(message) if escape_html else message,
|
2013-11-30 03:38:13 +08:00
|
|
|
'code': error.code or '',
|
|
|
|
})
|
2014-03-01 02:53:32 +08:00
|
|
|
return errors
|
|
|
|
|
|
|
|
def as_json(self, escape_html=False):
|
|
|
|
return json.dumps(self.get_json_data(escape_html))
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_ul(self):
|
2013-11-30 03:38:13 +08:00
|
|
|
if not self.data:
|
2013-10-17 16:17:41 +08:00
|
|
|
return ''
|
2013-12-09 01:20:06 +08:00
|
|
|
return format_html(
|
|
|
|
'<ul class="errorlist">{0}</ul>',
|
|
|
|
format_html_join('', '<li>{0}</li>', ((force_text(e),) for e in self))
|
|
|
|
)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
|
|
def as_text(self):
|
2013-11-30 03:38:13 +08:00
|
|
|
return '\n'.join('* %s' % e for e in self)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.as_ul()
|
2006-10-29 04:34:37 +08:00
|
|
|
|
2007-10-28 13:40:26 +08:00
|
|
|
def __repr__(self):
|
2013-11-30 03:38:13 +08:00
|
|
|
return repr(list(self))
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in list(self)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return list(self) == other
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return list(self) != other
|
|
|
|
|
|
|
|
def __getitem__(self, i):
|
|
|
|
error = self.data[i]
|
|
|
|
if isinstance(error, ValidationError):
|
|
|
|
return list(error)[0]
|
|
|
|
return force_text(error)
|
2007-10-28 13:40:26 +08:00
|
|
|
|
2013-11-03 04:12:09 +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:
|
2013-06-06 02:55:05 +08:00
|
|
|
message = _(
|
2013-03-19 13:04:59 +08:00
|
|
|
'%(datetime)s couldn\'t be interpreted '
|
|
|
|
'in time zone %(current_timezone)s; it '
|
2013-06-06 02:55:05 +08:00
|
|
|
'may be ambiguous or it may not exist.'
|
|
|
|
)
|
|
|
|
params = {'datetime': value, 'current_timezone': current_timezone}
|
|
|
|
six.reraise(ValidationError, ValidationError(
|
|
|
|
message,
|
|
|
|
code='ambiguous_timezone',
|
|
|
|
params=params,
|
|
|
|
), sys.exc_info()[2])
|
2011-11-18 21:01:06 +08:00
|
|
|
return value
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
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
|