diff options
| author | Tim Graham <timograham@gmail.com> | 2013-09-16 12:52:05 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-09-16 12:52:05 -0400 |
| commit | 18ffdb1772ba60e085cff8fd9a1d4a7b129b4032 (patch) | |
| tree | 69ea529302e5c498e6b020b03c30e2ccf4a21936 /django/forms/utils.py | |
| parent | 8d29005524b141da8ff4d9b9bc4d858d43bb7154 (diff) | |
Fixed #17627 -- Renamed util.py files to utils.py
Thanks PaulM for the suggestion and Luke Granger-Brown and
Wiktor KoĆodziej for the initial patch.
Diffstat (limited to 'django/forms/utils.py')
| -rw-r--r-- | django/forms/utils.py | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/django/forms/utils.py b/django/forms/utils.py new file mode 100644 index 0000000000..bb5bb8c2e4 --- /dev/null +++ b/django/forms/utils.py @@ -0,0 +1,117 @@ +from __future__ import unicode_literals + +import warnings + +from django.conf import settings +from django.utils.html import format_html, format_html_join +from django.utils.encoding import force_text, python_2_unicode_compatible +from django.utils import timezone +from django.utils.translation import ugettext_lazy as _ +from django.utils import six +import sys + +# Import ValidationError so that it can be imported from this +# module to maintain backwards compatibility. +from django.core.exceptions import ValidationError + +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. + + The result is passed through 'mark_safe'. + """ + for attr_name, value in attrs.items(): + if type(value) is bool: + warnings.warn( + "In Django 1.8, widget attribute %(attr_name)s=%(bool_value)s " + "will %(action)s. To preserve current behavior, use the " + "string '%(bool_value)s' instead of the boolean value." % { + 'attr_name': attr_name, + 'action': "be rendered as '%s'" % attr_name if value else "not be rendered", + 'bool_value': value, + }, + DeprecationWarning + ) + return format_html_join('', ' {0}="{1}"', sorted(attrs.items())) + +@python_2_unicode_compatible +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. + """ + def __str__(self): + return self.as_ul() + + def as_ul(self): + if not self: return '' + 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()) + )) + + def as_text(self): + return '\n'.join('* %s\n%s' % (k, '\n'.join(' * %s' % force_text(i) for i in v)) for k, v in self.items()) + +@python_2_unicode_compatible +class ErrorList(list): + """ + A collection of errors that knows how to display itself in various formats. + """ + def __str__(self): + return self.as_ul() + + def as_ul(self): + if not self: return '' + return format_html('<ul class="errorlist">{0}</ul>', + format_html_join('', '<li>{0}</li>', + ((force_text(e),) for e in self) + ) + ) + + def as_text(self): + if not self: return '' + return '\n'.join('* %s' % force_text(e) for e in self) + + def __repr__(self): + return repr([force_text(e) for e in self]) + +# 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) + except Exception: + message = _( + '%(datetime)s couldn\'t be interpreted ' + 'in time zone %(current_timezone)s; it ' + '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]) + 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 |
