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
06d8a31e
Commit
06d8a31e
authored
Dec 09, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Catch and mask ParseErrors that occur during rendering of the BrowsableAPI.
parent
774298f1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
6 deletions
+17
-6
rest_framework/renderers.py
+7
-2
rest_framework/request.py
+1
-1
rest_framework/tests/test_renderers.py
+9
-3
No files found.
rest_framework/renderers.py
View file @
06d8a31e
...
@@ -20,6 +20,7 @@ from rest_framework.compat import StringIO
...
@@ -20,6 +20,7 @@ from rest_framework.compat import StringIO
from
rest_framework.compat
import
six
from
rest_framework.compat
import
six
from
rest_framework.compat
import
smart_text
from
rest_framework.compat
import
smart_text
from
rest_framework.compat
import
yaml
from
rest_framework.compat
import
yaml
from
rest_framework.exceptions
import
ParseError
from
rest_framework.settings
import
api_settings
from
rest_framework.settings
import
api_settings
from
rest_framework.request
import
is_form_media_type
,
override_method
from
rest_framework.request
import
is_form_media_type
,
override_method
from
rest_framework.utils
import
encoders
from
rest_framework.utils
import
encoders
...
@@ -420,8 +421,12 @@ class BrowsableAPIRenderer(BaseRenderer):
...
@@ -420,8 +421,12 @@ class BrowsableAPIRenderer(BaseRenderer):
In the absence of the View having an associated form then return None.
In the absence of the View having an associated form then return None.
"""
"""
if
request
.
method
==
method
:
if
request
.
method
==
method
:
data
=
request
.
DATA
try
:
files
=
request
.
FILES
data
=
request
.
DATA
files
=
request
.
FILES
except
ParseError
:
data
=
None
files
=
None
else
:
else
:
data
=
None
data
=
None
files
=
None
files
=
None
...
...
rest_framework/request.py
View file @
06d8a31e
...
@@ -362,7 +362,7 @@ class Request(object):
...
@@ -362,7 +362,7 @@ class Request(object):
# If we get an exception during parsing, fill in empty data and
# If we get an exception during parsing, fill in empty data and
# re-raise. Ensures we don't simply repeat the error when
# re-raise. Ensures we don't simply repeat the error when
# attempting to render the browsable renderer response, or when
# attempting to render the browsable renderer response, or when
# logging the request or similar.
# logging the request or similar.
self
.
_data
=
QueryDict
(
''
,
self
.
_request
.
_encoding
)
self
.
_data
=
QueryDict
(
''
,
self
.
_request
.
_encoding
)
self
.
_files
=
MultiValueDict
()
self
.
_files
=
MultiValueDict
()
raise
raise
...
...
rest_framework/tests/test_renderers.py
View file @
06d8a31e
...
@@ -69,6 +69,12 @@ class MockGETView(APIView):
...
@@ -69,6 +69,12 @@ class MockGETView(APIView):
return
Response
({
'foo'
:
[
'bar'
,
'baz'
]})
return
Response
({
'foo'
:
[
'bar'
,
'baz'
]})
class
MockPOSTView
(
APIView
):
def
post
(
self
,
request
,
**
kwargs
):
return
Response
({
'foo'
:
request
.
DATA
})
class
HTMLView
(
APIView
):
class
HTMLView
(
APIView
):
renderer_classes
=
(
BrowsableAPIRenderer
,
)
renderer_classes
=
(
BrowsableAPIRenderer
,
)
...
@@ -88,7 +94,7 @@ urlpatterns = patterns('',
...
@@ -88,7 +94,7 @@ urlpatterns = patterns('',
url
(
r'^cache$'
,
MockGETView
.
as_view
()),
url
(
r'^cache$'
,
MockGETView
.
as_view
()),
url
(
r'^jsonp/jsonrenderer$'
,
MockGETView
.
as_view
(
renderer_classes
=
[
JSONRenderer
,
JSONPRenderer
])),
url
(
r'^jsonp/jsonrenderer$'
,
MockGETView
.
as_view
(
renderer_classes
=
[
JSONRenderer
,
JSONPRenderer
])),
url
(
r'^jsonp/nojsonrenderer$'
,
MockGETView
.
as_view
(
renderer_classes
=
[
JSONPRenderer
])),
url
(
r'^jsonp/nojsonrenderer$'
,
MockGETView
.
as_view
(
renderer_classes
=
[
JSONPRenderer
])),
url
(
r'^parseerror$'
,
Mock
GE
TView
.
as_view
(
renderer_classes
=
[
JSONRenderer
,
BrowsableAPIRenderer
])),
url
(
r'^parseerror$'
,
Mock
POS
TView
.
as_view
(
renderer_classes
=
[
JSONRenderer
,
BrowsableAPIRenderer
])),
url
(
r'^html$'
,
HTMLView
.
as_view
()),
url
(
r'^html$'
,
HTMLView
.
as_view
()),
url
(
r'^html1$'
,
HTMLView1
.
as_view
()),
url
(
r'^html1$'
,
HTMLView1
.
as_view
()),
url
(
r'^api'
,
include
(
'rest_framework.urls'
,
namespace
=
'rest_framework'
))
url
(
r'^api'
,
include
(
'rest_framework.urls'
,
namespace
=
'rest_framework'
))
...
@@ -224,8 +230,8 @@ class RendererEndToEndTests(TestCase):
...
@@ -224,8 +230,8 @@ class RendererEndToEndTests(TestCase):
"""Invalid data should still render the browsable API correctly."""
"""Invalid data should still render the browsable API correctly."""
resp
=
self
.
client
.
post
(
'/parseerror'
,
data
=
'foobar'
,
content_type
=
'application/json'
,
HTTP_ACCEPT
=
'text/html'
)
resp
=
self
.
client
.
post
(
'/parseerror'
,
data
=
'foobar'
,
content_type
=
'application/json'
,
HTTP_ACCEPT
=
'text/html'
)
self
.
assertEqual
(
resp
[
'Content-Type'
],
'text/html; charset=utf-8'
)
self
.
assertEqual
(
resp
[
'Content-Type'
],
'text/html; charset=utf-8'
)
self
.
assert
Contains
(
resp
.
content
,
'Mock GET View'
)
self
.
assert
In
(
'Mock Post'
,
resp
.
content
)
self
.
assertEqual
(
resp
.
status_code
,
status
.
HTTP_400_
)
self
.
assertEqual
(
resp
.
status_code
,
status
.
HTTP_400_
BAD_REQUEST
)
_flat_repr
=
'{"foo": ["bar", "baz"]}'
_flat_repr
=
'{"foo": ["bar", "baz"]}'
_indented_repr
=
'{
\n
"foo": [
\n
"bar",
\n
"baz"
\n
]
\n
}'
_indented_repr
=
'{
\n
"foo": [
\n
"bar",
\n
"baz"
\n
]
\n
}'
...
...
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