Fix str/unicode problems in Python 3

Changes:
- Use unicode_literals from the future for Python 2.6.5+
- Use six's text_type for proper unicode use in Python 2/3
This commit is contained in:
Trey Hunner 2013-04-06 15:55:42 -07:00
parent 81c7e40e60
commit 4f2673e6a4
6 changed files with 21 additions and 12 deletions

View file

@ -1,3 +1,6 @@
from __future__ import unicode_literals
class Choices(object):
"""
A class to encapsulate handy functionality for lists of choices
@ -72,4 +75,4 @@ class Choices(object):
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__,
', '.join(("%s" % str(i) for i in self._full)))
', '.join(("%s" % repr(i) for i in self._full)))

View file

@ -1,7 +1,9 @@
from __future__ import unicode_literals
from datetime import datetime
from django.db import models
from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible
try:
@ -128,6 +130,7 @@ def get_excerpt(content):
return '\n'.join(default_excerpt)
@python_2_unicode_compatible
class SplitText(object):
def __init__(self, instance, field_name, excerpt_field_name):
# instead of storing actual values store a reference to the instance
@ -153,8 +156,7 @@ class SplitText(object):
return self.excerpt.strip() != self.content.strip()
has_more = property(_get_has_more)
# allows display via templates without .content necessary
def __unicode__(self):
def __str__(self):
return self.content
class SplitDescriptor(object):

View file

@ -1,3 +1,4 @@
from __future__ import unicode_literals
import django
from django.db import models
from django.db.models.fields.related import OneToOneField

View file

@ -1,3 +1,4 @@
from __future__ import unicode_literals
from datetime import datetime
from django.db import models

View file

@ -1,4 +1,4 @@
from __future__ import with_statement
from __future__ import unicode_literals, with_statement
import pickle
from datetime import datetime, timedelta
@ -6,6 +6,7 @@ from datetime import datetime, timedelta
import django
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.utils.six import text_type
from django.core.exceptions import ImproperlyConfigured, FieldError
from django.test import TestCase
@ -47,8 +48,8 @@ class GetExcerptTests(TestCase):
class SplitFieldTests(TestCase):
full_text = u'summary\n\n<!-- split -->\n\nmore'
excerpt = u'summary\n'
full_text = 'summary\n\n<!-- split -->\n\nmore'
excerpt = 'summary\n'
def setUp(self):
@ -57,7 +58,7 @@ class SplitFieldTests(TestCase):
def test_unicode_content(self):
self.assertEquals(unicode(self.post.body), self.full_text)
self.assertEquals(text_type(self.post.body), self.full_text)
def test_excerpt(self):
@ -85,17 +86,17 @@ class SplitFieldTests(TestCase):
def test_assign_to_body(self):
new_text = u'different\n\n<!-- split -->\n\nother'
new_text = 'different\n\n<!-- split -->\n\nother'
self.post.body = new_text
self.post.save()
self.assertEquals(unicode(self.post.body), new_text)
self.assertEquals(text_type(self.post.body), new_text)
def test_assign_to_content(self):
new_text = u'different\n\n<!-- split -->\n\nother'
new_text = 'different\n\n<!-- split -->\n\nother'
self.post.body.content = new_text
self.post.save()
self.assertEquals(unicode(self.post.body), new_text)
self.assertEquals(text_type(self.post.body), new_text)
def test_assign_to_excerpt(self):
@ -118,7 +119,7 @@ class SplitFieldTests(TestCase):
def test_assign_splittext(self):
a = Article(title='Some Title')
a.body = self.post.body
self.assertEquals(a.body.excerpt, u'summary\n')
self.assertEquals(a.body.excerpt, 'summary\n')
def test_value_to_string(self):

View file

@ -1,3 +1,4 @@
from __future__ import unicode_literals
from django.db import models
from django.core.exceptions import FieldError