Permalink
Browse files

[py3] Refactored __unicode__ to __str__.

* Renamed the __unicode__ methods
* Applied the python_2_unicode_compatible decorator
* Removed the StrAndUnicode mix-in that is superseded by
  python_2_unicode_compatible
* Kept the __unicode__ methods in classes that specifically
  test it under Python 2
  • Loading branch information...
1 parent 79d62a7 commit d4a0b27838c815af87698920cc4db7d2afd6f05b @aaugustin aaugustin committed Aug 12, 2012
Showing with 1,072 additions and 481 deletions.
  1. +3 −1 django/contrib/admin/models.py
  2. +9 −4 django/contrib/auth/models.py
  3. +5 −2 django/contrib/comments/models.py
  4. +3 −1 django/contrib/contenttypes/models.py
  5. +3 −1 django/contrib/contenttypes/tests.py
  6. +3 −1 django/contrib/databrowse/datastructures.py
  7. +7 −3 django/contrib/databrowse/tests.py
  8. +3 −1 django/contrib/flatpages/models.py
  9. +3 −1 django/contrib/gis/db/backends/base.py
  10. +3 −1 django/contrib/gis/db/backends/oracle/models.py
  11. +3 −1 django/contrib/gis/db/backends/postgis/models.py
  12. +3 −1 django/contrib/gis/db/backends/spatialite/models.py
  13. +5 −2 django/contrib/gis/maps/google/overlays.py
  14. +15 −7 django/contrib/gis/tests/distapp/models.py
  15. +17 −9 django/contrib/gis/tests/geo3d/models.py
  16. +3 −1 django/contrib/gis/tests/geoadmin/models.py
  17. +11 −5 django/contrib/gis/tests/geoapp/models.py
  18. +7 −3 django/contrib/gis/tests/geogapp/models.py
  19. +9 −5 django/contrib/gis/tests/relatedapp/models.py
  20. +1 −1 django/contrib/gis/utils/ogrinspect.py
  21. +4 −3 django/contrib/messages/storage/base.py
  22. +4 −2 django/contrib/redirects/models.py
  23. +5 −2 django/contrib/sites/models.py
  24. +5 −2 django/core/files/base.py
  25. +3 −1 django/core/files/uploadhandler.py
  26. +3 −1 django/db/models/options.py
  27. +7 −5 django/forms/forms.py
  28. +4 −3 django/forms/formsets.py
  29. +7 −5 django/forms/util.py
  30. +12 −8 django/forms/widgets.py
  31. +3 −1 django/template/base.py
  32. +5 −2 django/test/html.py
  33. +9 −4 tests/modeltests/aggregation/models.py
  34. +3 −1 tests/modeltests/basic/models.py
  35. +3 −1 tests/modeltests/choices/models.py
  36. +5 −2 tests/modeltests/custom_columns/models.py
  37. +7 −3 tests/modeltests/custom_managers/models.py
  38. +3 −1 tests/modeltests/custom_methods/models.py
  39. +3 −1 tests/modeltests/custom_pk/fields.py
  40. +7 −3 tests/modeltests/custom_pk/models.py
  41. +3 −1 tests/modeltests/defer/models.py
  42. +3 −1 tests/modeltests/delete/models.py
  43. +9 −4 tests/modeltests/distinct_on_fields/models.py
  44. +5 −2 tests/modeltests/expressions/models.py
  45. +3 −1 tests/modeltests/field_defaults/models.py
  46. +3 −1 tests/modeltests/field_subclassing/fields.py
  47. +3 −1 tests/modeltests/field_subclassing/models.py
  48. +15 −7 tests/modeltests/fixtures/models.py
  49. +3 −1 tests/modeltests/fixtures_model_package/models/__init__.py
  50. +11 −5 tests/modeltests/generic_relations/models.py
  51. +5 −2 tests/modeltests/get_latest/models.py
  52. +5 −2 tests/modeltests/get_object_or_404/models.py
  53. +3 −1 tests/modeltests/get_or_create/models.py
  54. +9 −4 tests/modeltests/lookup/models.py
  55. +3 −1 tests/modeltests/m2m_and_m2o/models.py
  56. +7 −3 tests/modeltests/m2m_intermediary/models.py
  57. +5 −2 tests/modeltests/m2m_multiple/models.py
  58. +3 −1 tests/modeltests/m2m_recursive/models.py
  59. +7 −3 tests/modeltests/m2m_signals/models.py
  60. +11 −5 tests/modeltests/m2m_through/models.py
  61. +5 −2 tests/modeltests/m2o_recursive/models.py
  62. +5 −2 tests/modeltests/many_to_many/models.py
  63. +5 −2 tests/modeltests/many_to_one/models.py
  64. +5 −2 tests/modeltests/many_to_one_null/models.py
  65. +31 −15 tests/modeltests/model_forms/models.py
  66. +37 −18 tests/modeltests/model_formsets/models.py
  67. +19 −9 tests/modeltests/model_inheritance/models.py
  68. +3 −1 tests/modeltests/model_inheritance_same_model_name/models.py
  69. +9 −4 tests/modeltests/one_to_one/models.py
  70. +3 −1 tests/modeltests/or_lookups/models.py
  71. +5 −2 tests/modeltests/order_with_respect_to/models.py
  72. +5 −2 tests/modeltests/ordering/models.py
  73. +3 −1 tests/modeltests/pagination/models.py
  74. +15 −7 tests/modeltests/prefetch_related/models.py
  75. +9 −4 tests/modeltests/proxy_models/models.py
  76. +4 −2 tests/modeltests/reserved_names/models.py
  77. +7 −3 tests/modeltests/reverse_lookup/models.py
  78. +3 −1 tests/modeltests/save_delete_hooks/models.py
  79. +18 −9 tests/modeltests/select_related/models.py
  80. +17 −8 tests/modeltests/serializers/models.py
  81. +5 −2 tests/modeltests/signals/models.py
  82. +4 −2 tests/modeltests/str/models.py
  83. +3 −1 tests/modeltests/transactions/models.py
  84. +13 −6 tests/modeltests/unmanaged_models/models.py
  85. +5 −2 tests/modeltests/update/models.py
  86. +5 −2 tests/modeltests/update_only_fields/models.py
  87. +3 −1 tests/modeltests/validation/models.py
  88. +5 −2 tests/regressiontests/admin_changelist/models.py
  89. +3 −1 tests/regressiontests/admin_custom_urls/models.py
  90. +7 −3 tests/regressiontests/admin_filters/models.py
  91. +7 −3 tests/regressiontests/admin_inlines/models.py
  92. +3 −1 tests/regressiontests/admin_scripts/models.py
  93. +3 −1 tests/regressiontests/admin_util/models.py
  94. +3 −1 tests/regressiontests/admin_validation/models.py
  95. +61 −30 tests/regressiontests/admin_views/models.py
  96. +16 −8 tests/regressiontests/admin_widgets/models.py
  97. +11 −5 tests/regressiontests/aggregation_regress/models.py
  98. +9 −4 tests/regressiontests/backends/models.py
  99. +7 −3 tests/regressiontests/comment_tests/models.py
  100. +5 −2 tests/regressiontests/custom_columns_regress/models.py
  101. +7 −3 tests/regressiontests/custom_managers_regress/models.py
  102. +3 −1 tests/regressiontests/datatypes/models.py
  103. +5 −2 tests/regressiontests/dates/models.py
  104. +3 −1 tests/regressiontests/defaultfilters/tests.py
  105. +7 −3 tests/regressiontests/defer_regress/models.py
  106. +3 −1 tests/regressiontests/expressions_regress/models.py
  107. +5 −2 tests/regressiontests/extra_regress/models.py
  108. +17 −8 tests/regressiontests/fixtures_regress/models.py
  109. +5 −2 tests/regressiontests/forms/models.py
  110. +3 −1 tests/regressiontests/forms/tests/error_messages.py
  111. +20 −8 tests/regressiontests/forms/tests/extra.py
  112. +3 −1 tests/regressiontests/forms/tests/util.py
  113. +5 −2 tests/regressiontests/forms/tests/widgets.py
  114. +3 −1 tests/regressiontests/generic_inline_admin/models.py
  115. +11 −5 tests/regressiontests/generic_relations_regress/models.py
  116. +7 −3 tests/regressiontests/generic_views/models.py
  117. +5 −2 tests/regressiontests/inline_formsets/models.py
  118. +5 −2 tests/regressiontests/introspection/models.py
  119. +9 −4 tests/regressiontests/m2m_regress/models.py
  120. +9 −4 tests/regressiontests/m2m_through_regress/models.py
  121. +13 −6 tests/regressiontests/managers_regress/models.py
  122. +5 −2 tests/regressiontests/many_to_one_regress/models.py
  123. +5 −2 tests/regressiontests/model_forms_regress/models.py
  124. +3 −1 tests/regressiontests/model_formsets_regress/models.py
  125. +19 −9 tests/regressiontests/model_inheritance_regress/models.py
  126. +7 −3 tests/regressiontests/model_inheritance_select_related/models.py
  127. +9 −4 tests/regressiontests/model_regress/models.py
  128. +3 −1 tests/regressiontests/modeladmin/models.py
  129. +9 −4 tests/regressiontests/multiple_database/models.py
  130. +5 −2 tests/regressiontests/null_fk/models.py
  131. +7 −3 tests/regressiontests/null_fk_ordering/models.py
  132. +5 −2 tests/regressiontests/null_queries/models.py
  133. +9 −4 tests/regressiontests/one_to_one_regress/models.py
  134. +53 −26 tests/regressiontests/queries/models.py
  135. +11 −5 tests/regressiontests/select_related_onetoone/models.py
  136. +13 −6 tests/regressiontests/select_related_regress/models.py
  137. +5 −2 tests/regressiontests/signals_regress/models.py
  138. +3 −1 tests/regressiontests/sites_framework/models.py
  139. +13 −6 tests/regressiontests/string_lookup/models.py
  140. +5 −2 tests/regressiontests/syndication/models.py
  141. +10 −7 tests/regressiontests/templates/filters.py
  142. +5 −2 tests/regressiontests/views/models.py
