Commit d4a0b278 authored by Aymeric Augustin's avatar Aymeric Augustin
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
parent 79d62a71
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ from django.contrib.auth.models import User
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 @@ class LogEntryManager(models.Manager):
        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 LogEntry(models.Model):
    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:
+9 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ from django.contrib.auth.hashers import (
    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 @@ class PermissionManager(models.Manager):
        )


@python_2_unicode_compatible
class Permission(models.Model):
    """
    The permissions system provides a way to assign permissions to specific
@@ -76,7 +78,7 @@ class Permission(models.Model):
        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 Group(models.Model):
        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 User(models.Model):
        verbose_name = _('user')
        verbose_name_plural = _('users')

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

    def natural_key(self):
@@ -403,6 +407,7 @@ class User(models.Model):
        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):
+5 −2
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from django.core import urlresolvers
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 @@ class BaseCommentAbstractModel(models.Model):
            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 Comment(BaseCommentAbstractModel):
        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 @@ class Comment(BaseCommentAbstractModel):
        }
        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 CommentFlag(models.Model):
        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)

+3 −1
Original line number Diff line number Diff line
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 @@ class ContentTypeManager(models.Manager):
        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 ContentType(models.Model):
        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
+3 −1
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ from django.http import HttpRequest, Http404
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


Loading