Permalink
Browse files

Refs #27795 -- Replaced many force_text() with str()

Thanks Tim Graham for the review.
  • Loading branch information...
claudep committed Apr 21, 2017
1 parent 8ab7ce8 commit 301de774c21d055e9e5a7073e5bffdb52bc71079
Showing with 135 additions and 181 deletions.
  1. +2 −3 django/contrib/admin/actions.py
  2. +4 −5 django/contrib/admin/filters.py
  3. +2 −3 django/contrib/admin/models.py
  4. +8 −9 django/contrib/admin/options.py
  5. +2 −3 django/contrib/admin/templatetags/admin_list.py
  6. +9 −10 django/contrib/admin/utils.py
  7. +1 −2 django/contrib/admin/widgets.py
  8. +1 −2 django/contrib/auth/mixins.py
  9. +1 −2 django/contrib/auth/password_validation.py
  10. +1 −2 django/contrib/contenttypes/fields.py
  11. +1 −2 django/contrib/contenttypes/models.py
  12. +3 −4 django/contrib/postgres/fields/hstore.py
  13. +1 −2 django/contrib/postgres/lookups.py
  14. +2 −2 django/contrib/sessions/backends/base.py
  15. +1 −2 django/contrib/sessions/backends/cached_db.py
  16. +1 −2 django/contrib/sessions/backends/db.py
  17. +1 −2 django/contrib/sessions/backends/file.py
  18. +6 −6 django/contrib/syndication/views.py
  19. +1 −3 django/core/checks/messages.py
  20. +1 −2 django/core/exceptions.py
  21. +1 −2 django/core/handlers/exception.py
  22. +5 −5 django/core/mail/message.py
  23. +7 −8 django/core/serializers/xml_serializer.py
  24. +1 −3 django/core/validators.py
  25. +1 −1 django/db/backends/base/operations.py
  26. +1 −2 django/db/backends/sqlite3/base.py
  27. +1 −2 django/db/migrations/serializer.py
  28. +5 −5 django/db/models/fields/__init__.py
  29. +1 −2 django/db/models/fields/reverse_related.py
  30. +1 −2 django/db/models/options.py
  31. +3 −4 django/forms/boundfield.py
  32. +11 −12 django/forms/fields.py
  33. +3 −4 django/forms/forms.py
  34. +7 −8 django/forms/models.py
  35. +3 −4 django/forms/widgets.py
  36. +2 −2 django/http/response.py
  37. +1 −2 django/shortcuts.py
  38. +4 −5 django/urls/resolvers.py
  39. +2 −3 django/utils/dateformat.py
  40. +4 −4 django/utils/html.py
  41. +10 −13 django/utils/text.py
  42. +1 −2 django/utils/translation/__init__.py
  43. +2 −2 django/views/debug.py
  44. +1 −2 django/views/defaults.py
  45. +3 −8 django/views/generic/edit.py
  46. +4 −4 docs/howto/custom-model-fields.txt
  47. +1 −2 docs/topics/serialization.txt