@@ -6,6 +6,7 @@
from django.contrib.admin.util import quote
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text
+from django.utils.encoding import python_2_unicode_compatible
ADDITION = 1
CHANGE = 2
@@ -16,6 +17,7 @@ def log_action(self, user_id, content_type_id, object_id, object_repr, action_fl
e = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
e.save()
+@python_2_unicode_compatible
class LogEntry(models.Model):
action_time = models.DateTimeField(_('action time'), auto_now=True)
user = models.ForeignKey(User)
@@ -36,7 +38,7 @@ class Meta:
def __repr__(self):
return smart_text(self.action_time)
- def __unicode__(self):
+ def __str__(self):
if self.action_flag == ADDITION:
return _('Added "%(object)s".') % {'object': self.object_repr}
elif self.action_flag == CHANGE:
@@ -16,6 +16,7 @@
check_password, make_password, is_password_usable, UNUSABLE_PASSWORD)
from django.contrib.auth.signals import user_logged_in
from django.contrib.contenttypes.models import ContentType
+from django.utils.encoding import python_2_unicode_compatible
def update_last_login(sender, user, **kwargs):
@@ -41,6 +42,7 @@ def get_by_natural_key(self, codename, app_label, model):
)
+@python_2_unicode_compatible
class Permission(models.Model):
"""
The permissions system provides a way to assign permissions to specific
@@ -76,7 +78,7 @@ class Meta:
ordering = ('content_type__app_label', 'content_type__model',
'codename')
- def __unicode__(self):
+ def __str__(self):
return "%s | %s | %s" % (
six.text_type(self.content_type.app_label),
six.text_type(self.content_type),
@@ -94,6 +96,7 @@ class GroupManager(models.Manager):
def get_by_natural_key(self, name):
return self.get(name=name)
+@python_2_unicode_compatible
class Group(models.Model):
"""
Groups are a generic way of categorizing users to apply permissions, or
@@ -121,7 +124,7 @@ class Meta:
verbose_name = _('group')
verbose_name_plural = _('groups')
- def __unicode__(self):
+ def __str__(self):
return self.name
def natural_key(self):
@@ -221,6 +224,7 @@ def _user_has_module_perms(user, app_label):
return False
+@python_2_unicode_compatible
class User(models.Model):
"""
Users within the Django authentication system are represented by this
@@ -259,7 +263,7 @@ class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
- def __unicode__(self):
+ def __str__(self):
return self.username
def natural_key(self):
@@ -403,6 +407,7 @@ def get_profile(self):
return self._profile_cache
+@python_2_unicode_compatible
class AnonymousUser(object):
id = None
pk = None
@@ -416,7 +421,7 @@ class AnonymousUser(object):
def __init__(self):
pass
- def __unicode__(self):
+ def __str__(self):
return 'AnonymousUser'
def __eq__(self, other):
@@ -8,6 +8,7 @@
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.conf import settings
+from django.utils.encoding import python_2_unicode_compatible
COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH',3000)
@@ -39,6 +40,7 @@ def get_content_object_url(self):
args=(self.content_type_id, self.object_pk)
)
+@python_2_unicode_compatible
class Comment(BaseCommentAbstractModel):
"""
A user comment about some object.
@@ -76,7 +78,7 @@ class Meta:
verbose_name = _('comment')
verbose_name_plural = _('comments')
- def __unicode__(self):
+ def __str__(self):
return "%s: %s..." % (self.name, self.comment[:50])
def save(self, *args, **kwargs):
@@ -153,6 +155,7 @@ def get_as_text(self):
}
return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d
+@python_2_unicode_compatible
class CommentFlag(models.Model):
"""
Records a flag on a comment. This is intentionally flexible; right now, a
@@ -182,7 +185,7 @@ class Meta:
verbose_name = _('comment flag')
verbose_name_plural = _('comment flags')
- def __unicode__(self):
+ def __str__(self):
return "%s flag of comment ID %s by %s" % \
(self.flag, self.comment_id, self.user.username)
@@ -1,6 +1,7 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text, force_text
+from django.utils.encoding import python_2_unicode_compatible
class ContentTypeManager(models.Manager):
@@ -122,6 +123,7 @@ def _add_to_cache(self, using, ct):
self.__class__._cache.setdefault(using, {})[key] = ct
self.__class__._cache.setdefault(using, {})[ct.id] = ct
+@python_2_unicode_compatible
class ContentType(models.Model):
name = models.CharField(max_length=100)
app_label = models.CharField(max_length=100)
@@ -135,7 +137,7 @@ class Meta:
ordering = ('name',)
unique_together = (('app_label', 'model'),)
- def __unicode__(self):
+ def __str__(self):
# self.name is deprecated in favor of using model's verbose_name, which
# can be translated. Formal deprecation is delayed until we have DB
# migration to be able to remove the field from the database along with
@@ -8,6 +8,7 @@
from django.test import TestCase
from django.utils.http import urlquote
from django.utils import six
+from django.utils.encoding import python_2_unicode_compatible
class ConcreteModel(models.Model):
@@ -17,13 +18,14 @@ class ProxyModel(ConcreteModel):
class Meta:
proxy = True
+@python_2_unicode_compatible
class FooWithoutUrl(models.Model):
"""
Fake model not defining ``get_absolute_url`` for
:meth:`ContentTypesTests.test_shortcut_view_without_get_absolute_url`"""
name = models.CharField(max_length=30, unique=True)
- def __unicode__(self):
+ def __str__(self):
return self.name
@@ -9,6 +9,7 @@
from django.utils.text import capfirst
from django.utils.encoding import smart_text, smart_bytes, iri_to_uri
from django.db.models.query import QuerySet
+from django.utils.encoding import python_2_unicode_compatible
EMPTY_VALUE = '(None)'
DISPLAY_SIZE = 100
@@ -84,14 +85,15 @@ def __repr__(self):
def url(self):
return '%s%s/%s/%s/%s/' % (self.model.site.root_url, self.model.model._meta.app_label, self.model.model._meta.module_name, self.field.field.name, iri_to_uri(self.value))
+@python_2_unicode_compatible
class EasyInstance(object):
def __init__(self, easy_model, instance):
self.model, self.instance = easy_model, instance
def __repr__(self):
return smart_bytes('<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val()))
- def __unicode__(self):
+ def __str__(self):
val = smart_text(self.instance)
if len(val) > DISPLAY_SIZE:
return val[:DISPLAY_SIZE] + '...'
@@ -1,26 +1,30 @@
from django.contrib import databrowse
from django.db import models
from django.test import TestCase
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class SomeModel(models.Model):
some_field = models.CharField(max_length=50)
- def __unicode__(self):
+ def __str__(self):
return self.some_field
+@python_2_unicode_compatible
class SomeOtherModel(models.Model):
some_other_field = models.CharField(max_length=50)
- def __unicode__(self):
+ def __str__(self):
return self.some_other_field
+@python_2_unicode_compatible
class YetAnotherModel(models.Model):
yet_another_field = models.CharField(max_length=50)
- def __unicode__(self):
+ def __str__(self):
return self.yet_another_field
@@ -3,8 +3,10 @@
from django.db import models
from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class FlatPage(models.Model):
url = models.CharField(_('URL'), max_length=100, db_index=True)
title = models.CharField(_('title'), max_length=200)
@@ -21,7 +23,7 @@ class Meta:
verbose_name_plural = _('flat pages')
ordering = ('url',)
- def __unicode__(self):
+ def __str__(self):
return "%s -- %s" % (self.url, self.title)
def get_absolute_url(self):
@@ -5,6 +5,7 @@
import re
from django.contrib.gis import gdal
from django.utils import six
+from django.utils.encoding import python_2_unicode_compatible
class BaseSpatialOperations(object):
"""
@@ -131,6 +132,7 @@ def geometry_columns(self):
def spatial_ref_sys(self):
raise NotImplementedError
+@python_2_unicode_compatible
class SpatialRefSysMixin(object):
"""
The SpatialRefSysMixin is a class used by the database-dependent
@@ -325,7 +327,7 @@ def get_spheroid(cls, wkt, string=True):
radius, flattening = sphere_params
return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)
- def __unicode__(self):
+ def __str__(self):
"""
Returns the string representation. If GDAL is installed,
it will be 'pretty' OGC WKT.
@@ -9,7 +9,9 @@
"""
from django.contrib.gis.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class GeometryColumns(models.Model):
"Maps to the Oracle USER_SDO_GEOM_METADATA table."
table_name = models.CharField(max_length=32)
@@ -36,7 +38,7 @@ def geom_col_name(cls):
"""
return 'column_name'
- def __unicode__(self):
+ def __str__(self):
return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid)
class SpatialRefSys(models.Model, SpatialRefSysMixin):
@@ -3,7 +3,9 @@
"""
from django.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class GeometryColumns(models.Model):
"""
The 'geometry_columns' table from the PostGIS. See the PostGIS
@@ -37,7 +39,7 @@ def geom_col_name(cls):
"""
return 'f_geometry_column'
- def __unicode__(self):
+ def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid)
@@ -3,7 +3,9 @@
"""
from django.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class GeometryColumns(models.Model):
"""
The 'geometry_columns' table from SpatiaLite.
@@ -35,7 +37,7 @@ def geom_col_name(cls):
"""
return 'f_geometry_column'
- def __unicode__(self):
+ def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid)
@@ -2,8 +2,10 @@
from django.utils.functional import total_ordering
from django.utils.safestring import mark_safe
from django.utils import six
+from django.utils.encoding import python_2_unicode_compatible
+@python_2_unicode_compatible
class GEvent(object):
"""
A Python wrapper for the Google GEvent object.
@@ -48,10 +50,11 @@ def __init__(self, event, action):
self.event = event
self.action = action
- def __unicode__(self):
+ def __str__(self):
"Returns the parameter part of a GEvent."
return mark_safe('"%s", %s' %(self.event, self.action))
+@python_2_unicode_compatible
class GOverlayBase(object):
def __init__(self):
self.events = []
@@ -64,7 +67,7 @@ def add_event(self, event):
"Attaches a GEvent to the overlay object."
self.events.append(event)
- def __unicode__(self):
+ def __str__(self):
"The string representation is the JavaScript API call."
return mark_safe('%s(%s)' % (self.__class__.__name__, self.js_params))
Oops, something went wrong.

0 comments on commit d4a0b27

Please sign in to comment.