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
b522cc8e
Commit
b522cc8e
authored
Jan 11, 2012
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge
https://github.com/dzen/django-rest-framework
parents
0cfe2acd
35ba2fc6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
12 deletions
+30
-12
djangorestframework/tests/views.py
+16
-0
djangorestframework/views.py
+14
-12
No files found.
djangorestframework/tests/views.py
View file @
b522cc8e
from
django.conf.urls.defaults
import
patterns
,
url
from
django.http
import
HttpResponse
from
django.test
import
TestCase
from
django.test
import
Client
from
django
import
forms
...
...
@@ -16,6 +17,13 @@ class MockView(View):
"""This is a basic mock view"""
pass
class
MockViewFinal
(
View
):
"""View with final() override"""
def
final
(
self
,
request
,
response
,
*
args
,
**
kwargs
):
return
HttpResponse
(
'{"test": "passed"}'
,
content_type
=
"application/json"
)
class
ResourceMockView
(
View
):
"""This is a resource-based mock view"""
...
...
@@ -43,6 +51,7 @@ urlpatterns = patterns('djangorestframework.utils.staticviews',
url
(
r'^accounts/login$'
,
'api_login'
),
url
(
r'^accounts/logout$'
,
'api_logout'
),
url
(
r'^mock/$'
,
MockView
.
as_view
()),
url
(
r'^mock/final/$'
,
MockViewFinal
.
as_view
()),
url
(
r'^resourcemock/$'
,
ResourceMockView
.
as_view
()),
url
(
r'^model/$'
,
ListOrCreateModelView
.
as_view
(
resource
=
MockResource
)),
url
(
r'^model/(?P<pk>[^/]+)/$'
,
InstanceModelView
.
as_view
(
resource
=
MockResource
)),
...
...
@@ -52,6 +61,13 @@ class BaseViewTests(TestCase):
"""Test the base view class of djangorestframework"""
urls
=
'djangorestframework.tests.views'
def
test_view_call_final
(
self
):
response
=
self
.
client
.
options
(
'/mock/final/'
)
self
.
assertEqual
(
response
[
'Content-Type'
]
.
split
(
';'
)[
0
],
"application/json"
)
parser
=
JSONParser
(
None
)
(
data
,
files
)
=
parser
.
parse
(
StringIO
(
response
.
content
))
self
.
assertEqual
(
data
[
'test'
],
'passed'
)
def
test_options_method_simple_view
(
self
):
response
=
self
.
client
.
options
(
'/mock/'
)
self
.
_verify_options_response
(
response
,
...
...
djangorestframework/views.py
View file @
b522cc8e
...
...
@@ -154,19 +154,8 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
except
ErrorResponse
,
exc
:
response
=
exc
.
response
# Always add these headers.
#
# TODO - this isn't actually the correct way to set the vary header,
# also it's currently sub-optimal for HTTP caching - need to sort that out.
response
.
headers
[
'Allow'
]
=
', '
.
join
(
self
.
allowed_methods
)
response
.
headers
[
'Vary'
]
=
'Authenticate, Accept'
# merge with headers possibly set at some point in the view
response
.
headers
.
update
(
self
.
headers
)
set_script_prefix
(
orig_prefix
)
return
self
.
render
(
response
)
return
self
.
final
(
request
,
response
,
*
args
,
**
kwargs
)
def
options
(
self
,
request
,
*
args
,
**
kwargs
):
response_obj
=
{
...
...
@@ -183,6 +172,19 @@ class View(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, DjangoView):
response_obj
[
'fields'
]
=
field_name_types
return
response_obj
def
final
(
self
,
request
,
response
,
*
args
,
**
kargs
):
"""
Hook for any code that needs to run after everything else in the view.
"""
# Always add these headers.
response
.
headers
[
'Allow'
]
=
', '
.
join
(
self
.
allowed_methods
)
# sample to allow caching using Vary http header
response
.
headers
[
'Vary'
]
=
'Authenticate, Accept'
# merge with headers possibly set at some point in the view
response
.
headers
.
update
(
self
.
headers
)
return
self
.
render
(
response
)
class
ModelView
(
View
):
"""
...
...
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