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
370274f5
Commit
370274f5
authored
May 24, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow views to return HttpResponses. Add initial() hook method
parent
698df527
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
70 additions
and
57 deletions
+70
-57
djangorestframework/mixins.py
+1
-1
djangorestframework/tests/accept.py
+2
-2
djangorestframework/tests/authentication.py
+2
-2
djangorestframework/tests/breadcrumbs.py
+6
-6
djangorestframework/tests/description.py
+7
-7
djangorestframework/tests/files.py
+2
-2
djangorestframework/tests/parsers.py
+4
-4
djangorestframework/tests/renderers.py
+2
-2
djangorestframework/tests/reverse.py
+2
-2
djangorestframework/tests/throttling.py
+2
-2
djangorestframework/tests/validators.py
+9
-9
djangorestframework/views.py
+20
-7
examples/objectstore/views.py
+3
-3
examples/pygments_api/views.py
+3
-3
examples/resourceexample/views.py
+3
-3
examples/sandbox/views.py
+2
-2
No files found.
djangorestframework/mixins.py
View file @
370274f5
...
@@ -216,7 +216,7 @@ class RequestMixin(object):
...
@@ -216,7 +216,7 @@ class RequestMixin(object):
class
ResponseMixin
(
object
):
class
ResponseMixin
(
object
):
"""
"""
Adds behavior for pluggable `Renderers` to a :class:`views.
BaseView` or Django :class:`
View` class.
Adds behavior for pluggable `Renderers` to a :class:`views.View` class.
Default behavior is to use standard HTTP Accept header content negotiation.
Default behavior is to use standard HTTP Accept header content negotiation.
Also supports overriding the content type by specifying an ``_accept=`` parameter in the URL.
Also supports overriding the content type by specifying an ``_accept=`` parameter in the URL.
...
...
djangorestframework/tests/accept.py
View file @
370274f5
from
django.test
import
TestCase
from
django.test
import
TestCase
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
# See: http://www.useragentstring.com/
# See: http://www.useragentstring.com/
...
@@ -19,7 +19,7 @@ class UserAgentMungingTest(TestCase):
...
@@ -19,7 +19,7 @@ class UserAgentMungingTest(TestCase):
def
setUp
(
self
):
def
setUp
(
self
):
class
MockView
(
Base
View
):
class
MockView
(
View
):
permissions
=
()
permissions
=
()
def
get
(
self
,
request
):
def
get
(
self
,
request
):
...
...
djangorestframework/tests/authentication.py
View file @
370274f5
...
@@ -6,13 +6,13 @@ from django.test import Client, TestCase
...
@@ -6,13 +6,13 @@ from django.test import Client, TestCase
from
django.utils
import
simplejson
as
json
from
django.utils
import
simplejson
as
json
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework
import
permissions
from
djangorestframework
import
permissions
import
base64
import
base64
class
MockView
(
Base
View
):
class
MockView
(
View
):
permissions
=
(
permissions
.
IsAuthenticated
,
)
permissions
=
(
permissions
.
IsAuthenticated
,
)
def
post
(
self
,
request
):
def
post
(
self
,
request
):
return
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
return
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
...
...
djangorestframework/tests/breadcrumbs.py
View file @
370274f5
from
django.conf.urls.defaults
import
patterns
,
url
from
django.conf.urls.defaults
import
patterns
,
url
from
django.test
import
TestCase
from
django.test
import
TestCase
from
djangorestframework.utils.breadcrumbs
import
get_breadcrumbs
from
djangorestframework.utils.breadcrumbs
import
get_breadcrumbs
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
class
Root
(
Base
View
):
class
Root
(
View
):
pass
pass
class
ResourceRoot
(
Base
View
):
class
ResourceRoot
(
View
):
pass
pass
class
ResourceInstance
(
Base
View
):
class
ResourceInstance
(
View
):
pass
pass
class
NestedResourceRoot
(
Base
View
):
class
NestedResourceRoot
(
View
):
pass
pass
class
NestedResourceInstance
(
Base
View
):
class
NestedResourceInstance
(
View
):
pass
pass
urlpatterns
=
patterns
(
''
,
urlpatterns
=
patterns
(
''
,
...
...
djangorestframework/tests/description.py
View file @
370274f5
from
django.test
import
TestCase
from
django.test
import
TestCase
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.compat
import
apply_markdown
from
djangorestframework.compat
import
apply_markdown
from
djangorestframework.utils.description
import
get_name
,
get_description
from
djangorestframework.utils.description
import
get_name
,
get_description
...
@@ -35,7 +35,7 @@ MARKED_DOWN = """<h2>an example docstring</h2>
...
@@ -35,7 +35,7 @@ MARKED_DOWN = """<h2>an example docstring</h2>
class
TestViewNamesAndDescriptions
(
TestCase
):
class
TestViewNamesAndDescriptions
(
TestCase
):
def
test_resource_name_uses_classname_by_default
(
self
):
def
test_resource_name_uses_classname_by_default
(
self
):
"""Ensure Resource names are based on the classname by default."""
"""Ensure Resource names are based on the classname by default."""
class
MockView
(
Base
View
):
class
MockView
(
View
):
pass
pass
self
.
assertEquals
(
get_name
(
MockView
()),
'Mock'
)
self
.
assertEquals
(
get_name
(
MockView
()),
'Mock'
)
...
@@ -43,13 +43,13 @@ class TestViewNamesAndDescriptions(TestCase):
...
@@ -43,13 +43,13 @@ class TestViewNamesAndDescriptions(TestCase):
#def test_resource_name_can_be_set_explicitly(self):
#def test_resource_name_can_be_set_explicitly(self):
# """Ensure Resource names can be set using the 'name' class attribute."""
# """Ensure Resource names can be set using the 'name' class attribute."""
# example = 'Some Other Name'
# example = 'Some Other Name'
# class MockView(
Base
View):
# class MockView(View):
# name = example
# name = example
# self.assertEquals(get_name(MockView()), example)
# self.assertEquals(get_name(MockView()), example)
def
test_resource_description_uses_docstring_by_default
(
self
):
def
test_resource_description_uses_docstring_by_default
(
self
):
"""Ensure Resource names are based on the docstring by default."""
"""Ensure Resource names are based on the docstring by default."""
class
MockView
(
Base
View
):
class
MockView
(
View
):
"""an example docstring
"""an example docstring
====================
====================
...
@@ -71,7 +71,7 @@ class TestViewNamesAndDescriptions(TestCase):
...
@@ -71,7 +71,7 @@ class TestViewNamesAndDescriptions(TestCase):
#def test_resource_description_can_be_set_explicitly(self):
#def test_resource_description_can_be_set_explicitly(self):
# """Ensure Resource descriptions can be set using the 'description' class attribute."""
# """Ensure Resource descriptions can be set using the 'description' class attribute."""
# example = 'Some other description'
# example = 'Some other description'
# class MockView(
Base
View):
# class MockView(View):
# """docstring"""
# """docstring"""
# description = example
# description = example
# self.assertEquals(get_description(MockView()), example)
# self.assertEquals(get_description(MockView()), example)
...
@@ -79,13 +79,13 @@ class TestViewNamesAndDescriptions(TestCase):
...
@@ -79,13 +79,13 @@ class TestViewNamesAndDescriptions(TestCase):
#def test_resource_description_does_not_require_docstring(self):
#def test_resource_description_does_not_require_docstring(self):
# """Ensure that empty docstrings do not affect the Resource's description if it has been set using the 'description' class attribute."""
# """Ensure that empty docstrings do not affect the Resource's description if it has been set using the 'description' class attribute."""
# example = 'Some other description'
# example = 'Some other description'
# class MockView(
Base
View):
# class MockView(View):
# description = example
# description = example
# self.assertEquals(get_description(MockView()), example)
# self.assertEquals(get_description(MockView()), example)
def
test_resource_description_can_be_empty
(
self
):
def
test_resource_description_can_be_empty
(
self
):
"""Ensure that if a resource has no doctring or 'description' class attribute, then it's description is the empty string"""
"""Ensure that if a resource has no doctring or 'description' class attribute, then it's description is the empty string"""
class
MockView
(
Base
View
):
class
MockView
(
View
):
pass
pass
self
.
assertEquals
(
get_description
(
MockView
()),
''
)
self
.
assertEquals
(
get_description
(
MockView
()),
''
)
...
...
djangorestframework/tests/files.py
View file @
370274f5
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django
import
forms
from
django
import
forms
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.resources
import
FormResource
from
djangorestframework.resources
import
FormResource
import
StringIO
import
StringIO
...
@@ -19,7 +19,7 @@ class UploadFilesTests(TestCase):
...
@@ -19,7 +19,7 @@ class UploadFilesTests(TestCase):
class
MockResource
(
FormResource
):
class
MockResource
(
FormResource
):
form
=
FileForm
form
=
FileForm
class
MockView
(
Base
View
):
class
MockView
(
View
):
permissions
=
()
permissions
=
()
resource
=
MockResource
resource
=
MockResource
...
...
djangorestframework/tests/parsers.py
View file @
370274f5
...
@@ -2,11 +2,11 @@
...
@@ -2,11 +2,11 @@
# ..
# ..
# >>> from djangorestframework.parsers import FormParser
# >>> from djangorestframework.parsers import FormParser
# >>> from djangorestframework.compat import RequestFactory
# >>> from djangorestframework.compat import RequestFactory
# >>> from djangorestframework.views import
Base
View
# >>> from djangorestframework.views import View
# >>> from StringIO import StringIO
# >>> from StringIO import StringIO
# >>> from urllib import urlencode
# >>> from urllib import urlencode
# >>> req = RequestFactory().get('/')
# >>> req = RequestFactory().get('/')
# >>> some_view =
Base
View()
# >>> some_view = View()
# >>> some_view.request = req # Make as if this request had been dispatched
# >>> some_view.request = req # Make as if this request had been dispatched
#
#
# FormParser
# FormParser
...
@@ -85,7 +85,7 @@
...
@@ -85,7 +85,7 @@
# from django.test import TestCase
# from django.test import TestCase
# from djangorestframework.compat import RequestFactory
# from djangorestframework.compat import RequestFactory
# from djangorestframework.parsers import MultiPartParser
# from djangorestframework.parsers import MultiPartParser
# from djangorestframework.views import
Base
View
# from djangorestframework.views import View
# from StringIO import StringIO
# from StringIO import StringIO
#
#
# def encode_multipart_formdata(fields, files):
# def encode_multipart_formdata(fields, files):
...
@@ -125,7 +125,7 @@
...
@@ -125,7 +125,7 @@
# def test_multipartparser(self):
# def test_multipartparser(self):
# """Ensure that MultiPartParser can parse multipart/form-data that contains a mix of several files and parameters."""
# """Ensure that MultiPartParser can parse multipart/form-data that contains a mix of several files and parameters."""
# post_req = RequestFactory().post('/', self.body, content_type=self.content_type)
# post_req = RequestFactory().post('/', self.body, content_type=self.content_type)
# view =
Base
View()
# view = View()
# view.request = post_req
# view.request = post_req
# (data, files) = MultiPartParser(view).parse(StringIO(self.body))
# (data, files) = MultiPartParser(view).parse(StringIO(self.body))
# self.assertEqual(data['key1'], 'val1')
# self.assertEqual(data['key1'], 'val1')
...
...
djangorestframework/tests/renderers.py
View file @
370274f5
from
django.conf.urls.defaults
import
patterns
,
url
from
django.conf.urls.defaults
import
patterns
,
url
from
django
import
http
from
django
import
http
from
django.test
import
TestCase
from
django.test
import
TestCase
from
djangorestframework.compat
import
View
from
djangorestframework.compat
import
View
as
DjangoView
from
djangorestframework.renderers
import
BaseRenderer
,
JSONRenderer
from
djangorestframework.renderers
import
BaseRenderer
,
JSONRenderer
from
djangorestframework.mixins
import
ResponseMixin
from
djangorestframework.mixins
import
ResponseMixin
from
djangorestframework.response
import
Response
from
djangorestframework.response
import
Response
...
@@ -13,7 +13,7 @@ DUMMYCONTENT = 'dummycontent'
...
@@ -13,7 +13,7 @@ DUMMYCONTENT = 'dummycontent'
RENDERER_A_SERIALIZER
=
lambda
x
:
'Renderer A:
%
s'
%
x
RENDERER_A_SERIALIZER
=
lambda
x
:
'Renderer A:
%
s'
%
x
RENDERER_B_SERIALIZER
=
lambda
x
:
'Renderer B:
%
s'
%
x
RENDERER_B_SERIALIZER
=
lambda
x
:
'Renderer B:
%
s'
%
x
class
MockView
(
ResponseMixin
,
View
):
class
MockView
(
ResponseMixin
,
Django
View
):
def
get
(
self
,
request
):
def
get
(
self
,
request
):
response
=
Response
(
DUMMYSTATUS
,
DUMMYCONTENT
)
response
=
Response
(
DUMMYSTATUS
,
DUMMYCONTENT
)
return
self
.
render
(
response
)
return
self
.
render
(
response
)
...
...
djangorestframework/tests/reverse.py
View file @
370274f5
...
@@ -3,10 +3,10 @@ from django.core.urlresolvers import reverse
...
@@ -3,10 +3,10 @@ from django.core.urlresolvers import reverse
from
django.test
import
TestCase
from
django.test
import
TestCase
from
django.utils
import
simplejson
as
json
from
django.utils
import
simplejson
as
json
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
class
MockView
(
Base
View
):
class
MockView
(
View
):
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified"""
"""Mock resource which simply returns a URL, so that we can ensure that reversed URLs are fully qualified"""
permissions
=
()
permissions
=
()
...
...
djangorestframework/tests/throttling.py
View file @
370274f5
...
@@ -3,11 +3,11 @@ from django.test import TestCase
...
@@ -3,11 +3,11 @@ from django.test import TestCase
from
django.utils
import
simplejson
as
json
from
django.utils
import
simplejson
as
json
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.permissions
import
PerUserThrottling
from
djangorestframework.permissions
import
PerUserThrottling
class
MockView
(
Base
View
):
class
MockView
(
View
):
permissions
=
(
PerUserThrottling
,
)
permissions
=
(
PerUserThrottling
,
)
throttle
=
(
3
,
1
)
# 3 requests per second
throttle
=
(
3
,
1
)
# 3 requests per second
...
...
djangorestframework/tests/validators.py
View file @
370274f5
...
@@ -4,7 +4,7 @@ from django.test import TestCase
...
@@ -4,7 +4,7 @@ from django.test import TestCase
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.compat
import
RequestFactory
from
djangorestframework.resources
import
Resource
,
FormResource
,
ModelResource
from
djangorestframework.resources
import
Resource
,
FormResource
,
ModelResource
from
djangorestframework.response
import
ErrorResponse
from
djangorestframework.response
import
ErrorResponse
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.resources
import
Resource
from
djangorestframework.resources
import
Resource
...
@@ -18,7 +18,7 @@ class TestDisabledValidations(TestCase):
...
@@ -18,7 +18,7 @@ class TestDisabledValidations(TestCase):
class
DisabledFormResource
(
FormResource
):
class
DisabledFormResource
(
FormResource
):
form
=
None
form
=
None
class
MockView
(
Base
View
):
class
MockView
(
View
):
resource
=
DisabledFormResource
resource
=
DisabledFormResource
view
=
MockView
()
view
=
MockView
()
...
@@ -31,7 +31,7 @@ class TestDisabledValidations(TestCase):
...
@@ -31,7 +31,7 @@ class TestDisabledValidations(TestCase):
class
DisabledFormResource
(
FormResource
):
class
DisabledFormResource
(
FormResource
):
form
=
None
form
=
None
class
MockView
(
Base
View
):
class
MockView
(
View
):
resource
=
DisabledFormResource
resource
=
DisabledFormResource
view
=
MockView
()
view
=
MockView
()
...
@@ -43,7 +43,7 @@ class TestDisabledValidations(TestCase):
...
@@ -43,7 +43,7 @@ class TestDisabledValidations(TestCase):
"""If the view's form is None and does not have a Resource with a model set then
"""If the view's form is None and does not have a Resource with a model set then
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
ModelFormValidator(view).validate_request(content, None) should just return the content unmodified."""
class
DisabledModelFormView
(
Base
View
):
class
DisabledModelFormView
(
View
):
resource
=
ModelResource
resource
=
ModelResource
view
=
DisabledModelFormView
()
view
=
DisabledModelFormView
()
...
@@ -52,7 +52,7 @@ class TestDisabledValidations(TestCase):
...
@@ -52,7 +52,7 @@ class TestDisabledValidations(TestCase):
def
test_disabled_model_form_validator_get_bound_form_returns_none
(
self
):
def
test_disabled_model_form_validator_get_bound_form_returns_none
(
self
):
"""If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None."""
"""If the form attribute is None on FormValidatorMixin then get_bound_form(content) should just return None."""
class
DisabledModelFormView
(
Base
View
):
class
DisabledModelFormView
(
View
):
resource
=
ModelResource
resource
=
ModelResource
view
=
DisabledModelFormView
()
view
=
DisabledModelFormView
()
...
@@ -77,7 +77,7 @@ class TestNonFieldErrors(TestCase):
...
@@ -77,7 +77,7 @@ class TestNonFieldErrors(TestCase):
class
MockResource
(
FormResource
):
class
MockResource
(
FormResource
):
form
=
MockForm
form
=
MockForm
class
MockView
(
Base
View
):
class
MockView
(
View
):
pass
pass
view
=
MockView
()
view
=
MockView
()
...
@@ -104,10 +104,10 @@ class TestFormValidation(TestCase):
...
@@ -104,10 +104,10 @@ class TestFormValidation(TestCase):
class
MockModelResource
(
ModelResource
):
class
MockModelResource
(
ModelResource
):
form
=
MockForm
form
=
MockForm
class
MockFormView
(
Base
View
):
class
MockFormView
(
View
):
resource
=
MockFormResource
resource
=
MockFormResource
class
MockModelFormView
(
Base
View
):
class
MockModelFormView
(
View
):
resource
=
MockModelResource
resource
=
MockModelResource
self
.
MockFormResource
=
MockFormResource
self
.
MockFormResource
=
MockFormResource
...
@@ -277,7 +277,7 @@ class TestModelFormValidator(TestCase):
...
@@ -277,7 +277,7 @@ class TestModelFormValidator(TestCase):
class
MockResource
(
ModelResource
):
class
MockResource
(
ModelResource
):
model
=
MockModel
model
=
MockModel
class
MockView
(
Base
View
):
class
MockView
(
View
):
resource
=
MockResource
resource
=
MockResource
self
.
validator
=
MockResource
(
MockView
)
self
.
validator
=
MockResource
(
MockView
)
...
...
djangorestframework/views.py
View file @
370274f5
...
@@ -6,16 +6,17 @@ By setting or modifying class attributes on your view, you change it's predefine
...
@@ -6,16 +6,17 @@ By setting or modifying class attributes on your view, you change it's predefine
"""
"""
from
django.core.urlresolvers
import
set_script_prefix
from
django.core.urlresolvers
import
set_script_prefix
from
django.http
import
HttpResponse
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.csrf
import
csrf_exempt
from
djangorestframework.compat
import
View
from
djangorestframework.compat
import
View
as
DjangoView
from
djangorestframework.response
import
Response
,
ErrorResponse
from
djangorestframework.response
import
Response
,
ErrorResponse
from
djangorestframework.mixins
import
*
from
djangorestframework.mixins
import
*
from
djangorestframework
import
resources
,
renderers
,
parsers
,
authentication
,
permissions
,
status
from
djangorestframework
import
resources
,
renderers
,
parsers
,
authentication
,
permissions
,
status
__all__
=
(
__all__
=
(
'
Base
View'
,
'View'
,
'ModelView'
,
'ModelView'
,
'InstanceModelView'
,
'InstanceModelView'
,
'ListModelView'
,
'ListModelView'
,
...
@@ -24,7 +25,7 @@ __all__ = (
...
@@ -24,7 +25,7 @@ __all__ = (
class
BaseView
(
ResourceMixin
,
RequestMixin
,
ResponseMixin
,
AuthMixin
,
View
):
class
View
(
ResourceMixin
,
RequestMixin
,
ResponseMixin
,
AuthMixin
,
Django
View
):
"""
"""
Handles incoming requests and maps them to REST operations.
Handles incoming requests and maps them to REST operations.
Performs request deserialization, response serialization, authentication and input validation.
Performs request deserialization, response serialization, authentication and input validation.
...
@@ -65,7 +66,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -65,7 +66,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
as an attribute on the callable function. This allows us to discover
as an attribute on the callable function. This allows us to discover
information about the view when we do URL reverse lookups.
information about the view when we do URL reverse lookups.
"""
"""
view
=
super
(
Base
View
,
cls
)
.
as_view
(
**
initkwargs
)
view
=
super
(
View
,
cls
)
.
as_view
(
**
initkwargs
)
view
.
cls_instance
=
cls
(
**
initkwargs
)
view
.
cls_instance
=
cls
(
**
initkwargs
)
return
view
return
view
...
@@ -86,6 +87,14 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -86,6 +87,14 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
{
'detail'
:
'Method
\'
%
s
\'
not allowed on this resource.'
%
self
.
method
})
{
'detail'
:
'Method
\'
%
s
\'
not allowed on this resource.'
%
self
.
method
})
def
initial
(
self
,
request
,
*
args
,
**
kargs
):
"""
Hook for any code that needs to run prior to anything else.
Required if you want to do things like set `request.upload_handlers` before
the authentication and dispatch handling is run.
"""
pass
# Note: session based authentication is explicitly CSRF validated,
# Note: session based authentication is explicitly CSRF validated,
# all other authentication is CSRF exempt.
# all other authentication is CSRF exempt.
@csrf_exempt
@csrf_exempt
...
@@ -99,6 +108,8 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -99,6 +108,8 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
set_script_prefix
(
prefix
)
set_script_prefix
(
prefix
)
try
:
try
:
self
.
initial
(
request
,
*
args
,
**
kwargs
)
# Authenticate and check request has the relevant permissions
# Authenticate and check request has the relevant permissions
self
.
_check_permissions
()
self
.
_check_permissions
()
...
@@ -110,8 +121,10 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -110,8 +121,10 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
response_obj
=
handler
(
request
,
*
args
,
**
kwargs
)
response_obj
=
handler
(
request
,
*
args
,
**
kwargs
)
# Allow return value to be either Response, or an object, or None
# Allow return value to be either HttpResponse, Response, or an object, or None
if
isinstance
(
response_obj
,
Response
):
if
isinstance
(
response_obj
,
HttpResponse
):
return
response_obj
elif
isinstance
(
response_obj
,
Response
):
response
=
response_obj
response
=
response_obj
elif
response_obj
is
not
None
:
elif
response_obj
is
not
None
:
response
=
Response
(
status
.
HTTP_200_OK
,
response_obj
)
response
=
Response
(
status
.
HTTP_200_OK
,
response_obj
)
...
@@ -135,7 +148,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -135,7 +148,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
class
ModelView
(
Base
View
):
class
ModelView
(
View
):
"""A RESTful view that maps to a model in the database."""
"""A RESTful view that maps to a model in the database."""
resource
=
resources
.
ModelResource
resource
=
resources
.
ModelResource
...
...
examples/objectstore/views.py
View file @
370274f5
from
django.conf
import
settings
from
django.conf
import
settings
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.response
import
Response
from
djangorestframework.response
import
Response
from
djangorestframework
import
status
from
djangorestframework
import
status
...
@@ -25,7 +25,7 @@ def remove_oldest_files(dir, max_files):
...
@@ -25,7 +25,7 @@ def remove_oldest_files(dir, max_files):
[
os
.
remove
(
path
)
for
path
in
ctime_sorted_paths
[
max_files
:]]
[
os
.
remove
(
path
)
for
path
in
ctime_sorted_paths
[
max_files
:]]
class
ObjectStoreRoot
(
Base
View
):
class
ObjectStoreRoot
(
View
):
"""
"""
Root of the Object Store API.
Root of the Object Store API.
Allows the client to get a complete list of all the stored objects, or to create a new stored object.
Allows the client to get a complete list of all the stored objects, or to create a new stored object.
...
@@ -51,7 +51,7 @@ class ObjectStoreRoot(BaseView):
...
@@ -51,7 +51,7 @@ class ObjectStoreRoot(BaseView):
return
Response
(
status
.
HTTP_201_CREATED
,
self
.
CONTENT
,
{
'Location'
:
reverse
(
'stored-object'
,
kwargs
=
{
'key'
:
key
})})
return
Response
(
status
.
HTTP_201_CREATED
,
self
.
CONTENT
,
{
'Location'
:
reverse
(
'stored-object'
,
kwargs
=
{
'key'
:
key
})})
class
StoredObject
(
Base
View
):
class
StoredObject
(
View
):
"""
"""
Represents a stored object.
Represents a stored object.
The object may be any picklable content.
The object may be any picklable content.
...
...
examples/pygments_api/views.py
View file @
370274f5
...
@@ -5,7 +5,7 @@ from django.core.urlresolvers import reverse
...
@@ -5,7 +5,7 @@ from django.core.urlresolvers import reverse
from
djangorestframework.resources
import
FormResource
from
djangorestframework.resources
import
FormResource
from
djangorestframework.response
import
Response
from
djangorestframework.response
import
Response
from
djangorestframework.renderers
import
BaseRenderer
from
djangorestframework.renderers
import
BaseRenderer
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework
import
status
from
djangorestframework
import
status
from
pygments.formatters
import
HtmlFormatter
from
pygments.formatters
import
HtmlFormatter
...
@@ -53,7 +53,7 @@ class PygmentsFormResource(FormResource):
...
@@ -53,7 +53,7 @@ class PygmentsFormResource(FormResource):
form
=
PygmentsForm
form
=
PygmentsForm
class
PygmentsRoot
(
Base
View
):
class
PygmentsRoot
(
View
):
"""
"""
This example demonstrates a simple RESTful Web API aound the awesome pygments library.
This example demonstrates a simple RESTful Web API aound the awesome pygments library.
This top level resource is used to create highlighted code snippets, and to list all the existing code snippets.
This top level resource is used to create highlighted code snippets, and to list all the existing code snippets.
...
@@ -88,7 +88,7 @@ class PygmentsRoot(BaseView):
...
@@ -88,7 +88,7 @@ class PygmentsRoot(BaseView):
return
Response
(
status
.
HTTP_201_CREATED
,
headers
=
{
'Location'
:
reverse
(
'pygments-instance'
,
args
=
[
unique_id
])})
return
Response
(
status
.
HTTP_201_CREATED
,
headers
=
{
'Location'
:
reverse
(
'pygments-instance'
,
args
=
[
unique_id
])})
class
PygmentsInstance
(
Base
View
):
class
PygmentsInstance
(
View
):
"""
"""
Simply return the stored highlighted HTML file with the correct mime type.
Simply return the stored highlighted HTML file with the correct mime type.
This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class.
This Resource only renders HTML and uses a standard HTML renderer rather than the renderers.DocumentingHTMLRenderer class.
...
...
examples/resourceexample/views.py
View file @
370274f5
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
from
djangorestframework.resources
import
FormResource
from
djangorestframework.resources
import
FormResource
from
djangorestframework.response
import
Response
from
djangorestframework.response
import
Response
from
djangorestframework
import
status
from
djangorestframework
import
status
...
@@ -14,7 +14,7 @@ class MyFormValidation(FormResource):
...
@@ -14,7 +14,7 @@ class MyFormValidation(FormResource):
form
=
MyForm
form
=
MyForm
class
ExampleResource
(
Base
View
):
class
ExampleResource
(
View
):
"""
"""
A basic read-only resource that points to 3 other resources.
A basic read-only resource that points to 3 other resources.
"""
"""
...
@@ -23,7 +23,7 @@ class ExampleResource(BaseView):
...
@@ -23,7 +23,7 @@ class ExampleResource(BaseView):
return
{
"Some other resources"
:
[
reverse
(
'another-example-resource'
,
kwargs
=
{
'num'
:
num
})
for
num
in
range
(
3
)]}
return
{
"Some other resources"
:
[
reverse
(
'another-example-resource'
,
kwargs
=
{
'num'
:
num
})
for
num
in
range
(
3
)]}
class
AnotherExampleResource
(
Base
View
):
class
AnotherExampleResource
(
View
):
"""
"""
A basic GET-able/POST-able resource.
A basic GET-able/POST-able resource.
"""
"""
...
...
examples/sandbox/views.py
View file @
370274f5
"""The root view for the examples provided with Django REST framework"""
"""The root view for the examples provided with Django REST framework"""
from
django.core.urlresolvers
import
reverse
from
django.core.urlresolvers
import
reverse
from
djangorestframework.views
import
Base
View
from
djangorestframework.views
import
View
class
Sandbox
(
Base
View
):
class
Sandbox
(
View
):
"""This is the sandbox for the examples provided with [Django REST framework](http://django-rest-framework.org).
"""This is the sandbox for the examples provided with [Django REST framework](http://django-rest-framework.org).
These examples are provided to help you get a better idea of the some of the features of RESTful APIs created using the framework.
These examples are provided to help you get a better idea of the some of the features of RESTful APIs created using the framework.
...
...
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