2013-11-30 03:38:13 +08:00
|
|
|
|
import json
|
2021-09-22 19:28:49 +08:00
|
|
|
|
from collections import UserList
|
2013-07-06 01:33:19 +08:00
|
|
|
|
|
2011-11-18 21:01:06 +08:00
|
|
|
|
from django.conf import settings
|
2019-07-18 14:13:01 +08:00
|
|
|
|
from django.core.exceptions import ValidationError
|
2021-09-10 15:06:01 +08:00
|
|
|
|
from django.forms.renderers import get_default_renderer
|
2017-01-08 03:13:29 +08:00
|
|
|
|
from django.utils import timezone
|
2021-09-10 15:06:01 +08:00
|
|
|
|
from django.utils.html import escape, format_html_join
|
|
|
|
|
from django.utils.safestring import mark_safe
|
2017-01-27 03:58:33 +08:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2007-01-21 04:33:23 +08:00
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
|
2015-09-09 21:09:59 +08:00
|
|
|
|
def pretty_name(name):
|
2017-01-25 05:23:56 +08:00
|
|
|
|
"""Convert 'first_name' to 'First name'."""
|
2015-09-09 21:09:59 +08:00
|
|
|
|
if not name:
|
|
|
|
|
return ''
|
|
|
|
|
return name.replace('_', ' ').capitalize()
|
|
|
|
|
|
|
|
|
|
|
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",
|
2015-06-08 17:11:05 +08:00
|
|
|
|
XML-style pairs. In the case of a boolean value, the key will appear
|
|
|
|
|
without a value. 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
|
|
|
|
|
2015-06-08 17:11:05 +08:00
|
|
|
|
The result is passed through 'mark_safe' (by way of 'format_html_join').
|
2007-05-20 02:49:35 +08:00
|
|
|
|
"""
|
2014-11-21 09:35:58 +08:00
|
|
|
|
key_value_attrs = []
|
2014-03-23 01:16:49 +08:00
|
|
|
|
boolean_attrs = []
|
2014-11-21 09:35:58 +08:00
|
|
|
|
for attr, value in attrs.items():
|
|
|
|
|
if isinstance(value, bool):
|
|
|
|
|
if value:
|
|
|
|
|
boolean_attrs.append((attr,))
|
2016-12-27 22:42:17 +08:00
|
|
|
|
elif value is not None:
|
2014-11-21 09:35:58 +08:00
|
|
|
|
key_value_attrs.append((attr, value))
|
2014-03-23 01:16:49 +08:00
|
|
|
|
|
|
|
|
|
return (
|
2014-11-27 08:41:27 +08:00
|
|
|
|
format_html_join('', ' {}="{}"', sorted(key_value_attrs)) +
|
|
|
|
|
format_html_join('', ' {}', sorted(boolean_attrs))
|
2014-03-23 01:16:49 +08:00
|
|
|
|
)
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
|
2021-09-10 15:06:01 +08:00
|
|
|
|
class RenderableMixin:
|
|
|
|
|
def get_context(self):
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
|
'Subclasses of RenderableMixin must provide a get_context() method.'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def render(self, template_name=None, context=None, renderer=None):
|
|
|
|
|
return mark_safe((renderer or self.renderer).render(
|
|
|
|
|
template_name or self.template_name,
|
|
|
|
|
context or self.get_context(),
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
__str__ = render
|
|
|
|
|
__html__ = render
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RenderableFormMixin(RenderableMixin):
|
|
|
|
|
def as_p(self):
|
|
|
|
|
"""Render as <p> elements."""
|
|
|
|
|
return self.render(self.template_name_p)
|
|
|
|
|
|
|
|
|
|
def as_table(self):
|
|
|
|
|
"""Render as <tr> elements excluding the surrounding <table> tag."""
|
|
|
|
|
return self.render(self.template_name_table)
|
|
|
|
|
|
|
|
|
|
def as_ul(self):
|
|
|
|
|
"""Render as <li> elements excluding the surrounding <ul> tag."""
|
|
|
|
|
return self.render(self.template_name_ul)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RenderableErrorMixin(RenderableMixin):
|
|
|
|
|
def as_json(self, escape_html=False):
|
|
|
|
|
return json.dumps(self.get_json_data(escape_html))
|
|
|
|
|
|
|
|
|
|
def as_text(self):
|
|
|
|
|
return self.render(self.template_name_text)
|
|
|
|
|
|
|
|
|
|
def as_ul(self):
|
|
|
|
|
return self.render(self.template_name_ul)
|
|
|
|
|
|
|
|
|
|
|
2021-09-22 19:28:49 +08:00
|
|
|
|
class ErrorDict(dict, RenderableErrorMixin):
|
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.
|
|
|
|
|
"""
|
2021-09-10 15:06:01 +08:00
|
|
|
|
template_name = 'django/forms/errors/dict/default.html'
|
|
|
|
|
template_name_text = 'django/forms/errors/dict/text.txt'
|
|
|
|
|
template_name_ul = 'django/forms/errors/dict/ul.html'
|
|
|
|
|
|
2021-09-22 19:28:49 +08:00
|
|
|
|
def __init__(self, *args, renderer=None, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
2021-09-10 15:06:01 +08:00
|
|
|
|
self.renderer = renderer or get_default_renderer()
|
|
|
|
|
|
2013-11-30 03:38:13 +08:00
|
|
|
|
def as_data(self):
|
|
|
|
|
return {f: e.as_data() for f, e in self.items()}
|
|
|
|
|
|
2017-03-10 07:45:50 +08:00
|
|
|
|
def get_json_data(self, escape_html=False):
|
|
|
|
|
return {f: e.get_json_data(escape_html) for f, e in self.items()}
|
|
|
|
|
|
2021-09-10 15:06:01 +08:00
|
|
|
|
def get_context(self):
|
|
|
|
|
return {
|
|
|
|
|
'errors': self.items(),
|
|
|
|
|
'error_class': 'errorlist',
|
|
|
|
|
}
|
2013-11-30 03:38:13 +08:00
|
|
|
|
|
2006-10-29 04:34:37 +08:00
|
|
|
|
|
2021-09-10 15:06:01 +08:00
|
|
|
|
class ErrorList(UserList, list, RenderableErrorMixin):
|
2006-10-29 04:34:37 +08:00
|
|
|
|
"""
|
|
|
|
|
A collection of errors that knows how to display itself in various formats.
|
|
|
|
|
"""
|
2021-09-10 15:06:01 +08:00
|
|
|
|
template_name = 'django/forms/errors/list/default.html'
|
|
|
|
|
template_name_text = 'django/forms/errors/list/text.txt'
|
|
|
|
|
template_name_ul = 'django/forms/errors/list/ul.html'
|
|
|
|
|
|
|
|
|
|
def __init__(self, initlist=None, error_class=None, renderer=None):
|
2017-01-21 21:13:44 +08:00
|
|
|
|
super().__init__(initlist)
|
2014-04-15 11:58:51 +08:00
|
|
|
|
|
|
|
|
|
if error_class is None:
|
|
|
|
|
self.error_class = 'errorlist'
|
|
|
|
|
else:
|
|
|
|
|
self.error_class = 'errorlist {}'.format(error_class)
|
2021-09-10 15:06:01 +08:00
|
|
|
|
self.renderer = renderer or get_default_renderer()
|
2014-04-15 11:58:51 +08:00
|
|
|
|
|
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
|
|
|
|
|
2020-02-11 05:40:07 +08:00
|
|
|
|
def copy(self):
|
|
|
|
|
copy = super().copy()
|
|
|
|
|
copy.error_class = self.error_class
|
|
|
|
|
return copy
|
|
|
|
|
|
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():
|
2017-07-31 23:02:23 +08:00
|
|
|
|
message = next(iter(error))
|
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
|
|
|
|
|
|
2021-09-10 15:06:01 +08:00
|
|
|
|
def get_context(self):
|
|
|
|
|
return {
|
|
|
|
|
'errors': self,
|
|
|
|
|
'error_class': self.error_class,
|
|
|
|
|
}
|
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 __getitem__(self, i):
|
|
|
|
|
error = self.data[i]
|
|
|
|
|
if isinstance(error, ValidationError):
|
2017-07-31 23:02:23 +08:00
|
|
|
|
return next(iter(error))
|
2017-03-04 22:47:49 +08:00
|
|
|
|
return error
|
2007-10-28 13:40:26 +08:00
|
|
|
|
|
2014-10-07 01:09:21 +08:00
|
|
|
|
def __reduce_ex__(self, *args, **kwargs):
|
|
|
|
|
# The `list` reduce function returns an iterator as the fourth element
|
|
|
|
|
# that is normally used for repopulating. Since we only inherit from
|
|
|
|
|
# `list` for `isinstance` backward compatibility (Refs #17413) we
|
|
|
|
|
# nullify this iterator as it would otherwise result in duplicate
|
|
|
|
|
# entries. (Refs #23594)
|
|
|
|
|
info = super(UserList, self).__reduce_ex__(*args, **kwargs)
|
|
|
|
|
return info[:3] + (None, None)
|
|
|
|
|
|
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:
|
2021-01-19 18:16:01 +08:00
|
|
|
|
if (
|
|
|
|
|
not timezone._is_pytz_zone(current_timezone) and
|
|
|
|
|
timezone._datetime_ambiguous_or_imaginary(value, current_timezone)
|
|
|
|
|
):
|
|
|
|
|
raise ValueError('Ambiguous or non-existent time.')
|
2011-11-18 21:01:06 +08:00
|
|
|
|
return timezone.make_aware(value, current_timezone)
|
2017-01-08 03:13:29 +08:00
|
|
|
|
except Exception as exc:
|
|
|
|
|
raise ValidationError(
|
2019-06-28 00:39:47 +08:00
|
|
|
|
_('%(datetime)s couldn’t be interpreted '
|
2017-01-08 03:13:29 +08:00
|
|
|
|
'in time zone %(current_timezone)s; it '
|
|
|
|
|
'may be ambiguous or it may not exist.'),
|
2013-06-06 02:55:05 +08:00
|
|
|
|
code='ambiguous_timezone',
|
2017-01-08 03:13:29 +08:00
|
|
|
|
params={'datetime': value, 'current_timezone': current_timezone}
|
|
|
|
|
) from exc
|
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
|
2015-09-13 18:35:56 +08:00
|
|
|
|
to naive datetimes in the current time zone for display.
|
2011-11-18 21:01:06 +08:00
|
|
|
|
"""
|
|
|
|
|
if settings.USE_TZ and value is not None and timezone.is_aware(value):
|
2018-01-19 00:21:12 +08:00
|
|
|
|
return timezone.make_naive(value)
|
2011-11-18 21:01:06 +08:00
|
|
|
|
return value
|