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
ed281be3
Commit
ed281be3
authored
Oct 05, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
User .accepted_renderer, .accepted_media_type
parent
d07dc77e
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
12 additions
and
27 deletions
+12
-27
rest_framework/tests/negotiation.py
+1
-22
rest_framework/views.py
+11
-5
No files found.
rest_framework/tests/negotiation.py
View file @
ed281be3
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.test.client
import
RequestFactory
from
django.test.client
import
RequestFactory
from
rest_framework.decorators
import
api_view
,
renderer_classes
from
rest_framework.negotiation
import
DefaultContentNegotiation
from
rest_framework.negotiation
import
DefaultContentNegotiation
from
rest_framework.response
import
Response
factory
=
RequestFactory
()
factory
=
RequestFactory
()
...
@@ -10,26 +8,14 @@ factory = RequestFactory()
...
@@ -10,26 +8,14 @@ factory = RequestFactory()
class
MockJSONRenderer
(
object
):
class
MockJSONRenderer
(
object
):
media_type
=
'application/json'
media_type
=
'application/json'
def
__init__
(
self
,
view
):
pass
class
MockHTMLRenderer
(
object
):
class
MockHTMLRenderer
(
object
):
media_type
=
'text/html'
media_type
=
'text/html'
def
__init__
(
self
,
view
):
pass
@api_view
((
'GET'
,))
@renderer_classes
((
MockJSONRenderer
,
MockHTMLRenderer
))
def
example
(
request
):
return
Response
()
class
TestAcceptedMediaType
(
TestCase
):
class
TestAcceptedMediaType
(
TestCase
):
def
setUp
(
self
):
def
setUp
(
self
):
self
.
renderers
=
[
MockJSONRenderer
(
None
),
MockHTMLRenderer
(
None
)]
self
.
renderers
=
[
MockJSONRenderer
(
),
MockHTMLRenderer
(
)]
self
.
negotiator
=
DefaultContentNegotiation
()
self
.
negotiator
=
DefaultContentNegotiation
()
def
negotiate
(
self
,
request
):
def
negotiate
(
self
,
request
):
...
@@ -49,10 +35,3 @@ class TestAcceptedMediaType(TestCase):
...
@@ -49,10 +35,3 @@ class TestAcceptedMediaType(TestCase):
request
=
factory
.
get
(
'/'
,
HTTP_ACCEPT
=
'application/json; indent=8'
)
request
=
factory
.
get
(
'/'
,
HTTP_ACCEPT
=
'application/json; indent=8'
)
accepted_renderer
,
accepted_media_type
=
self
.
negotiate
(
request
)
accepted_renderer
,
accepted_media_type
=
self
.
negotiate
(
request
)
self
.
assertEquals
(
accepted_media_type
,
'application/json; indent=8'
)
self
.
assertEquals
(
accepted_media_type
,
'application/json; indent=8'
)
class
IntegrationTests
(
TestCase
):
def
test_accepted_negotiation_set_on_request
(
self
):
request
=
factory
.
get
(
'/'
,
HTTP_ACCEPT
=
'*/*'
)
response
=
example
(
request
)
self
.
assertEquals
(
response
.
accepted_media_type
,
'application/json'
)
rest_framework/views.py
View file @
ed281be3
...
@@ -199,20 +199,26 @@ class APIView(View):
...
@@ -199,20 +199,26 @@ class APIView(View):
Runs anything that needs to occur prior to calling the method handlers.
Runs anything that needs to occur prior to calling the method handlers.
"""
"""
self
.
format
=
self
.
get_format_suffix
(
**
kwargs
)
self
.
format
=
self
.
get_format_suffix
(
**
kwargs
)
if
not
self
.
has_permission
(
request
):
if
not
self
.
has_permission
(
request
):
self
.
permission_denied
(
request
)
self
.
permission_denied
(
request
)
self
.
check_throttles
(
request
)
self
.
check_throttles
(
request
)
self
.
renderer
,
self
.
accepted_media_type
=
self
.
perform_content_negotiation
(
request
)
# Perform content negotiation and store the accepted info on the request
neg
=
self
.
perform_content_negotiation
(
request
)
request
.
accepted_renderer
,
request
.
accepted_media_type
=
neg
def
finalize_response
(
self
,
request
,
response
,
*
args
,
**
kwargs
):
def
finalize_response
(
self
,
request
,
response
,
*
args
,
**
kwargs
):
"""
"""
Returns the final response object.
Returns the final response object.
"""
"""
if
isinstance
(
response
,
Response
):
if
isinstance
(
response
,
Response
):
if
not
getattr
(
self
,
'renderer'
,
None
):
if
not
getattr
(
request
,
'accepted_renderer'
,
None
):
self
.
renderer
,
self
.
accepted_media_type
=
self
.
perform_content_negotiation
(
request
,
force
=
True
)
neg
=
self
.
perform_content_negotiation
(
request
,
force
=
True
)
response
.
accepted_renderer
=
self
.
renderer
request
.
accepted_renderer
,
request
.
accepted_media_type
=
neg
response
.
accepted_media_type
=
self
.
accepted_media_type
response
.
accepted_renderer
=
request
.
accepted_renderer
response
.
accepted_media_type
=
request
.
accepted_media_type
for
key
,
value
in
self
.
headers
.
items
():
for
key
,
value
in
self
.
headers
.
items
():
response
[
key
]
=
value
response
[
key
]
=
value
...
...
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