@@ -8,7 +8,6 @@
from django.core.exceptions import PermissionDenied
from django.db import router
from django.template.response import TemplateResponse
from django.utils.encoding import force_text
from django.utils.translation import gettext as _, gettext_lazy
@@ -44,7 +43,7 @@ def delete_selected(modeladmin, request, queryset):
n = queryset.count()
if n:
for obj in queryset:
obj_display = force_text(obj)
obj_display = str(obj)
modeladmin.log_deletion(request, obj, obj_display)
queryset.delete()
modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
@@ -63,7 +62,7 @@ def delete_selected(modeladmin, request, queryset):
context = dict(
modeladmin.admin_site.each_context(request),
title=title,
objects_name=objects_name,
objects_name=str(objects_name),
deletable_objects=[deletable_objects],
model_count=dict(model_count).items(),
queryset=queryset,
@@ -14,7 +14,6 @@
from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.db import models
from django.utils import timezone
from django.utils.encoding import force_text
from django.utils.translation import gettext_lazy as _
@@ -107,7 +106,7 @@ def choices(self, changelist):
}
for lookup, title in self.lookup_choices:
yield {
'selected': self.value() == force_text(lookup),
'selected': self.value() == str(lookup),
'query_string': changelist.get_query_string({self.parameter_name: lookup}, []),
'display': title,
}
@@ -204,7 +203,7 @@ def choices(self, changelist):
}
for pk_val, val in self.lookup_choices:
yield {
'selected': self.lookup_val == force_text(pk_val),
'selected': self.lookup_val == str(pk_val),
'query_string': changelist.get_query_string({
self.lookup_kwarg: pk_val,
}, [self.lookup_kwarg_isnull]),
@@ -290,7 +289,7 @@ def choices(self, changelist):
none_title = title
continue
yield {
'selected': force_text(lookup) == self.lookup_val,
'selected': str(lookup) == self.lookup_val,
'query_string': changelist.get_query_string(
{self.lookup_kwarg: lookup}, [self.lookup_kwarg_isnull]
),
@@ -415,7 +414,7 @@ def choices(self, changelist):
if val is None:
include_none = True
continue
val = force_text(val)
val = str(val)
yield {
'selected': self.lookup_val == val,
'query_string': changelist.get_query_string({
@@ -6,7 +6,6 @@
from django.db import models
from django.urls import NoReverseMatch, reverse
from django.utils import timezone
from django.utils.encoding import force_text
from django.utils.text import get_text_list
from django.utils.translation import gettext, gettext_lazy as _
@@ -24,7 +23,7 @@ def log_action(self, user_id, content_type_id, object_id, object_repr, action_fl
return self.model.objects.create(
user_id=user_id,
content_type_id=content_type_id,
object_id=force_text(object_id),
object_id=str(object_id),
object_repr=object_repr[:200],
action_flag=action_flag,
change_message=change_message,
@@ -64,7 +63,7 @@ class Meta:
ordering = ('-action_time',)
def __repr__(self):
return force_text(self.action_time)
return str(self.action_time)
def __str__(self):
if self.is_addition():
@@ -38,7 +38,6 @@
from django.template.response import SimpleTemplateResponse, TemplateResponse
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.http import urlencode
from django.utils.safestring import mark_safe
@@ -722,7 +721,7 @@ def log_addition(self, request, object, message):
user_id=request.user.pk,
content_type_id=get_content_type_for_model(object).pk,
object_id=object.pk,
object_repr=force_text(object),
object_repr=str(object),
action_flag=ADDITION,
change_message=message,
)
@@ -738,7 +737,7 @@ def log_change(self, request, object, message):
user_id=request.user.pk,
content_type_id=get_content_type_for_model(object).pk,
object_id=object.pk,
object_repr=force_text(object),
object_repr=str(object),
action_flag=CHANGE,
change_message=message,
)
@@ -763,7 +762,7 @@ def action_checkbox(self, obj):
"""
A list_display column containing a checkbox widget.
"""
return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, force_text(obj.pk))
return helpers.checkbox.render(helpers.ACTION_CHECKBOX_NAME, str(obj.pk))
action_checkbox.short_description = mark_safe('<input type="checkbox" id="action-toggle" />')
def get_actions(self, request):
@@ -1056,7 +1055,7 @@ def response_add(self, request, obj, post_url_continue=None):
if self.has_change_permission(request, obj):
obj_repr = format_html('<a href="{}">{}</a>', urlquote(obj_url), obj)
else:
obj_repr = force_text(obj)
obj_repr = str(obj)
msg_dict = {
'name': opts.verbose_name,
'obj': obj_repr,
@@ -1652,7 +1651,7 @@ def changelist_view(self, request, extra_context=None):
context = dict(
self.admin_site.each_context(request),
module_name=force_text(opts.verbose_name_plural),
module_name=str(opts.verbose_name_plural),
selection_note=_('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
selection_note_all=selection_note_all % {'total_count': cl.result_count},
title=cl.title,
@@ -1710,15 +1709,15 @@ def _delete_view(self, request, object_id, extra_context):
if request.POST and not protected: # The user has confirmed the deletion.
if perms_needed:
raise PermissionDenied
obj_display = force_text(obj)
obj_display = str(obj)
attr = str(to_field) if to_field else opts.pk.attname
obj_id = obj.serializable_value(attr)
self.log_deletion(request, obj, obj_display)
self.delete_model(request, obj)
return self.response_delete(request, obj_display, obj_id)
object_name = force_text(opts.verbose_name)
object_name = str(opts.verbose_name)
if perms_needed or protected:
title = _("Cannot delete %(name)s") % {"name": object_name}
@@ -1769,7 +1768,7 @@ def history_view(self, request, object_id, extra_context=None):
self.admin_site.each_context(request),
title=_('Change history: %s') % obj,
action_list=action_list,
module_name=capfirst(force_text(opts.verbose_name_plural)),
module_name=str(capfirst(opts.verbose_name_plural)),
object=obj,
opts=opts,
preserved_filters=self.get_preserved_filters(request),
@@ -14,7 +14,6 @@
from django.templatetags.static import static
from django.urls import NoReverseMatch
from django.utils import formats
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.text import capfirst
@@ -233,7 +232,7 @@ def link_in_col(is_first, field_name, cl):
result_repr = display_for_field(value, f, empty_value_display)
if isinstance(f, (models.DateField, models.TimeField, models.ForeignKey)):
row_classes.append('nowrap')
if force_text(result_repr) == '':
if str(result_repr) == '':
result_repr = mark_safe('&nbsp;')
row_class = mark_safe(' class="%s"' % ' '.join(row_classes))
# If list_display_links not defined, add the link tag to the first field
@@ -277,7 +276,7 @@ def link_in_col(is_first, field_name, cl):
field_name == cl.model._meta.pk.name and
form[cl.model._meta.pk.name].is_hidden)):
bf = form[field_name]
result_repr = mark_safe(force_text(bf.errors) + force_text(bf))
result_repr = mark_safe(str(bf.errors) + str(bf))
yield format_html('<td{}>{}</td>', row_class, result_repr)
if form and not form[cl.model._meta.pk.name].is_hidden:
yield format_html('<td>{}</td>', form[cl.model._meta.pk.name])
@@ -11,7 +11,6 @@
from django.forms.utils import pretty_name
from django.urls import NoReverseMatch, reverse
from django.utils import formats, timezone
from django.utils.encoding import force_text
from django.utils.html import format_html
from django.utils.text import capfirst
from django.utils.translation import ngettext, override as translation_override
@@ -338,7 +337,7 @@ def label_for_field(name, model, model_admin=None, return_attr=False):
label = field.related_model._meta.verbose_name
except FieldDoesNotExist:
if name == "__str__":
label = force_text(model._meta.verbose_name)
label = str(model._meta.verbose_name)
attr = str
else:
if callable(name):
@@ -427,9 +426,9 @@ def display_for_value(value, empty_value_display, boolean=False):
elif isinstance(value, (int, decimal.Decimal, float)):
return formats.number_format(value)
elif isinstance(value, (list, tuple)):
return ', '.join(force_text(v) for v in value)
return ', '.join(str(v) for v in value)
else:
return force_text(value)
return str(value)
class NotRelationField(Exception):
@@ -512,23 +511,23 @@ def construct_change_message(form, formsets, add):
for added_object in formset.new_objects:
change_message.append({
'added': {
'name': force_text(added_object._meta.verbose_name),
'object': force_text(added_object),
'name': str(added_object._meta.verbose_name),
'object': str(added_object),
}
})
for changed_object, changed_fields in formset.changed_objects:
change_message.append({
'changed': {
'name': force_text(changed_object._meta.verbose_name),
'object': force_text(changed_object),
'name': str(changed_object._meta.verbose_name),
'object': str(changed_object),
'fields': changed_fields,
}
})
for deleted_object in formset.deleted_objects:
change_message.append({
'deleted': {
'name': force_text(deleted_object._meta.verbose_name),
'object': force_text(deleted_object),
'name': str(deleted_object._meta.verbose_name),
'object': str(deleted_object),
}
})
return change_message
@@ -7,7 +7,6 @@
from django.db.models.deletion import CASCADE
from django.urls import reverse
from django.urls.exceptions import NoReverseMatch
from django.utils.encoding import force_text
from django.utils.html import smart_urlquote
from django.utils.safestring import mark_safe
from django.utils.text import Truncator
@@ -215,7 +214,7 @@ def value_from_datadict(self, data, files, name):
return value.split(',')
def format_value(self, value):
return ','.join(force_text(v) for v in value) if value else ''
return ','.join(str(v) for v in value) if value else ''
class RelatedFieldWidgetWrapper(forms.Widget):
@@ -2,7 +2,6 @@
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.contrib.auth.views import redirect_to_login
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.utils.encoding import force_text
class AccessMixin:
@@ -25,7 +24,7 @@ def get_login_url(self):
'{0} is missing the login_url attribute. Define {0}.login_url, settings.LOGIN_URL, or override '
'{0}.get_login_url().'.format(self.__class__.__name__)
)
return force_text(login_url)
return str(login_url)
def get_permission_denied_message(self):
"""
@@ -8,7 +8,6 @@
from django.core.exceptions import (
FieldDoesNotExist, ImproperlyConfigured, ValidationError,
)
from django.utils.encoding import force_text
from django.utils.functional import lazy
from django.utils.html import format_html
from django.utils.module_loading import import_string
@@ -145,7 +144,7 @@ def validate(self, password, user=None):
for value_part in value_parts:
if SequenceMatcher(a=password.lower(), b=value_part.lower()).quick_ratio() >= self.max_similarity:
try:
verbose_name = force_text(user._meta.get_field(attribute_name).verbose_name)
verbose_name = str(user._meta.get_field(attribute_name).verbose_name)
except FieldDoesNotExist:
verbose_name = attribute_name
raise ValidationError(
@@ -11,7 +11,6 @@
lazy_related_operation,
)
from django.db.models.query_utils import PathInfo
from django.utils.encoding import force_text
from django.utils.functional import cached_property
@@ -398,7 +397,7 @@ def get_reverse_path_info(self):
def value_to_string(self, obj):
qs = getattr(obj, self.name).all()
return force_text([instance._get_pk_val() for instance in qs])
return str([instance._get_pk_val() for instance in qs])
def contribute_to_class(self, cls, name, **kwargs):
kwargs['private_only'] = True
@@ -2,7 +2,6 @@
from django.apps import apps
from django.db import models
from django.utils.encoding import force_text
from django.utils.translation import gettext_lazy as _
@@ -151,7 +150,7 @@ def name(self):
model = self.model_class()
if not model:
return self.model
return force_text(model._meta.verbose_name)
return str(model._meta.verbose_name)
def model_class(self):
"""Return the model class for this type of content."""
@@ -4,7 +4,6 @@
from django.contrib.postgres.fields.array import ArrayField
from django.core import exceptions
from django.db.models import Field, TextField, Transform
from django.utils.encoding import force_text
from django.utils.translation import gettext_lazy as _
__all__ = ['HStoreField']
@@ -57,14 +56,14 @@ def get_prep_value(self, value):
if isinstance(value, dict):
prep_value = {}
for key, val in value.items():
key = force_text(key)
key = str(key)
if val is not None:
val = force_text(val)
val = str(val)
prep_value[key] = val
value = prep_value
if isinstance(value, list):
value = [force_text(item) for item in value]
value = [str(item) for item in value]
return value
@@ -1,5 +1,4 @@
from django.db.models import Lookup, Transform
from django.utils.encoding import force_text
from .search import SearchVector, SearchVectorExact, SearchVectorField
@@ -38,7 +37,7 @@ class HasKeys(PostgresSimpleLookup):
operator = '?&'
def get_prep_lookup(self):
return [force_text(item) for item in self.rhs]
return [str(item) for item in self.rhs]
class HasAnyKeys(HasKeys):
Oops, something went wrong.

0 comments on commit 301de77

Please sign in to comment.