Skip to content

Commit 0ea0021

Browse files
committed
Removed custom python2_unicode_compatible. Closes #2183 (thanks @sh4r3m4n)
1 parent f8fdfe5 commit 0ea0021

File tree

5 files changed

+19
-45
lines changed

5 files changed

+19
-45
lines changed

rest_framework/authtoken/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
# Note that we don't perform this code in the compat module due to
99
# bug report #1297
1010
# See: https://github.com/tomchristie/django-rest-framework/issues/1297
11+
from django.utils.encoding import python_2_unicode_compatible
12+
1113
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
1214

1315

16+
@python_2_unicode_compatible
1417
class Token(models.Model):
1518
"""
1619
The default authorization token model.
@@ -35,5 +38,5 @@ def save(self, *args, **kwargs):
3538
def generate_key(self):
3639
return binascii.hexlify(os.urandom(20)).decode()
3740

38-
def __unicode__(self):
41+
def __str__(self):
3942
return self.key

rest_framework/compat.py

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
except ImportError:
5050
django_filters = None
5151

52-
5352
if django.VERSION >= (1, 6):
5453
def clean_manytomany_helptext(text):
5554
return text
@@ -104,14 +103,6 @@ def get_model_name(model_cls):
104103
return model_cls._meta.module_name
105104

106105

107-
def get_concrete_model(model_cls):
108-
try:
109-
return model_cls._meta.concrete_model
110-
except AttributeError:
111-
# 1.3 does not include concrete model
112-
return model_cls
113-
114-
115106
# View._allowed_methods only present from 1.5 onwards
116107
if django.VERSION >= (1, 5):
117108
from django.views.generic import View
@@ -123,7 +114,6 @@ def _allowed_methods(self):
123114
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
124115

125116

126-
127117
# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+
128118
if django.VERSION >= (1, 8):
129119
from django.core.validators import MinValueValidator, MaxValueValidator
@@ -187,28 +177,30 @@ def __init__(self, *args, **kwargs):
187177
# RequestFactory only provides `generic` from 1.5 onwards
188178
from django.test.client import RequestFactory as DjangoRequestFactory
189179
from django.test.client import FakePayload
180+
190181
try:
191182
# In 1.5 the test client uses force_bytes
192183
from django.utils.encoding import force_bytes as force_bytes_or_smart_bytes
193184
except ImportError:
194185
# In 1.4 the test client just uses smart_str
195186
from django.utils.encoding import smart_str as force_bytes_or_smart_bytes
196187

188+
197189
class RequestFactory(DjangoRequestFactory):
198190
def generic(self, method, path,
199191
data='', content_type='application/octet-stream', **extra):
200192
parsed = urlparse.urlparse(path)
201193
data = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET)
202194
r = {
203-
'PATH_INFO': self._get_path(parsed),
204-
'QUERY_STRING': force_text(parsed[4]),
195+
'PATH_INFO': self._get_path(parsed),
196+
'QUERY_STRING': force_text(parsed[4]),
205197
'REQUEST_METHOD': six.text_type(method),
206198
}
207199
if data:
208200
r.update({
209201
'CONTENT_LENGTH': len(data),
210-
'CONTENT_TYPE': six.text_type(content_type),
211-
'wsgi.input': FakePayload(data),
202+
'CONTENT_TYPE': six.text_type(content_type),
203+
'wsgi.input': FakePayload(data),
212204
})
213205
elif django.VERSION <= (1, 4):
214206
# For 1.3 we need an empty WSGI payload
@@ -287,10 +279,12 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
287279
import provider as oauth2_provider
288280
from provider import scope as oauth2_provider_scope
289281
from provider import constants as oauth2_constants
282+
290283
if oauth2_provider.__version__ in ('0.2.3', '0.2.4'):
291284
# 0.2.3 and 0.2.4 are supported version that do not support
292285
# timezone aware datetimes
293286
import datetime
287+
294288
provider_now = datetime.datetime.now
295289
else:
296290
# Any other supported version does use timezone aware datetimes
@@ -301,7 +295,7 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
301295
oauth2_constants = None
302296
provider_now = None
303297

304-
# `seperators` argument to `json.dumps()` differs between 2.x and 3.x
298+
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
305299
# See: http://bugs.python.org/issue22767
306300
if six.PY3:
307301
SHORT_SEPARATORS = (',', ':')
@@ -316,30 +310,9 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
316310

317311
if six.PY3:
318312
def is_non_str_iterable(obj):
319-
if (isinstance(obj, str) or
320-
(isinstance(obj, Promise) and obj._delegate_text)):
313+
if isinstance(obj, str) or (isinstance(obj, Promise) and obj._delegate_text):
321314
return False
322315
return hasattr(obj, '__iter__')
323316
else:
324317
def is_non_str_iterable(obj):
325318
return hasattr(obj, '__iter__')
326-
327-
328-
try:
329-
from django.utils.encoding import python_2_unicode_compatible
330-
except ImportError:
331-
def python_2_unicode_compatible(klass):
332-
"""
333-
A decorator that defines __unicode__ and __str__ methods under Python 2.
334-
Under Python 3 it does nothing.
335-
336-
To support Python 2 and 3 with a single code base, define a __str__ method
337-
returning text and apply this decorator to the class.
338-
"""
339-
if '__str__' not in klass.__dict__:
340-
raise ValueError("@python_2_unicode_compatible cannot be applied "
341-
"to %s because it doesn't define __str__()." %
342-
klass.__name__)
343-
klass.__unicode__ = klass.__str__
344-
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
345-
return klass

rest_framework/utils/mediatypes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from __future__ import unicode_literals
77
from django.http.multipartparser import parse_header
8+
from django.utils.encoding import python_2_unicode_compatible
89
from rest_framework import HTTP_HEADER_ENCODING
910

1011

@@ -43,6 +44,7 @@ def order_by_precedence(media_type_lst):
4344
return [media_types for media_types in ret if media_types]
4445

4546

47+
@python_2_unicode_compatible
4648
class _MediaType(object):
4749
def __init__(self, media_type_str):
4850
if media_type_str is None:
@@ -79,9 +81,6 @@ def precedence(self):
7981
return 3
8082

8183
def __str__(self):
82-
return self.__unicode__().encode('utf-8')
83-
84-
def __unicode__(self):
8584
ret = "%s/%s" % (self.main_type, self.sub_type)
8685
for key, val in self.params.items():
8786
ret += "; %s=%s" % (key, val)

tests/test_description.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import unicode_literals
44
from django.test import TestCase
5+
from django.utils.encoding import python_2_unicode_compatible
56
from rest_framework.compat import apply_markdown, smart_text
67
from rest_framework.views import APIView
78
from .description import ViewWithNonASCIICharactersInDocstring
@@ -107,16 +108,14 @@ class that can be converted to a string.
107108
"""
108109
# use a mock object instead of gettext_lazy to ensure that we can't end
109110
# up with a test case string in our l10n catalog
111+
@python_2_unicode_compatible
110112
class MockLazyStr(object):
111113
def __init__(self, string):
112114
self.s = string
113115

114116
def __str__(self):
115117
return self.s
116118

117-
def __unicode__(self):
118-
return self.s
119-
120119
class MockView(APIView):
121120
__doc__ = MockLazyStr("a gettext string")
122121

tests/test_relations_generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey
44
from django.db import models
55
from django.test import TestCase
6+
from django.utils.encoding import python_2_unicode_compatible
67
from rest_framework import serializers
7-
from rest_framework.compat import python_2_unicode_compatible
88

99

1010
@python_2_unicode_compatible

0 commit comments

Comments
 (0)