[damned-lies] Add more Python 3 compatibility wrt unicode



commit f034d3445e2b093b0ec1d0d2ac4a304b06aefb70
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Nov 13 13:52:11 2015 +0100

    Add more Python 3 compatibility wrt unicode

 languages/models.py |    5 ++++-
 people/models.py    |    6 +++++-
 requirements.txt    |    2 +-
 stats/admin.py      |    6 +++---
 stats/doap.py       |    5 ++++-
 stats/models.py     |   25 +++++++++++++++++--------
 stats/utils.py      |    3 ++-
 teams/models.py     |    8 +++++---
 vertimus/admin.py   |    2 +-
 vertimus/models.py  |    8 ++++++--
 10 files changed, 48 insertions(+), 22 deletions(-)
---
diff --git a/languages/models.py b/languages/models.py
index c192df0..bccb165 100644
--- a/languages/models.py
+++ b/languages/models.py
@@ -2,9 +2,12 @@
 from django.core.urlresolvers import NoReverseMatch
 from django.db import models
 from django.db.models import Q
+from django.utils.encoding import python_2_unicode_compatible
 from django.utils.translation import ugettext as _
 from teams.models import Team, FakeTeam
 
+
+ python_2_unicode_compatible
 class Language(models.Model):
     name = models.CharField(max_length=50, unique=True)
     locale = models.CharField(max_length=15, unique=True)
@@ -15,7 +18,7 @@ class Language(models.Model):
         db_table = 'language'
         ordering = ('name',)
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s)" % (self.name, self.locale)
 
     @classmethod
diff --git a/people/models.py b/people/models.py
index 53676eb..bf1d765 100644
--- a/people/models.py
+++ b/people/models.py
@@ -23,11 +23,13 @@ import re
 from django.core.exceptions import ObjectDoesNotExist
 from django.core.urlresolvers import reverse
 from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
 from django.utils.html import escape
 from django.utils.safestring import mark_safe
 from django.utils.translation import ugettext_lazy as _
 from django.contrib.auth.models import User, UserManager
 
+
 def obfuscate_email(email):
     if email:
         # Do not replace the 1st dot in "First M. Name <name dom com>"
@@ -35,6 +37,8 @@ def obfuscate_email(email):
         return mark_safe(escape(email.replace('@', ' at ')).replace(' ', '&nbsp;'))
     return ''
 
+
+ python_2_unicode_compatible
 class Person(User):
     """ The User class of D-L. """
 
@@ -126,7 +130,7 @@ class Person(User):
         else:
             return self.username
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
     def as_author(self):
diff --git a/requirements.txt b/requirements.txt
index a0d6e25..5e60ebf 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
-django>=1.7
+django>=1.8
 django-debug-toolbar
 markdown
 pyicu
diff --git a/stats/admin.py b/stats/admin.py
index 5fecec3..787efac 100644
--- a/stats/admin.py
+++ b/stats/admin.py
@@ -22,7 +22,7 @@ from django.core.exceptions import PermissionDenied
 from django.contrib import admin
 from django.contrib.admin import helpers
 from django.shortcuts import render
