Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
5ae94547
Commit
5ae94547
authored
Feb 18, 2014
by
Xavier Ordoquy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved the python_2_unicode_compatible into compat module.
parent
d4984602
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
19 deletions
+21
-19
rest_framework/compat.py
+20
-0
rest_framework/tests/test_genericrelations.py
+1
-19
No files found.
rest_framework/compat.py
View file @
5ae94547
...
@@ -584,3 +584,23 @@ if six.PY3:
...
@@ -584,3 +584,23 @@ if six.PY3:
else
:
else
:
def
is_non_str_iterable
(
obj
):
def
is_non_str_iterable
(
obj
):
return
hasattr
(
obj
,
'__iter__'
)
return
hasattr
(
obj
,
'__iter__'
)
try
:
from
django.utils.encoding
import
python_2_unicode_compatible
except
ImportError
:
def
python_2_unicode_compatible
(
klass
):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if
'__str__'
not
in
klass
.
__dict__
:
raise
ValueError
(
"@python_2_unicode_compatible cannot be applied "
"to
%
s because it doesn't define __str__()."
%
klass
.
__name__
)
klass
.
__unicode__
=
klass
.
__str__
klass
.
__str__
=
lambda
self
:
self
.
__unicode__
()
.
encode
(
'utf-8'
)
return
klass
rest_framework/tests/test_genericrelations.py
View file @
5ae94547
...
@@ -4,25 +4,7 @@ from django.contrib.contenttypes.generic import GenericRelation, GenericForeignK
...
@@ -4,25 +4,7 @@ from django.contrib.contenttypes.generic import GenericRelation, GenericForeignK
from
django.db
import
models
from
django.db
import
models
from
django.test
import
TestCase
from
django.test
import
TestCase
from
rest_framework
import
serializers
from
rest_framework
import
serializers
from
rest_framework.compat
import
python_2_unicode_compatible
try
:
from
django.utils.encoding
import
python_2_unicode_compatible
except
ImportError
:
def
python_2_unicode_compatible
(
klass
):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if
'__str__'
not
in
klass
.
__dict__
:
raise
ValueError
(
"@python_2_unicode_compatible cannot be applied "
"to
%
s because it doesn't define __str__()."
%
klass
.
__name__
)
klass
.
__unicode__
=
klass
.
__str__
klass
.
__str__
=
lambda
self
:
self
.
__unicode__
()
.
encode
(
'utf-8'
)
return
klass
@python_2_unicode_compatible
@python_2_unicode_compatible
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment