Skip to content

Commit

Permalink
[py3] Refactored __unicode__ to __str__.
Browse files Browse the repository at this point in the history
* 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
aaugustin committed Aug 12, 2012
1 parent 79d62a7 commit d4a0b27
Show file tree
Hide file tree
Showing 142 changed files with 1,072 additions and 481 deletions.
4 changes: 3 additions & 1 deletion django/contrib/admin/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.contrib.admin.util import quote from django.contrib.admin.util import quote
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
from django.utils.encoding import python_2_unicode_compatible


ADDITION = 1 ADDITION = 1
CHANGE = 2 CHANGE = 2
Expand All @@ -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 = self.model(None, None, user_id, content_type_id, smart_text(object_id), object_repr[:200], action_flag, change_message)
e.save() e.save()


@python_2_unicode_compatible
class LogEntry(models.Model): class LogEntry(models.Model):
action_time = models.DateTimeField(_('action time'), auto_now=True) action_time = models.DateTimeField(_('action time'), auto_now=True)
user = models.ForeignKey(User) user = models.ForeignKey(User)
Expand All @@ -36,7 +38,7 @@ class Meta:
def __repr__(self): def __repr__(self):
return smart_text(self.action_time) return smart_text(self.action_time)


def __unicode__(self): def __str__(self):
if self.action_flag == ADDITION: if self.action_flag == ADDITION:
return _('Added "%(object)s".') % {'object': self.object_repr} return _('Added "%(object)s".') % {'object': self.object_repr}
elif self.action_flag == CHANGE: elif self.action_flag == CHANGE:
Expand Down
13 changes: 9 additions & 4 deletions django/contrib/auth/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
check_password, make_password, is_password_usable, UNUSABLE_PASSWORD) check_password, make_password, is_password_usable, UNUSABLE_PASSWORD)
from django.contrib.auth.signals import user_logged_in from django.contrib.auth.signals import user_logged_in
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import python_2_unicode_compatible




def update_last_login(sender, user, **kwargs): def update_last_login(sender, user, **kwargs):
Expand All @@ -41,6 +42,7 @@ def get_by_natural_key(self, codename, app_label, model):
) )




@python_2_unicode_compatible
class Permission(models.Model): class Permission(models.Model):
""" """
The permissions system provides a way to assign permissions to specific The permissions system provides a way to assign permissions to specific
Expand Down Expand Up @@ -76,7 +78,7 @@ class Meta:
ordering = ('content_type__app_label', 'content_type__model', ordering = ('content_type__app_label', 'content_type__model',
'codename') 'codename')


def __unicode__(self): def __str__(self):
return "%s | %s | %s" % ( return "%s | %s | %s" % (
six.text_type(self.content_type.app_label), six.text_type(self.content_type.app_label),
six.text_type(self.content_type), six.text_type(self.content_type),
Expand All @@ -94,6 +96,7 @@ class GroupManager(models.Manager):
def get_by_natural_key(self, name): def get_by_natural_key(self, name):
return self.get(name=name) return self.get(name=name)


@python_2_unicode_compatible
class Group(models.Model): class Group(models.Model):
""" """
Groups are a generic way of categorizing users to apply permissions, or Groups are a generic way of categorizing users to apply permissions, or
Expand Down Expand Up @@ -121,7 +124,7 @@ class Meta:
verbose_name = _('group') verbose_name = _('group')
verbose_name_plural = _('groups') verbose_name_plural = _('groups')


def __unicode__(self): def __str__(self):
return self.name return self.name


def natural_key(self): def natural_key(self):
Expand Down Expand Up @@ -221,6 +224,7 @@ def _user_has_module_perms(user, app_label):
return False return False




@python_2_unicode_compatible
class User(models.Model): class User(models.Model):
""" """
Users within the Django authentication system are represented by this Users within the Django authentication system are represented by this
Expand Down Expand Up @@ -259,7 +263,7 @@ class Meta:
verbose_name = _('user') verbose_name = _('user')
verbose_name_plural = _('users') verbose_name_plural = _('users')


def __unicode__(self): def __str__(self):
return self.username return self.username


def natural_key(self): def natural_key(self):
Expand Down Expand Up @@ -403,6 +407,7 @@ def get_profile(self):
return self._profile_cache return self._profile_cache




@python_2_unicode_compatible
class AnonymousUser(object): class AnonymousUser(object):
id = None id = None
pk = None pk = None
Expand All @@ -416,7 +421,7 @@ class AnonymousUser(object):
def __init__(self): def __init__(self):
pass pass


def __unicode__(self): def __str__(self):
return 'AnonymousUser' return 'AnonymousUser'


def __eq__(self, other): def __eq__(self, other):
Expand Down
7 changes: 5 additions & 2 deletions django/contrib/comments/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible


COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH',3000) COMMENT_MAX_LENGTH = getattr(settings,'COMMENT_MAX_LENGTH',3000)


Expand Down Expand Up @@ -39,6 +40,7 @@ def get_content_object_url(self):
args=(self.content_type_id, self.object_pk) args=(self.content_type_id, self.object_pk)
) )