-from django.utils.encoding import force_unicode
+from django.utils.encoding import force_text
 from django import forms
 from stats.models import (
     Statistics, Information, PoFile, Module, Branch, Domain, Category,
@@ -113,7 +113,7 @@ class BranchAdmin(admin.ModelAdmin):
     search_fields = ('name', 'module__name')
 
 class DomainAdmin(admin.ModelAdmin):
-    list_display = ('__unicode__', 'directory', 'pot_method')
+    list_display = ('__str__', 'directory', 'pot_method')
     search_fields = ('name', 'module__name','directory', 'pot_method')
 
 class CategoryInline(admin.TabularInline):
@@ -137,7 +137,7 @@ class ReleaseAdmin(admin.ModelAdmin):
         if request.POST.get('post'):
             # Already confirmed
             for obj in queryset:
-                self.log_deletion(request, obj, force_unicode(obj))
+                self.log_deletion(request, obj, force_text(obj))
             n = queryset.count()
             b = 0
             for release in queryset:
diff --git a/stats/doap.py b/stats/doap.py
index 13ac16f..cd24b03 100644
--- a/stats/doap.py
+++ b/stats/doap.py
@@ -4,8 +4,11 @@ import re
 from xml.etree.ElementTree import parse
 from urllib import unquote
 
+from django.utils.encoding import force_text
+
 from people.models import Person
 
+
 class DoapParser(object):
     def __init__(self, doap_path):
         self.tree = parse(doap_path)
@@ -51,7 +54,7 @@ def update_doap_infos(module):
 
     # *********** Update maintainers
     def slugify(val):
-        value = unicode(re.sub('[^\w\s-]', '', val).strip().lower())
+        value = force_text(re.sub('[^\w\s-]', '', val).strip().lower())
         return re.sub('[-\s]+', '-', value)
     doap_maintainers = tree.parse_maintainers()
     current_maintainers = dict([(m.email or m.username, m) for m in module.maintainers.all()])
diff --git a/stats/models.py b/stats/models.py
index 83bdc96..d47f60a 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -35,6 +35,7 @@ from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.core.validators import RegexValidator
+from django.utils.encoding import python_2_unicode_compatible
 from django.utils.functional import cached_property
 from django.utils.translation import ungettext, ugettext as _, ugettext_noop
 from django.utils import dateformat
@@ -70,6 +71,7 @@ BRANCH_HEAD_NAMES = (
     'master'
 )
 
+ python_2_unicode_compatible
 class Module(models.Model):
     name = models.CharField(max_length=50, unique=True, validators=[validate_slug])
     homepage    = models.URLField(null=True, blank=True,
@@ -95,7 +97,7 @@ class Module(models.Model):
         db_table = 'module'
         ordering = ('name',)
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
     def __cmp__(self, other):
@@ -191,6 +193,7 @@ class ModuleLock(object):
         return os.path.exists(self.dirpath)
 
 
+ python_2_unicode_compatible
 @total_ordering
 class Branch(models.Model):
     """ Branch of a module """
@@ -216,7 +219,7 @@ class Branch(models.Model):
         self._ui_stats = None
         self._doc_stats = None
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s)" % (self.name, self.module)
 
     def clean(self):
@@ -744,6 +747,7 @@ DOMAIN_TYPE_CHOICES = (
     ('doc', 'Documentation')
 )
 
+ python_2_unicode_compatible
 class Domain(models.Model):
     module = models.ForeignKey(Module)
     name = models.CharField(max_length=50)
@@ -768,7 +772,7 @@ class Domain(models.Model):
         db_table = 'domain'
         ordering = ('-dtype', 'name')
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s/%s)" % (self.name, self.module.name, self.get_dtype_display())
 
     def potbase(self):
@@ -914,6 +918,7 @@ RELEASE_STATUS_CHOICES = (
     ('unofficial', 'Unofficial'),
     ('xternal', 'External')
 )
+ python_2_unicode_compatible
 class Release(models.Model):
     name = models.SlugField(max_length=20)
     description = models.CharField(max_length=50)
@@ -927,7 +932,7 @@ class Release(models.Model):
         db_table = 'release'
         ordering = ('status', '-name')
 
-    def __unicode__(self):
+    def __str__(self):
         return self.description
 
     def get_description(self):
@@ -1177,16 +1182,18 @@ class Release(models.Model):
         return last_modif_date, lang_files
 
 
+ python_2_unicode_compatible
 class CategoryName(models.Model):
     name = models.CharField(max_length=30, unique=True)
 
     class Meta:
         db_table = 'categoryname'
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name
 
 
+ python_2_unicode_compatible
 class Category(models.Model):
     release = models.ForeignKey(Release)
     branch = models.ForeignKey(Branch)
@@ -1197,10 +1204,11 @@ class Category(models.Model):
         verbose_name_plural = 'categories'
         unique_together = ('release', 'branch')
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s, %s)" % (self.name, self.release, self.branch)
 
 
+ python_2_unicode_compatible
 class PoFile(models.Model):
     # File type fields of Django may not be flexible enough for our use case
     path         = models.CharField(max_length=255, blank=True, null=True)
@@ -1218,7 +1226,7 @@ class PoFile(models.Model):
     class Meta:
         db_table = 'pofile'
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s (%s/%s/%s)" % (self.path, self.translated, self.fuzzy, self.untranslated)
 
     @property
@@ -1293,6 +1301,7 @@ class PoFile(models.Model):
         self.save()
 
 
+ python_2_unicode_compatible
 class Statistics(models.Model):
     branch = models.ForeignKey(Branch)
     domain = models.ForeignKey(Domain)
@@ -1312,7 +1321,7 @@ class Statistics(models.Model):
         self.partial_po = False # True if part of a multiple po module
         self.info_list = []
 
-    def __unicode__(self):
+    def __str__(self):
         """ String representation of the object """
         return "%s (%s-%s) %s (%s)" % (self.branch.module.name, self.domain.dtype, self.domain.name,
                                        self.branch.name, self.get_lang())
