测试gitnore
This commit is contained in:
@@ -3,18 +3,16 @@ from collections import UserList
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms.renderers import get_default_renderer
|
||||
from django.utils import timezone
|
||||
from django.utils.html import escape, format_html_join
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils.html import escape, format_html, format_html_join, html_safe
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
def pretty_name(name):
|
||||
"""Convert 'first_name' to 'First name'."""
|
||||
if not name:
|
||||
return ""
|
||||
return name.replace("_", " ").capitalize()
|
||||
return ''
|
||||
return name.replace('_', ' ').capitalize()
|
||||
|
||||
|
||||
def flatatt(attrs):
|
||||
@@ -37,99 +35,59 @@ def flatatt(attrs):
|
||||
elif value is not None:
|
||||
key_value_attrs.append((attr, value))
|
||||
|
||||
return format_html_join("", ' {}="{}"', sorted(key_value_attrs)) + format_html_join(
|
||||
"", " {}", sorted(boolean_attrs)
|
||||
return (
|
||||
format_html_join('', ' {}="{}"', sorted(key_value_attrs)) +
|
||||
format_html_join('', ' {}', sorted(boolean_attrs))
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
class ErrorDict(dict, RenderableErrorMixin):
|
||||
@html_safe
|
||||
class ErrorDict(dict):
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
|
||||
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"
|
||||
|
||||
def __init__(self, *args, renderer=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.renderer = renderer or get_default_renderer()
|
||||
|
||||
def as_data(self):
|
||||
return {f: e.as_data() for f, e in self.items()}
|
||||
|
||||
def get_json_data(self, escape_html=False):
|
||||
return {f: e.get_json_data(escape_html) for f, e in self.items()}
|
||||
|
||||
def get_context(self):
|
||||
return {
|
||||
"errors": self.items(),
|
||||
"error_class": "errorlist",
|
||||
}
|
||||
def as_json(self, escape_html=False):
|
||||
return json.dumps(self.get_json_data(escape_html))
|
||||
|
||||
def as_ul(self):
|
||||
if not self:
|
||||
return ''
|
||||
return format_html(
|
||||
'<ul class="errorlist">{}</ul>',
|
||||
format_html_join('', '<li>{}{}</li>', self.items())
|
||||
)
|
||||
|
||||
def as_text(self):
|
||||
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()
|
||||
|
||||
|
||||
class ErrorList(UserList, list, RenderableErrorMixin):
|
||||
@html_safe
|
||||
class ErrorList(UserList, list):
|
||||
"""
|
||||
A collection of errors that knows how to display itself in various formats.
|
||||
"""
|
||||
|
||||
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):
|
||||
def __init__(self, initlist=None, error_class=None):
|
||||
super().__init__(initlist)
|
||||
|
||||
if error_class is None:
|
||||
self.error_class = "errorlist"
|
||||
self.error_class = 'errorlist'
|
||||
else:
|
||||
self.error_class = "errorlist {}".format(error_class)
|
||||
self.renderer = renderer or get_default_renderer()
|
||||
self.error_class = 'errorlist {}'.format(error_class)
|
||||
|
||||
def as_data(self):
|
||||
return ValidationError(self.data).error_list
|
||||
@@ -143,19 +101,30 @@ class ErrorList(UserList, list, RenderableErrorMixin):
|
||||
errors = []
|
||||
for error in self.as_data():
|
||||
message = next(iter(error))
|
||||
errors.append(
|
||||
{
|
||||
"message": escape(message) if escape_html else message,
|
||||
"code": error.code or "",
|
||||
}
|
||||
)
|
||||
errors.append({
|
||||
'message': escape(message) if escape_html else message,
|
||||
'code': error.code or '',
|
||||
})
|
||||
return errors
|
||||
|
||||
def get_context(self):
|
||||
return {
|
||||
"errors": self,
|
||||
"error_class": self.error_class,
|
||||
}
|
||||
def as_json(self, escape_html=False):
|
||||
return json.dumps(self.get_json_data(escape_html))
|
||||
|
||||
def as_ul(self):
|
||||
if not self.data:
|
||||
return ''
|
||||
|
||||
return format_html(
|
||||
'<ul class="{}">{}</ul>',
|
||||
self.error_class,
|
||||
format_html_join('', '<li>{}</li>', ((e,) for e in self))
|
||||
)
|
||||
|
||||
def as_text(self):
|
||||
return '\n'.join('* %s' % e for e in self)
|
||||
|
||||
def __str__(self):
|
||||
return self.as_ul()
|
||||
|
||||
def __repr__(self):
|
||||
return repr(list(self))
|
||||
@@ -184,7 +153,6 @@ class ErrorList(UserList, list, RenderableErrorMixin):
|
||||
|
||||
# Utilities for time zone support in DateTimeField et al.
|
||||
|
||||
|
||||
def from_current_timezone(value):
|
||||
"""
|
||||
When time zone support is enabled, convert naive datetimes
|
||||
@@ -193,20 +161,19 @@ def from_current_timezone(value):
|
||||
if settings.USE_TZ and value is not None and timezone.is_naive(value):
|
||||
current_timezone = timezone.get_current_timezone()
|
||||
try:
|
||||
if not timezone._is_pytz_zone(
|
||||
current_timezone
|
||||
) and timezone._datetime_ambiguous_or_imaginary(value, current_timezone):
|
||||
raise ValueError("Ambiguous or non-existent time.")
|
||||
if (
|
||||
not timezone._is_pytz_zone(current_timezone) and
|
||||
timezone._datetime_ambiguous_or_imaginary(value, current_timezone)
|
||||
):
|
||||
raise ValueError('Ambiguous or non-existent time.')
|
||||
return timezone.make_aware(value, current_timezone)
|
||||
except Exception as exc:
|
||||
raise ValidationError(
|
||||
_(
|
||||
"%(datetime)s couldn’t be interpreted "
|
||||
"in time zone %(current_timezone)s; it "
|
||||
"may be ambiguous or it may not exist."
|
||||
),
|
||||
code="ambiguous_timezone",
|
||||
params={"datetime": value, "current_timezone": current_timezone},
|
||||
_('%(datetime)s couldn’t be interpreted '
|
||||
'in time zone %(current_timezone)s; it '
|
||||
'may be ambiguous or it may not exist.'),
|
||||
code='ambiguous_timezone',
|
||||
params={'datetime': value, 'current_timezone': current_timezone}
|
||||
) from exc
|
||||
return value
|
||||
|
||||
|
||||
Reference in New Issue
Block a user