@python_2_unicode_compatible
class Comment(BaseCommentAbstractModel): class Comment(BaseCommentAbstractModel):
""" """
A user comment about some object. A user comment about some object.
Expand Down Expand Up @@ -76,7 +78,7 @@ class Meta:
verbose_name = _('comment') verbose_name = _('comment')
verbose_name_plural = _('comments') verbose_name_plural = _('comments')


def __unicode__(self): def __str__(self):
return "%s: %s..." % (self.name, self.comment[:50]) return "%s: %s..." % (self.name, self.comment[:50])


def save(self, *args, **kwargs): def save(self, *args, **kwargs):
Expand Down Expand Up @@ -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 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): class CommentFlag(models.Model):
""" """
Records a flag on a comment. This is intentionally flexible; right now, a Records a flag on a comment. This is intentionally flexible; right now, a
Expand Down Expand Up @@ -182,7 +185,7 @@ class Meta:
verbose_name = _('comment flag') verbose_name = _('comment flag')
verbose_name_plural = _('comment flags') verbose_name_plural = _('comment flags')


def __unicode__(self): def __str__(self):
return "%s flag of comment ID %s by %s" % \ return "%s flag of comment ID %s by %s" % \
(self.flag, self.comment_id, self.user.username) (self.flag, self.comment_id, self.user.username)


Expand Down
4 changes: 3 additions & 1 deletion django/contrib/contenttypes/models.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text, force_text from django.utils.encoding import smart_text, force_text
from django.utils.encoding import python_2_unicode_compatible


class ContentTypeManager(models.Manager): class ContentTypeManager(models.Manager):


Expand Down Expand Up @@ -122,6 +123,7 @@ def _add_to_cache(self, using, ct):
self.__class__._cache.setdefault(using, {})[key] = ct self.__class__._cache.setdefault(using, {})[key] = ct
self.__class__._cache.setdefault(using, {})[ct.id] = ct self.__class__._cache.setdefault(using, {})[ct.id] = ct


@python_2_unicode_compatible
class ContentType(models.Model): class ContentType(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
app_label = models.CharField(max_length=100) app_label = models.CharField(max_length=100)
Expand All @@ -135,7 +137,7 @@ class Meta:
ordering = ('name',) ordering = ('name',)
unique_together = (('app_label', 'model'),) unique_together = (('app_label', 'model'),)


def __unicode__(self): def __str__(self):
# self.name is deprecated in favor of using model's verbose_name, which # self.name is deprecated in favor of using model's verbose_name, which
# can be translated. Formal deprecation is delayed until we have DB # can be translated. Formal deprecation is delayed until we have DB
# migration to be able to remove the field from the database along with # migration to be able to remove the field from the database along with
Expand Down
4 changes: 3 additions & 1 deletion django/contrib/contenttypes/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.test import TestCase from django.test import TestCase
from django.utils.http import urlquote from django.utils.http import urlquote
from django.utils import six from django.utils import six
from django.utils.encoding import python_2_unicode_compatible




class ConcreteModel(models.Model): class ConcreteModel(models.Model):
Expand All @@ -17,13 +18,14 @@ class ProxyModel(ConcreteModel):
class Meta: class Meta:
proxy = True proxy = True


@python_2_unicode_compatible
class FooWithoutUrl(models.Model): class FooWithoutUrl(models.Model):
""" """
Fake model not defining ``get_absolute_url`` for Fake model not defining ``get_absolute_url`` for
:meth:`ContentTypesTests.test_shortcut_view_without_get_absolute_url`""" :meth:`ContentTypesTests.test_shortcut_view_without_get_absolute_url`"""
name = models.CharField(max_length=30, unique=True) name = models.CharField(max_length=30, unique=True)


def __unicode__(self): def __str__(self):
return self.name return self.name




Expand Down
4 changes: 3 additions & 1 deletion django/contrib/databrowse/datastructures.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.utils.text import capfirst from django.utils.text import capfirst
from django.utils.encoding import smart_text, smart_bytes, iri_to_uri from django.utils.encoding import smart_text, smart_bytes, iri_to_uri
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.utils.encoding import python_2_unicode_compatible


EMPTY_VALUE = '(None)' EMPTY_VALUE = '(None)'
DISPLAY_SIZE = 100 DISPLAY_SIZE = 100
Expand Down Expand Up @@ -84,14 +85,15 @@ def __repr__(self):
def url(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)) 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): class EasyInstance(object):
def __init__(self, easy_model, instance): def __init__(self, easy_model, instance):
self.model, self.instance = easy_model, instance self.model, self.instance = easy_model, instance