diff --git a/stats/utils.py b/stats/utils.py
index 66b4128..2a25093 100644
--- a/stats/utils.py
+++ b/stats/utils.py
@@ -37,6 +37,7 @@ from django.core.files.base import File
 from django.core.mail import send_mail
 from django.template import Context
 from django.template.loader import get_template
+from django.utils import six
 from django.utils.translation import ugettext_noop
 
 import potdiff
@@ -131,7 +132,7 @@ def run_shell_command(cmd, input_data=None, raise_on_error=False, **popen_kwargs
         os.environ.update(popen_kwargs['env'])
         popen_kwargs['env'] = os.environ
     shell = not isinstance(cmd, list)
-    if isinstance(cmd, unicode):
+    if isinstance(cmd, six.text_type):
         cmd = cmd.encode('utf-8')
     elif isinstance(cmd, list):
         cmd = [c.encode('utf-8') for c in cmd]
diff --git a/teams/models.py b/teams/models.py
index 0ff82e6..008f148 100644
--- a/teams/models.py
+++ b/teams/models.py
@@ -24,7 +24,7 @@ from django.db import models
 from django.core import mail
 from django.core.urlresolvers import reverse
 from django.utils import translation
-from django.utils.encoding import force_text
+from django.utils.encoding import force_text, python_2_unicode_compatible
 from django.utils.translation import ugettext_lazy, ugettext as _
 from django.conf import settings
 from django.contrib.sites.models import Site
@@ -81,6 +81,7 @@ class TeamManager(models.Manager):
         return teams
 
 
+ python_2_unicode_compatible
 class Team(models.Model):
     """The lang_code is generally used for the name of the team."""
 
@@ -102,7 +103,7 @@ class Team(models.Model):
         models.Model.__init__(self, *args, **kwargs)
         self.roles = {}
 
-    def __unicode__(self):
+    def __str__(self):
         return self.description
 
     def get_absolute_url(self):
@@ -242,6 +243,7 @@ ROLE_CHOICES = (
     ('coordinator', ugettext_lazy('Coordinator')),
 )
 
+ python_2_unicode_compatible
 class Role(models.Model):
     """
     This is the intermediary class between Person and Team to attribute roles to
@@ -257,7 +259,7 @@ class Role(models.Model):
         db_table = 'role'
         unique_together = ('team', 'person')
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s is %s in %s team" % (self.person.name, self.role,
                                         self.team.description)
 
diff --git a/vertimus/admin.py b/vertimus/admin.py
index 902adc3..9c28a2d 100644
--- a/vertimus/admin.py
+++ b/vertimus/admin.py
@@ -9,7 +9,7 @@ class StateAdmin(admin.ModelAdmin):
     search_fields = ('branch__module__name',)
 
 class ActionAdmin(admin.ModelAdmin):
-    list_display = ('__unicode__', 'state_db', 'merged_file')
+    list_display = ('__str__', 'state_db', 'merged_file')
     raw_id_fields = ('state_db', 'person', 'merged_file')
     search_fields = ('comment',)
 
diff --git a/vertimus/models.py b/vertimus/models.py
index 1a000c7..528f569 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -30,6 +30,7 @@ from django.db import models
 from django.db.models import Max
 from django.db.models.signals import post_save, pre_delete
 from django.dispatch import receiver
+from django.utils.encoding import python_2_unicode_compatible
 from django.utils.translation import override, ugettext, ugettext_noop, ugettext_lazy as _
 
 from stats.models import Branch, Domain, Statistics, PoFile
@@ -49,6 +50,7 @@ class SendMailFailed(Exception):
 # States
 #
 
+ python_2_unicode_compatible
 class State(models.Model):
     """State of a module translation"""
     branch = models.ForeignKey(Branch)
@@ -80,7 +82,7 @@ class State(models.Model):
             'Committed'   : StateCommitted,
         }.get(self.name, State)
 
-    def __unicode__(self):
+    def __str__(self):
         return "%s: %s %s (%s - %s)" % (self.name, self.branch.module.name,
             self.branch.name, self.language.name, self.domain.name)
 
@@ -344,6 +346,8 @@ def generate_upload_filename(instance, filename):
         ext)
     return "%s/%s" % (settings.UPLOAD_DIR, new_filename)
 
+
+ python_2_unicode_compatible
 class ActionAbstract(models.Model):
     """ Common model for Action and ActionArchived """
     state_db = models.ForeignKey(State)
@@ -368,7 +372,7 @@ class ActionAbstract(models.Model):
     class Meta:
         abstract = True
 
-    def __unicode__(self):
+    def __str__(self):
         return u"%s (%s) - %s" % (self.name, self.description, self.id)
 
     @property


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]