mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
[py3] Ported django.utils.encoding.
* Renamed smart_unicode to smart_text (but kept the old name under Python 2 for backwards compatibility). * Renamed smart_str to smart_bytes. * Re-introduced smart_str as an alias for smart_text under Python 3 and smart_bytes under Python 2 (which is backwards compatible). Thus smart_str always returns a str objects. * Used the new smart_str in a few places where both Python 2 and 3 want a str.
This commit is contained in:
@@ -23,7 +23,7 @@ from django.forms.widgets import (TextInput, PasswordInput, HiddenInput,
|
||||
NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput,
|
||||
SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION)
|
||||
from django.utils import formats
|
||||
from django.utils.encoding import smart_unicode, force_unicode
|
||||
from django.utils.encoding import smart_text, force_text
|
||||
from django.utils.ipv6 import clean_ipv6_address
|
||||
from django.utils import six
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@@ -78,13 +78,13 @@ class Field(object):
|
||||
# validators -- List of addtional validators to use
|
||||
# localize -- Boolean that specifies if the field should be localized.
|
||||
if label is not None:
|
||||
label = smart_unicode(label)
|
||||
label = smart_text(label)
|
||||
self.required, self.label, self.initial = required, label, initial
|
||||
self.show_hidden_initial = show_hidden_initial
|
||||
if help_text is None:
|
||||
self.help_text = ''
|
||||
else:
|
||||
self.help_text = smart_unicode(help_text)
|
||||
self.help_text = smart_text(help_text)
|
||||
widget = widget or self.widget
|
||||
if isinstance(widget, type):
|
||||
widget = widget()
|
||||
@@ -195,7 +195,7 @@ class CharField(Field):
|
||||
"Returns a Unicode object."
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return ''
|
||||
return smart_unicode(value)
|
||||
return smart_text(value)
|
||||
|
||||
def widget_attrs(self, widget):
|
||||
attrs = super(CharField, self).widget_attrs(widget)
|
||||
@@ -288,7 +288,7 @@ class DecimalField(Field):
|
||||
return None
|
||||
if self.localize:
|
||||
value = formats.sanitize_separators(value)
|
||||
value = smart_unicode(value).strip()
|
||||
value = smart_text(value).strip()
|
||||
try:
|
||||
value = Decimal(value)
|
||||
except DecimalException:
|
||||
@@ -333,7 +333,7 @@ class BaseTemporalField(Field):
|
||||
|
||||
def to_python(self, value):
|
||||
# Try to coerce the value to unicode.
|
||||
unicode_value = force_unicode(value, strings_only=True)
|
||||
unicode_value = force_text(value, strings_only=True)
|
||||
if isinstance(unicode_value, six.text_type):
|
||||
value = unicode_value.strip()
|
||||
# If unicode, try to strptime against each input format.
|
||||
@@ -692,7 +692,7 @@ class ChoiceField(Field):
|
||||
"Returns a Unicode object."
|
||||
if value in validators.EMPTY_VALUES:
|
||||
return ''
|
||||
return smart_unicode(value)
|
||||
return smart_text(value)
|
||||
|
||||
def validate(self, value):
|
||||
"""
|
||||
@@ -708,10 +708,10 @@ class ChoiceField(Field):
|
||||
if isinstance(v, (list, tuple)):
|
||||
# This is an optgroup, so look inside the group for options
|
||||
for k2, v2 in v:
|
||||
if value == smart_unicode(k2):
|
||||
if value == smart_text(k2):
|
||||
return True
|
||||
else:
|
||||
if value == smart_unicode(k):
|
||||
if value == smart_text(k):
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -752,7 +752,7 @@ class MultipleChoiceField(ChoiceField):
|
||||
return []
|
||||
elif not isinstance(value, (list, tuple)):
|
||||
raise ValidationError(self.error_messages['invalid_list'])
|
||||
return [smart_unicode(val) for val in value]
|
||||
return [smart_text(val) for val in value]
|
||||
|
||||
def validate(self, value):
|
||||
"""
|
||||
|
||||
@@ -12,7 +12,7 @@ from django.forms.util import flatatt, ErrorDict, ErrorList
|
||||
from django.forms.widgets import Media, media_property, TextInput, Textarea
|
||||
from django.utils.datastructures import SortedDict
|
||||
from django.utils.html import conditional_escape, format_html
|
||||
from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
|
||||
from django.utils.encoding import StrAndUnicode, smart_text, force_text
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils import six
|
||||
|
||||
@@ -150,7 +150,7 @@ class BaseForm(StrAndUnicode):
|
||||
bf_errors = self.error_class([conditional_escape(error) for error in bf.errors]) # Escape and cache in local variable.
|
||||
if bf.is_hidden:
|
||||
if bf_errors:
|
||||
top_errors.extend(['(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
|
||||
top_errors.extend(['(Hidden field %s) %s' % (name, force_text(e)) for e in bf_errors])
|
||||
hidden_fields.append(six.text_type(bf))
|
||||
else:
|
||||
# Create a 'class="..."' atribute if the row should have any
|
||||
@@ -160,10 +160,10 @@ class BaseForm(StrAndUnicode):
|
||||
html_class_attr = ' class="%s"' % css_classes
|
||||
|
||||
if errors_on_separate_row and bf_errors:
|
||||
output.append(error_row % force_unicode(bf_errors))
|
||||
output.append(error_row % force_text(bf_errors))
|
||||
|
||||
if bf.label:
|
||||
label = conditional_escape(force_unicode(bf.label))
|
||||
label = conditional_escape(force_text(bf.label))
|
||||
# Only add the suffix if the label does not end in
|
||||
# punctuation.
|
||||
if self.label_suffix:
|
||||
@@ -174,20 +174,20 @@ class BaseForm(StrAndUnicode):
|
||||
label = ''
|
||||
|
||||
if field.help_text:
|
||||
help_text = help_text_html % force_unicode(field.help_text)
|
||||
help_text = help_text_html % force_text(field.help_text)
|
||||
else:
|
||||
help_text = ''
|
||||
|
||||
output.append(normal_row % {
|
||||
'errors': force_unicode(bf_errors),
|
||||
'label': force_unicode(label),
|
||||
'errors': force_text(bf_errors),
|
||||
'label': force_text(label),
|
||||
'field': six.text_type(bf),
|
||||
'help_text': help_text,
|
||||
'html_class_attr': html_class_attr
|
||||
})
|
||||
|
||||
if top_errors:
|
||||
output.insert(0, error_row % force_unicode(top_errors))
|
||||
output.insert(0, error_row % force_text(top_errors))
|
||||
|
||||
if hidden_fields: # Insert any hidden fields in the last row.
|
||||
str_hidden = ''.join(hidden_fields)
|
||||
@@ -535,8 +535,8 @@ class BoundField(StrAndUnicode):
|
||||
associated Form has specified auto_id. Returns an empty string otherwise.
|
||||
"""
|
||||
auto_id = self.form.auto_id
|
||||
if auto_id and '%s' in smart_unicode(auto_id):
|
||||
return smart_unicode(auto_id) % self.html_name
|
||||
if auto_id and '%s' in smart_text(auto_id):
|
||||
return smart_text(auto_id) % self.html_name
|
||||
elif auto_id:
|
||||
return self.html_name
|
||||
return ''
|
||||
|
||||
@@ -13,7 +13,7 @@ from django.forms.formsets import BaseFormSet, formset_factory
|
||||
from django.forms.util import ErrorList
|
||||
from django.forms.widgets import (SelectMultiple, HiddenInput,
|
||||
MultipleHiddenInput, media_property)
|
||||
from django.utils.encoding import smart_unicode, force_unicode
|
||||
from django.utils.encoding import smart_text, force_text
|
||||
from django.utils.datastructures import SortedDict
|
||||
from django.utils import six
|
||||
from django.utils.text import get_text_list, capfirst
|
||||
@@ -875,7 +875,7 @@ class InlineForeignKeyField(Field):
|
||||
orig = getattr(self.parent_instance, self.to_field)
|
||||
else:
|
||||
orig = self.parent_instance.pk
|
||||
if force_unicode(value) != force_unicode(orig):
|
||||
if force_text(value) != force_text(orig):
|
||||
raise ValidationError(self.error_messages['invalid_choice'])
|
||||
return self.parent_instance
|
||||
|
||||
@@ -953,7 +953,7 @@ class ModelChoiceField(ChoiceField):
|
||||
generate the labels for the choices presented by this object. Subclasses
|
||||
can override this method to customize the display of the choices.
|
||||
"""
|
||||
return smart_unicode(obj)
|
||||
return smart_text(obj)
|
||||
|
||||
def _get_choices(self):
|
||||
# If self._choices is set, then somebody must have manually set
|
||||
@@ -1025,9 +1025,9 @@ class ModelMultipleChoiceField(ModelChoiceField):
|
||||
except ValueError:
|
||||
raise ValidationError(self.error_messages['invalid_pk_value'] % pk)
|
||||
qs = self.queryset.filter(**{'%s__in' % key: value})
|
||||
pks = set([force_unicode(getattr(o, key)) for o in qs])
|
||||
pks = set([force_text(getattr(o, key)) for o in qs])
|
||||
for val in value:
|
||||
if force_unicode(val) not in pks:
|
||||
if force_text(val) not in pks:
|
||||
raise ValidationError(self.error_messages['invalid_choice'] % val)
|
||||
# Since this overrides the inherited ModelChoiceField.clean
|
||||
# we run custom validators here
|
||||
|
||||
@@ -2,7 +2,7 @@ from __future__ import unicode_literals
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.html import format_html, format_html_join
|
||||
from django.utils.encoding import StrAndUnicode, force_unicode
|
||||
from django.utils.encoding import StrAndUnicode, force_text
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils import timezone
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@@ -35,12 +35,12 @@ class ErrorDict(dict, StrAndUnicode):
|
||||
if not self: return ''
|
||||
return format_html('<ul class="errorlist">{0}</ul>',
|
||||
format_html_join('', '<li>{0}{1}</li>',
|
||||
((k, force_unicode(v))
|
||||
((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_unicode(i) for i in v])) for k, v in self.items()])
|
||||
return '\n'.join(['* %s\n%s' % (k, '\n'.join([' * %s' % force_text(i) for i in v])) for k, v in self.items()])
|
||||
|
||||
class ErrorList(list, StrAndUnicode):
|
||||
"""
|
||||
@@ -53,16 +53,16 @@ class ErrorList(list, StrAndUnicode):
|
||||
if not self: return ''
|
||||
return format_html('<ul class="errorlist">{0}</ul>',
|
||||
format_html_join('', '<li>{0}</li>',
|
||||
((force_unicode(e),) for e in self)
|
||||
((force_text(e),) for e in self)
|
||||
)
|
||||
)
|
||||
|
||||
def as_text(self):
|
||||
if not self: return ''
|
||||
return '\n'.join(['* %s' % force_unicode(e) for e in self])
|
||||
return '\n'.join(['* %s' % force_text(e) for e in self])
|
||||
|
||||
def __repr__(self):
|
||||
return repr([force_unicode(e) for e in self])
|
||||
return repr([force_text(e) for e in self])
|
||||
|
||||
# Utilities for time zone support in DateTimeField et al.
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ from django.forms.util import flatatt, to_current_timezone
|
||||
from django.utils.datastructures import MultiValueDict, MergeDict
|
||||
from django.utils.html import conditional_escape, format_html, format_html_join
|
||||
from django.utils.translation import ugettext, ugettext_lazy
|
||||
from django.utils.encoding import StrAndUnicode, force_unicode
|
||||
from django.utils.encoding import StrAndUnicode, force_text
|
||||
from django.utils.safestring import mark_safe
|
||||
from django.utils import six
|
||||
from django.utils import datetime_safe, formats
|
||||
@@ -223,7 +223,7 @@ class Widget(six.with_metaclass(MediaDefiningClass)):
|
||||
initial_value = ''
|
||||
else:
|
||||
initial_value = initial
|
||||
if force_unicode(initial_value) != force_unicode(data_value):
|
||||
if force_text(initial_value) != force_text(data_value):
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -257,7 +257,7 @@ class Input(Widget):
|
||||
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
|
||||
if value != '':
|
||||
# Only add the 'value' attribute if a value is non-empty.
|
||||
final_attrs['value'] = force_unicode(self._format_value(value))
|
||||
final_attrs['value'] = force_text(self._format_value(value))
|
||||
return format_html('<input{0} />', flatatt(final_attrs))
|
||||
|
||||
class TextInput(Input):
|
||||
@@ -294,7 +294,7 @@ class MultipleHiddenInput(HiddenInput):
|
||||
id_ = final_attrs.get('id', None)
|
||||
inputs = []
|
||||
for i, v in enumerate(value):
|
||||
input_attrs = dict(value=force_unicode(v), **final_attrs)
|
||||
input_attrs = dict(value=force_text(v), **final_attrs)
|
||||
if id_:
|
||||
# An ID attribute was given. Add a numeric index as a suffix
|
||||
# so that the inputs don't all have the same ID attribute.
|
||||
@@ -361,7 +361,7 @@ class ClearableFileInput(FileInput):
|
||||
template = self.template_with_initial
|
||||
substitutions['initial'] = format_html('<a href="{0}">{1}</a>',
|
||||
value.url,
|
||||
force_unicode(value))
|
||||
force_text(value))
|
||||
if not self.is_required:
|
||||
checkbox_name = self.clear_checkbox_name(name)
|
||||
checkbox_id = self.clear_checkbox_id(checkbox_name)
|
||||
@@ -398,7 +398,7 @@ class Textarea(Widget):
|
||||
final_attrs = self.build_attrs(attrs, name=name)
|
||||
return format_html('<textarea{0}>{1}</textarea>',
|
||||
flatatt(final_attrs),
|
||||
force_unicode(value))
|
||||
force_text(value))
|
||||
|
||||
class DateInput(Input):
|
||||
input_type = 'text'
|
||||
@@ -515,7 +515,7 @@ class CheckboxInput(Widget):
|
||||
final_attrs['checked'] = 'checked'
|
||||
if not (value is True or value is False or value is None or value == ''):
|
||||
# Only add the 'value' attribute if a value is non-empty.
|
||||
final_attrs['value'] = force_unicode(value)
|
||||
final_attrs['value'] = force_text(value)
|
||||
return format_html('<input{0} />', flatatt(final_attrs))
|
||||
|
||||
def value_from_datadict(self, data, files, name):
|
||||
@@ -556,7 +556,7 @@ class Select(Widget):
|
||||
return mark_safe('\n'.join(output))
|
||||
|
||||
def render_option(self, selected_choices, option_value, option_label):
|
||||
option_value = force_unicode(option_value)
|
||||
option_value = force_text(option_value)
|
||||
if option_value in selected_choices:
|
||||
selected_html = mark_safe(' selected="selected"')
|
||||
if not self.allow_multiple_selected:
|
||||
@@ -567,15 +567,15 @@ class Select(Widget):
|
||||
return format_html('<option value="{0}"{1}>{2}</option>',
|
||||
option_value,
|
||||
selected_html,
|
||||
force_unicode(option_label))
|
||||
force_text(option_label))
|
||||
|
||||
def render_options(self, choices, selected_choices):
|
||||
# Normalize to strings.
|
||||
selected_choices = set(force_unicode(v) for v in selected_choices)
|
||||
selected_choices = set(force_text(v) for v in selected_choices)
|
||||
output = []
|
||||
for option_value, option_label in chain(self.choices, choices):
|
||||
if isinstance(option_label, (list, tuple)):
|
||||
output.append(format_html('<optgroup label="{0}">', force_unicode(option_value)))
|
||||
output.append(format_html('<optgroup label="{0}">', force_text(option_value)))
|
||||
for option in option_label:
|
||||
output.append(self.render_option(selected_choices, *option))
|
||||
output.append('</optgroup>')
|
||||
@@ -643,8 +643,8 @@ class SelectMultiple(Select):
|
||||
data = []
|
||||
if len(initial) != len(data):
|
||||
return True
|
||||
initial_set = set([force_unicode(value) for value in initial])
|
||||
data_set = set([force_unicode(value) for value in data])
|
||||
initial_set = set([force_text(value) for value in initial])
|
||||
data_set = set([force_text(value) for value in data])
|
||||
return data_set != initial_set
|
||||
|
||||
class RadioInput(SubWidget):
|
||||
@@ -656,8 +656,8 @@ class RadioInput(SubWidget):
|
||||
def __init__(self, name, value, attrs, choice, index):
|
||||
self.name, self.value = name, value
|
||||
self.attrs = attrs
|
||||
self.choice_value = force_unicode(choice[0])
|
||||
self.choice_label = force_unicode(choice[1])
|
||||
self.choice_value = force_text(choice[0])
|
||||
self.choice_label = force_text(choice[1])
|
||||
self.index = index
|
||||
|
||||
def __unicode__(self):
|
||||
@@ -671,7 +671,7 @@ class RadioInput(SubWidget):
|
||||
label_for = format_html(' for="{0}_{1}"', self.attrs['id'], self.index)
|
||||
else:
|
||||
label_for = ''
|
||||
choice_label = force_unicode(self.choice_label)
|
||||
choice_label = force_text(self.choice_label)
|
||||
return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), choice_label)
|
||||
|
||||
def is_checked(self):
|
||||
@@ -709,7 +709,7 @@ class RadioFieldRenderer(StrAndUnicode):
|
||||
"""Outputs a <ul> for this set of radio fields."""
|
||||
return format_html('<ul>\n{0}\n</ul>',
|
||||
format_html_join('\n', '<li>{0}</li>',
|
||||
[(force_unicode(w),) for w in self]
|
||||
[(force_text(w),) for w in self]
|
||||
))
|
||||
|
||||
class RadioSelect(Select):
|
||||
@@ -729,7 +729,7 @@ class RadioSelect(Select):
|
||||
def get_renderer(self, name, value, attrs=None, choices=()):
|
||||
"""Returns an instance of the renderer."""
|
||||
if value is None: value = ''
|
||||
str_value = force_unicode(value) # Normalize to string.
|
||||
str_value = force_text(value) # Normalize to string.
|
||||
final_attrs = self.build_attrs(attrs)
|
||||
choices = list(chain(self.choices, choices))
|
||||
return self.renderer(name, str_value, final_attrs, choices)
|
||||
@@ -753,7 +753,7 @@ class CheckboxSelectMultiple(SelectMultiple):
|
||||
final_attrs = self.build_attrs(attrs, name=name)
|
||||
output = ['<ul>']
|
||||
# Normalize to strings
|
||||
str_values = set([force_unicode(v) for v in value])
|
||||
str_values = set([force_text(v) for v in value])
|
||||
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
|
||||
# If an ID attribute was given, add a numeric index as a suffix,
|
||||
# so that the checkboxes don't all have the same ID attribute.
|
||||
@@ -764,9 +764,9 @@ class CheckboxSelectMultiple(SelectMultiple):
|
||||
label_for = ''
|
||||
|
||||
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
|
||||
option_value = force_unicode(option_value)
|
||||
option_value = force_text(option_value)
|
||||
rendered_cb = cb.render(name, option_value)
|
||||
option_label = force_unicode(option_label)
|
||||
option_label = force_text(option_label)
|
||||
output.append(format_html('<li><label{0}>{1} {2}</label></li>',
|
||||
label_for, rendered_cb, option_label))
|
||||
output.append('</ul>')
|
||||
|
||||
Reference in New Issue
Block a user