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
17000129
Commit
17000129
authored
Nov 24, 2012
by
Xavier Ordoquy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Every (base) test should now pass with python3.
parent
237e3512
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
43 additions
and
26 deletions
+43
-26
rest_framework/authtoken/models.py
+2
-2
rest_framework/response.py
+3
-1
rest_framework/tests/authentication.py
+2
-2
rest_framework/tests/files.py
+2
-1
rest_framework/tests/generics.py
+3
-1
rest_framework/tests/htmlrenderer.py
+6
-4
rest_framework/tests/renderers.py
+10
-6
rest_framework/tests/request.py
+7
-5
rest_framework/tests/response.py
+4
-3
rest_framework/utils/__init__.py
+4
-1
No files found.
rest_framework/authtoken/models.py
View file @
17000129
...
@@ -19,8 +19,8 @@ class Token(models.Model):
...
@@ -19,8 +19,8 @@ class Token(models.Model):
return
super
(
Token
,
self
)
.
save
(
*
args
,
**
kwargs
)
return
super
(
Token
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
generate_key
(
self
):
def
generate_key
(
self
):
unique
=
str
(
uuid
.
uuid4
()
)
unique
=
uuid
.
uuid4
(
)
return
hmac
.
new
(
unique
,
digestmod
=
sha1
)
.
hexdigest
()
return
hmac
.
new
(
unique
.
bytes
,
digestmod
=
sha1
)
.
hexdigest
()
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
self
.
key
return
self
.
key
rest_framework/response.py
View file @
17000129
import
six
from
django.core.handlers.wsgi
import
STATUS_CODE_TEXT
from
django.core.handlers.wsgi
import
STATUS_CODE_TEXT
from
django.template.response
import
SimpleTemplateResponse
from
django.template.response
import
SimpleTemplateResponse
...
@@ -24,7 +26,7 @@ class Response(SimpleTemplateResponse):
...
@@ -24,7 +26,7 @@ class Response(SimpleTemplateResponse):
self
.
exception
=
exception
self
.
exception
=
exception
if
headers
:
if
headers
:
for
name
,
value
in
headers
.
iteritems
(
):
for
name
,
value
in
six
.
iteritems
(
headers
):
self
[
name
]
=
value
self
[
name
]
=
value
@property
@property
...
...
rest_framework/tests/authentication.py
View file @
17000129
...
@@ -160,7 +160,7 @@ class TokenAuthTests(TestCase):
...
@@ -160,7 +160,7 @@ class TokenAuthTests(TestCase):
response
=
client
.
post
(
'/auth-token/login/'
,
response
=
client
.
post
(
'/auth-token/login/'
,
json
.
dumps
({
'username'
:
self
.
username
,
'password'
:
self
.
password
}),
'application/json'
)
json
.
dumps
({
'username'
:
self
.
username
,
'password'
:
self
.
password
}),
'application/json'
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
)[
'token'
],
self
.
key
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
.
decode
(
'ascii'
)
)[
'token'
],
self
.
key
)
def
test_token_login_json_bad_creds
(
self
):
def
test_token_login_json_bad_creds
(
self
):
"""Ensure token login view using JSON POST fails if bad credentials are used."""
"""Ensure token login view using JSON POST fails if bad credentials are used."""
...
@@ -182,4 +182,4 @@ class TokenAuthTests(TestCase):
...
@@ -182,4 +182,4 @@ class TokenAuthTests(TestCase):
response
=
client
.
post
(
'/auth-token/login/'
,
response
=
client
.
post
(
'/auth-token/login/'
,
{
'username'
:
self
.
username
,
'password'
:
self
.
password
})
{
'username'
:
self
.
username
,
'password'
:
self
.
password
})
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
response
.
status_code
,
200
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
)[
'token'
],
self
.
key
)
self
.
assertEqual
(
json
.
loads
(
response
.
content
.
decode
(
'ascii'
)
)[
'token'
],
self
.
key
)
rest_framework/tests/files.py
View file @
17000129
from
rest_framework.compat
import
BytesIO
from
rest_framework.compat
import
BytesIO
import
datetime
import
datetime
import
six
from
django.test
import
TestCase
from
django.test
import
TestCase
...
@@ -29,7 +30,7 @@ class FileSerializerTests(TestCase):
...
@@ -29,7 +30,7 @@ class FileSerializerTests(TestCase):
def
test_create
(
self
):
def
test_create
(
self
):
now
=
datetime
.
datetime
.
now
()
now
=
datetime
.
datetime
.
now
()
file
=
BytesIO
(
b
'stuff'
)
file
=
BytesIO
(
six
.
b
(
'stuff'
)
)
file
.
name
=
'stuff.txt'
file
.
name
=
'stuff.txt'
file
.
size
=
len
(
file
.
getvalue
())
file
.
size
=
len
(
file
.
getvalue
())
serializer
=
UploadedFileSerializer
(
data
=
{
'created'
:
now
},
files
=
{
'file'
:
file
})
serializer
=
UploadedFileSerializer
(
data
=
{
'created'
:
now
},
files
=
{
'file'
:
file
})
...
...
rest_framework/tests/generics.py
View file @
17000129
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
six
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.test.client
import
RequestFactory
from
django.utils
import
simplejson
as
json
from
django.utils
import
simplejson
as
json
...
@@ -189,7 +191,7 @@ class TestInstanceView(TestCase):
...
@@ -189,7 +191,7 @@ class TestInstanceView(TestCase):
request
=
factory
.
delete
(
'/1'
)
request
=
factory
.
delete
(
'/1'
)
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
response
=
self
.
view
(
request
,
pk
=
1
)
.
render
()
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertEquals
(
response
.
status_code
,
status
.
HTTP_204_NO_CONTENT
)
self
.
assertEquals
(
response
.
content
,
''
)
self
.
assertEquals
(
response
.
content
,
six
.
b
(
''
)
)
ids
=
[
obj
.
id
for
obj
in
self
.
objects
.
all
()]
ids
=
[
obj
.
id
for
obj
in
self
.
objects
.
all
()]
self
.
assertEquals
(
ids
,
[
2
,
3
])
self
.
assertEquals
(
ids
,
[
2
,
3
])
...
...
rest_framework/tests/htmlrenderer.py
View file @
17000129
import
six
from
django.core.exceptions
import
PermissionDenied
from
django.core.exceptions
import
PermissionDenied
from
django.conf.urls.defaults
import
patterns
,
url
from
django.conf.urls.defaults
import
patterns
,
url
from
django.http
import
Http404
from
django.http
import
Http404
...
@@ -68,13 +70,13 @@ class TemplateHTMLRendererTests(TestCase):
...
@@ -68,13 +70,13 @@ class TemplateHTMLRendererTests(TestCase):
def
test_not_found_html_view
(
self
):
def
test_not_found_html_view
(
self
):
response
=
self
.
client
.
get
(
'/not_found'
)
response
=
self
.
client
.
get
(
'/not_found'
)
self
.
assertEquals
(
response
.
status_code
,
404
)
self
.
assertEquals
(
response
.
status_code
,
404
)
self
.
assertEquals
(
response
.
content
,
"404 Not Found"
)
self
.
assertEquals
(
response
.
content
,
six
.
b
(
"404 Not Found"
)
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
def
test_permission_denied_html_view
(
self
):
def
test_permission_denied_html_view
(
self
):
response
=
self
.
client
.
get
(
'/permission_denied'
)
response
=
self
.
client
.
get
(
'/permission_denied'
)
self
.
assertEquals
(
response
.
status_code
,
403
)
self
.
assertEquals
(
response
.
status_code
,
403
)
self
.
assertEquals
(
response
.
content
,
"403 Forbidden"
)
self
.
assertEquals
(
response
.
content
,
six
.
b
(
"403 Forbidden"
)
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
...
@@ -105,11 +107,11 @@ class TemplateHTMLRendererExceptionTests(TestCase):
...
@@ -105,11 +107,11 @@ class TemplateHTMLRendererExceptionTests(TestCase):
def
test_not_found_html_view_with_template
(
self
):
def
test_not_found_html_view_with_template
(
self
):
response
=
self
.
client
.
get
(
'/not_found'
)
response
=
self
.
client
.
get
(
'/not_found'
)
self
.
assertEquals
(
response
.
status_code
,
404
)
self
.
assertEquals
(
response
.
status_code
,
404
)
self
.
assertEquals
(
response
.
content
,
"404: Not found"
)
self
.
assertEquals
(
response
.
content
,
six
.
b
(
"404: Not found"
)
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
def
test_permission_denied_html_view_with_template
(
self
):
def
test_permission_denied_html_view_with_template
(
self
):
response
=
self
.
client
.
get
(
'/permission_denied'
)
response
=
self
.
client
.
get
(
'/permission_denied'
)
self
.
assertEquals
(
response
.
status_code
,
403
)
self
.
assertEquals
(
response
.
status_code
,
403
)
self
.
assertEquals
(
response
.
content
,
"403: Permission denied"
)
self
.
assertEquals
(
response
.
content
,
six
.
b
(
"403: Permission denied"
)
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
self
.
assertEquals
(
response
[
'Content-Type'
],
'text/html'
)
rest_framework/tests/renderers.py
View file @
17000129
import
pickle
import
pickle
import
re
import
re
import
six
from
django.conf.urls.defaults
import
patterns
,
url
,
include
from
django.conf.urls.defaults
import
patterns
,
url
,
include
from
django.core.cache
import
cache
from
django.core.cache
import
cache
...
@@ -23,8 +24,8 @@ from decimal import Decimal
...
@@ -23,8 +24,8 @@ from decimal import Decimal
DUMMYSTATUS
=
status
.
HTTP_200_OK
DUMMYSTATUS
=
status
.
HTTP_200_OK
DUMMYCONTENT
=
'dummycontent'
DUMMYCONTENT
=
'dummycontent'
RENDERER_A_SERIALIZER
=
lambda
x
:
'Renderer A:
%
s'
%
x
RENDERER_A_SERIALIZER
=
lambda
x
:
(
'Renderer A:
%
s'
%
x
)
.
encode
(
'ascii'
)
RENDERER_B_SERIALIZER
=
lambda
x
:
'Renderer B:
%
s'
%
x
RENDERER_B_SERIALIZER
=
lambda
x
:
(
'Renderer B:
%
s'
%
x
)
.
encode
(
'ascii'
)
expected_results
=
[
expected_results
=
[
...
@@ -141,7 +142,7 @@ class RendererEndToEndTests(TestCase):
...
@@ -141,7 +142,7 @@ class RendererEndToEndTests(TestCase):
resp
=
self
.
client
.
head
(
'/'
)
resp
=
self
.
client
.
head
(
'/'
)
self
.
assertEquals
(
resp
.
status_code
,
DUMMYSTATUS
)
self
.
assertEquals
(
resp
.
status_code
,
DUMMYSTATUS
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
RendererA
.
media_type
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
RendererA
.
media_type
)
self
.
assertEquals
(
resp
.
content
,
''
)
self
.
assertEquals
(
resp
.
content
,
six
.
b
(
''
)
)
def
test_default_renderer_serializes_content_on_accept_any
(
self
):
def
test_default_renderer_serializes_content_on_accept_any
(
self
):
"""If the Accept header is set to */* the default renderer should serialize the response."""
"""If the Accept header is set to */* the default renderer should serialize the response."""
...
@@ -268,7 +269,8 @@ class JSONPRendererTests(TestCase):
...
@@ -268,7 +269,8 @@ class JSONPRendererTests(TestCase):
HTTP_ACCEPT
=
'application/javascript'
)
HTTP_ACCEPT
=
'application/javascript'
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
.
content
,
'callback(
%
s);'
%
_flat_repr
)
self
.
assertEquals
(
resp
.
content
,
(
'callback(
%
s);'
%
_flat_repr
)
.
encode
(
'ascii'
))
def
test_without_callback_without_json_renderer
(
self
):
def
test_without_callback_without_json_renderer
(
self
):
"""
"""
...
@@ -278,7 +280,8 @@ class JSONPRendererTests(TestCase):
...
@@ -278,7 +280,8 @@ class JSONPRendererTests(TestCase):
HTTP_ACCEPT
=
'application/javascript'
)
HTTP_ACCEPT
=
'application/javascript'
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
.
content
,
'callback(
%
s);'
%
_flat_repr
)
self
.
assertEquals
(
resp
.
content
,
(
'callback(
%
s);'
%
_flat_repr
)
.
encode
(
'ascii'
))
def
test_with_callback
(
self
):
def
test_with_callback
(
self
):
"""
"""
...
@@ -289,7 +292,8 @@ class JSONPRendererTests(TestCase):
...
@@ -289,7 +292,8 @@ class JSONPRendererTests(TestCase):
HTTP_ACCEPT
=
'application/javascript'
)
HTTP_ACCEPT
=
'application/javascript'
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
.
status_code
,
200
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
'application/javascript'
)
self
.
assertEquals
(
resp
.
content
,
'
%
s(
%
s);'
%
(
callback_func
,
_flat_repr
))
self
.
assertEquals
(
resp
.
content
,
(
'
%
s(
%
s);'
%
(
callback_func
,
_flat_repr
))
.
encode
(
'ascii'
))
if
yaml
:
if
yaml
:
...
...
rest_framework/tests/request.py
View file @
17000129
"""
"""
Tests for content parsing, and form-overloaded content parsing.
Tests for content parsing, and form-overloaded content parsing.
"""
"""
import
six
from
django.conf.urls.defaults
import
patterns
from
django.conf.urls.defaults
import
patterns
from
django.contrib.auth.models
import
User
from
django.contrib.auth.models
import
User
from
django.test
import
TestCase
,
Client
from
django.test
import
TestCase
,
Client
...
@@ -78,14 +80,14 @@ class TestContentParsing(TestCase):
...
@@ -78,14 +80,14 @@ class TestContentParsing(TestCase):
data
=
{
'qwerty'
:
'uiop'
}
data
=
{
'qwerty'
:
'uiop'
}
request
=
Request
(
factory
.
post
(
'/'
,
data
))
request
=
Request
(
factory
.
post
(
'/'
,
data
))
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
self
.
assertEqual
(
request
.
DATA
.
items
(),
data
.
items
(
))
self
.
assertEqual
(
list
(
request
.
DATA
.
items
()),
list
(
data
.
items
()
))
def
test_request_DATA_with_text_content
(
self
):
def
test_request_DATA_with_text_content
(
self
):
"""
"""
Ensure request.DATA returns content for POST request with
Ensure request.DATA returns content for POST request with
non-form content.
non-form content.
"""
"""
content
=
'qwerty'
content
=
six
.
b
(
'qwerty'
)
content_type
=
'text/plain'
content_type
=
'text/plain'
request
=
Request
(
factory
.
post
(
'/'
,
content
,
content_type
=
content_type
))
request
=
Request
(
factory
.
post
(
'/'
,
content
,
content_type
=
content_type
))
request
.
parsers
=
(
PlainTextParser
(),)
request
.
parsers
=
(
PlainTextParser
(),)
...
@@ -98,7 +100,7 @@ class TestContentParsing(TestCase):
...
@@ -98,7 +100,7 @@ class TestContentParsing(TestCase):
data
=
{
'qwerty'
:
'uiop'
}
data
=
{
'qwerty'
:
'uiop'
}
request
=
Request
(
factory
.
post
(
'/'
,
data
))
request
=
Request
(
factory
.
post
(
'/'
,
data
))
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
self
.
assertEqual
(
request
.
POST
.
items
(),
data
.
items
(
))
self
.
assertEqual
(
list
(
request
.
POST
.
items
()),
list
(
data
.
items
()
))
def
test_standard_behaviour_determines_form_content_PUT
(
self
):
def
test_standard_behaviour_determines_form_content_PUT
(
self
):
"""
"""
...
@@ -116,14 +118,14 @@ class TestContentParsing(TestCase):
...
@@ -116,14 +118,14 @@ class TestContentParsing(TestCase):
request
=
Request
(
factory
.
put
(
'/'
,
data
))
request
=
Request
(
factory
.
put
(
'/'
,
data
))
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
request
.
parsers
=
(
FormParser
(),
MultiPartParser
())
self
.
assertEqual
(
request
.
DATA
.
items
(),
data
.
items
(
))
self
.
assertEqual
(
list
(
request
.
DATA
.
items
()),
list
(
data
.
items
()
))
def
test_standard_behaviour_determines_non_form_content_PUT
(
self
):
def
test_standard_behaviour_determines_non_form_content_PUT
(
self
):
"""
"""
Ensure request.DATA returns content for PUT request with
Ensure request.DATA returns content for PUT request with
non-form content.
non-form content.
"""
"""
content
=
'qwerty'
content
=
six
.
b
(
'qwerty'
)
content_type
=
'text/plain'
content_type
=
'text/plain'
request
=
Request
(
factory
.
put
(
'/'
,
content
,
content_type
=
content_type
))
request
=
Request
(
factory
.
put
(
'/'
,
content
,
content_type
=
content_type
))
request
.
parsers
=
(
PlainTextParser
(),
)
request
.
parsers
=
(
PlainTextParser
(),
)
...
...
rest_framework/tests/response.py
View file @
17000129
import
unittest
import
unittest
import
six
from
django.conf.urls.defaults
import
patterns
,
url
,
include
from
django.conf.urls.defaults
import
patterns
,
url
,
include
from
django.test
import
TestCase
from
django.test
import
TestCase
...
@@ -25,8 +26,8 @@ class MockJsonRenderer(BaseRenderer):
...
@@ -25,8 +26,8 @@ class MockJsonRenderer(BaseRenderer):
DUMMYSTATUS
=
status
.
HTTP_200_OK
DUMMYSTATUS
=
status
.
HTTP_200_OK
DUMMYCONTENT
=
'dummycontent'
DUMMYCONTENT
=
'dummycontent'
RENDERER_A_SERIALIZER
=
lambda
x
:
'Renderer A:
%
s'
%
x
RENDERER_A_SERIALIZER
=
lambda
x
:
(
'Renderer A:
%
s'
%
x
)
.
encode
(
'ascii'
)
RENDERER_B_SERIALIZER
=
lambda
x
:
'Renderer B:
%
s'
%
x
RENDERER_B_SERIALIZER
=
lambda
x
:
(
'Renderer B:
%
s'
%
x
)
.
encode
(
'ascii'
)
class
RendererA
(
BaseRenderer
):
class
RendererA
(
BaseRenderer
):
...
@@ -95,7 +96,7 @@ class RendererIntegrationTests(TestCase):
...
@@ -95,7 +96,7 @@ class RendererIntegrationTests(TestCase):
resp
=
self
.
client
.
head
(
'/'
)
resp
=
self
.
client
.
head
(
'/'
)
self
.
assertEquals
(
resp
.
status_code
,
DUMMYSTATUS
)
self
.
assertEquals
(
resp
.
status_code
,
DUMMYSTATUS
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
RendererA
.
media_type
)
self
.
assertEquals
(
resp
[
'Content-Type'
],
RendererA
.
media_type
)
self
.
assertEquals
(
resp
.
content
,
''
)
self
.
assertEquals
(
resp
.
content
,
six
.
b
(
''
)
)
def
test_default_renderer_serializes_content_on_accept_any
(
self
):
def
test_default_renderer_serializes_content_on_accept_any
(
self
):
"""If the Accept header is set to */* the default renderer should serialize the response."""
"""If the Accept header is set to */* the default renderer should serialize the response."""
...
...
rest_framework/utils/__init__.py
View file @
17000129
import
six
try
:
try
:
from
django.utils.encoding
import
smart_text
from
django.utils.encoding
import
smart_text
except
ImportError
:
except
ImportError
:
from
django.utils.encoding
import
smart_unicode
as
smart_text
from
django.utils.encoding
import
smart_unicode
as
smart_text
from
django.utils.xmlutils
import
SimplerXMLGenerator
from
django.utils.xmlutils
import
SimplerXMLGenerator
from
rest_framework.compat
import
StringIO
from
rest_framework.compat
import
StringIO
import
re
import
re
...
@@ -74,7 +77,7 @@ class XMLRenderer():
...
@@ -74,7 +77,7 @@ class XMLRenderer():
xml
.
endElement
(
"list-item"
)
xml
.
endElement
(
"list-item"
)
elif
isinstance
(
data
,
dict
):
elif
isinstance
(
data
,
dict
):
for
key
,
value
in
data
.
iteritems
(
):
for
key
,
value
in
six
.
iteritems
(
data
):
xml
.
startElement
(
key
,
{})
xml
.
startElement
(
key
,
{})
self
.
_to_xml
(
xml
,
value
)
self
.
_to_xml
(
xml
,
value
)
xml
.
endElement
(
key
)
xml
.
endElement
(
key
)
...
...
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