def __repr__(self): def __repr__(self):
return smart_bytes('<EasyInstance for %s (%s)>' % (self.model.model._meta.object_name, self.instance._get_pk_val())) 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) val = smart_text(self.instance)
if len(val) > DISPLAY_SIZE: if len(val) > DISPLAY_SIZE:
return val[:DISPLAY_SIZE] + '...' return val[:DISPLAY_SIZE] + '...'
Expand Down
10 changes: 7 additions & 3 deletions django/contrib/databrowse/tests.py
Original file line number Original file line Diff line number Diff line change
@@ -1,26 +1,30 @@
from django.contrib import databrowse from django.contrib import databrowse
from django.db import models from django.db import models
from django.test import TestCase from django.test import TestCase
from django.utils.encoding import python_2_unicode_compatible




@python_2_unicode_compatible
class SomeModel(models.Model): class SomeModel(models.Model):
some_field = models.CharField(max_length=50) some_field = models.CharField(max_length=50)


def __unicode__(self): def __str__(self):
return self.some_field return self.some_field




@python_2_unicode_compatible
class SomeOtherModel(models.Model): class SomeOtherModel(models.Model):
some_other_field = models.CharField(max_length=50) some_other_field = models.CharField(max_length=50)


def __unicode__(self): def __str__(self):
return self.some_other_field return self.some_other_field




@python_2_unicode_compatible
class YetAnotherModel(models.Model): class YetAnotherModel(models.Model):
yet_another_field = models.CharField(max_length=50) yet_another_field = models.CharField(max_length=50)


def __unicode__(self): def __str__(self):
return self.yet_another_field return self.yet_another_field




Expand Down
4 changes: 3 additions & 1 deletion django/contrib/flatpages/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from django.db import models from django.db import models
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.utils.translation import ugettext_lazy as _ 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): class FlatPage(models.Model):
url = models.CharField(_('URL'), max_length=100, db_index=True) url = models.CharField(_('URL'), max_length=100, db_index=True)
title = models.CharField(_('title'), max_length=200) title = models.CharField(_('title'), max_length=200)
Expand All @@ -21,7 +23,7 @@ class Meta:
verbose_name_plural = _('flat pages') verbose_name_plural = _('flat pages')
ordering = ('url',) ordering = ('url',)


def __unicode__(self): def __str__(self):
return "%s -- %s" % (self.url, self.title) return "%s -- %s" % (self.url, self.title)


def get_absolute_url(self): def get_absolute_url(self):
Expand Down
4 changes: 3 additions & 1 deletion django/contrib/gis/db/backends/base.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re import re
from django.contrib.gis import gdal from django.contrib.gis import gdal
from django.utils import six from django.utils import six
from django.utils.encoding import python_2_unicode_compatible


class BaseSpatialOperations(object): class BaseSpatialOperations(object):
""" """
Expand Down Expand Up @@ -131,6 +132,7 @@ def geometry_columns(self):
def spatial_ref_sys(self): def spatial_ref_sys(self):
raise NotImplementedError raise NotImplementedError


@python_2_unicode_compatible
class SpatialRefSysMixin(object): class SpatialRefSysMixin(object):
""" """
The SpatialRefSysMixin is a class used by the database-dependent The SpatialRefSysMixin is a class used by the database-dependent
Expand Down Expand Up @@ -325,7 +327,7 @@ def get_spheroid(cls, wkt, string=True):
radius, flattening = sphere_params radius, flattening = sphere_params
return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening) return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)


def __unicode__(self): def __str__(self):
""" """
Returns the string representation. If GDAL is installed, Returns the string representation. If GDAL is installed,
it will be 'pretty' OGC WKT. it will be 'pretty' OGC WKT.
Expand Down
4 changes: 3 additions & 1 deletion django/contrib/gis/db/backends/oracle/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
""" """
from django.contrib.gis.db import models from django.contrib.gis.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin 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): class GeometryColumns(models.Model):
"Maps to the Oracle USER_SDO_GEOM_METADATA table." "Maps to the Oracle USER_SDO_GEOM_METADATA table."
table_name = models.CharField(max_length=32) table_name = models.CharField(max_length=32)
Expand All @@ -36,7 +38,7 @@ def geom_col_name(cls):
""" """
return 'column_name' return 'column_name'


def __unicode__(self): def __str__(self):
return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid) return '%s - %s (SRID: %s)' % (self.table_name, self.column_name, self.srid)


class SpatialRefSys(models.Model, SpatialRefSysMixin): class SpatialRefSys(models.Model, SpatialRefSysMixin):
Expand Down
4 changes: 3 additions & 1 deletion django/contrib/gis/db/backends/postgis/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
""" """
from django.db import models from django.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin 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): class GeometryColumns(models.Model):
""" """
The 'geometry_columns' table from the PostGIS. See the PostGIS The 'geometry_columns' table from the PostGIS. See the PostGIS
Expand Down Expand Up @@ -37,7 +39,7 @@ def geom_col_name(cls):
""" """
return 'f_geometry_column' return 'f_geometry_column'


def __unicode__(self): def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \ return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column, (self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid) self.coord_dimension, self.type, self.srid)
Expand Down
4 changes: 3 additions & 1 deletion django/contrib/gis/db/backends/spatialite/models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
""" """
from django.db import models from django.db import models
from django.contrib.gis.db.backends.base import SpatialRefSysMixin 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): class GeometryColumns(models.Model):
""" """
The 'geometry_columns' table from SpatiaLite. The 'geometry_columns' table from SpatiaLite.
Expand Down Expand Up @@ -35,7 +37,7 @@ def geom_col_name(cls):
""" """
return 'f_geometry_column' return 'f_geometry_column'


def __unicode__(self): def __str__(self):
return "%s.%s - %dD %s field (SRID: %d)" % \ return "%s.%s - %dD %s field (SRID: %d)" % \
(self.f_table_name, self.f_geometry_column, (self.f_table_name, self.f_geometry_column,
self.coord_dimension, self.type, self.srid) self.coord_dimension, self.type, self.srid)
Expand Down
7 changes: 5 additions & 2 deletions django/contrib/gis/maps/google/overlays.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from django.utils.functional import total_ordering from django.utils.functional import total_ordering
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils import six from django.utils import six
from django.utils.encoding import python_2_unicode_compatible




@python_2_unicode_compatible
class GEvent(object): class GEvent(object):
""" """
A Python wrapper for the Google GEvent object. A Python wrapper for the Google GEvent object.
Expand Down Expand Up @@ -48,10 +50,11 @@ def __init__(self, event, action):
self.event = event self.event = event
self.action = action self.action = action


def __unicode__(self): def __str__(self):
"Returns the parameter part of a GEvent." "Returns the parameter part of a GEvent."
return mark_safe('"%s", %s' %(self.event, self.action)) return mark_safe('"%s", %s' %(self.event, self.action))


@python_2_unicode_compatible
class GOverlayBase(object): class GOverlayBase(object):
def __init__(self): def __init__(self):
self.events = [] self.events = []
Expand All @@ -64,7 +67,7 @@ def add_event(self, event):
"Attaches a GEvent to the overlay object." "Attaches a GEvent to the overlay object."
self.events.append(event) self.events.append(event)


def __unicode__(self): def __str__(self):
"The string representation is the JavaScript API call." "The string representation is the JavaScript API call."
return mark_safe('%s(%s)' % (self.__class__.__name__, self.js_params)) return mark_safe('%s(%s)' % (self.__class__.__name__, self.js_params))


Expand Down
Loading

0 comments on commit d4a0b27

Please sign in to comment.