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
325e63a3
Commit
325e63a3
authored
May 13, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sorting out resources. Doing some crazy magic automatic url resolving stuff. Yum.
parent
8f6bcac7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
62 additions
and
12 deletions
+62
-12
djangorestframework/mixins.py
+54
-7
djangorestframework/resources.py
+0
-0
djangorestframework/tests/resources.py
+0
-0
djangorestframework/utils/mediatypes.py
+1
-0
djangorestframework/views.py
+7
-5
No files found.
djangorestframework/mixins.py
View file @
325e63a3
...
@@ -25,6 +25,8 @@ __all__ = (
...
@@ -25,6 +25,8 @@ __all__ = (
'ResponseMixin'
,
'ResponseMixin'
,
'AuthMixin'
,
'AuthMixin'
,
'ResourceMixin'
,
'ResourceMixin'
,
#
'InstanceMixin'
,
# Model behavior mixins
# Model behavior mixins
'ReadModelMixin'
,
'ReadModelMixin'
,
'CreateModelMixin'
,
'CreateModelMixin'
,
...
@@ -137,7 +139,7 @@ class RequestMixin(object):
...
@@ -137,7 +139,7 @@ class RequestMixin(object):
content_length
=
0
content_length
=
0
# TODO: Add 1.3's LimitedStream to compat and use that.
# TODO: Add 1.3's LimitedStream to compat and use that.
# Currently only supports parsing request body as a stream with 1.3
#
NOTE:
Currently only supports parsing request body as a stream with 1.3
if
content_length
==
0
:
if
content_length
==
0
:
return
None
return
None
elif
hasattr
(
request
,
'read'
):
elif
hasattr
(
request
,
'read'
):
...
@@ -405,26 +407,71 @@ class AuthMixin(object):
...
@@ -405,26 +407,71 @@ class AuthMixin(object):
permission
.
check_permission
(
user
)
permission
.
check_permission
(
user
)
##########
class
InstanceMixin
(
object
):
"""
Mixin class that is used to identify a view class as being the canonical identifier
for the resources it is mapped too.
"""
@classmethod
def
as_view
(
cls
,
**
initkwargs
):
"""
Store the callable object on the resource class that has been associated with this view.
"""
view
=
super
(
InstanceMixin
,
cls
)
.
as_view
(
**
initkwargs
)
if
'resource'
in
initkwargs
:
# We do a little dance when we store the view callable...
# we need to store it wrapped in a 1-tuple, so that inspect will treat it
# as a function when we later look it up (rather than turning it into a method).
# This makes sure our URL reversing works ok.
initkwargs
[
'resource'
]
.
view_callable
=
(
view
,)
return
view
########## Resource Mixin ##########
########## Resource Mixin ##########
class
ResourceMixin
(
object
):
class
ResourceMixin
(
object
):
"""
Provides request validation and response filtering behavior.
"""
"""
Should be a class as described in the ``resources`` module.
The ``resource`` is an object that maps a view onto it's representation on the server.
It provides validation on the content of incoming requests,
and filters the object representation into a serializable object for the response.
"""
resource
=
None
@property
@property
def
CONTENT
(
self
):
def
CONTENT
(
self
):
if
not
hasattr
(
self
,
'_content'
):
if
not
hasattr
(
self
,
'_content'
):
self
.
_content
=
self
.
_get_content
(
)
self
.
_content
=
self
.
validate_request
(
self
.
DATA
,
self
.
FILES
)
return
self
.
_content
return
self
.
_content
def
_get_content
(
self
):
def
validate_request
(
self
,
data
,
files
):
"""
Given the request data return the cleaned, validated content.
Typically raises a ErrorResponse with status code 400 (Bad Request) on failure.
"""
resource
=
self
.
resource
(
self
)
resource
=
self
.
resource
(
self
)
return
resource
.
validate
(
self
.
DATA
,
self
.
FILES
)
return
resource
.
validate_request
(
data
,
files
)
def
filter_response
(
self
,
obj
):
"""
Given the response content, filter it into a serializable object.
"""
resource
=
self
.
resource
(
self
)
return
resource
.
filter_response
(
obj
)
def
get_bound_form
(
self
,
content
=
None
):
def
get_bound_form
(
self
,
content
=
None
):
resource
=
self
.
resource
(
self
)
resource
=
self
.
resource
(
self
)
return
resource
.
get_bound_form
(
content
)
return
resource
.
get_bound_form
(
content
)
def
object_to_data
(
self
,
obj
):
resource
=
self
.
resource
(
self
)
return
resource
.
object_to_data
(
obj
)
########## Model Mixins ##########
########## Model Mixins ##########
...
...
djangorestframework/resources.py
View file @
325e63a3
This diff is collapsed.
Click to expand it.
djangorestframework/tests/resources.py
View file @
325e63a3
djangorestframework/utils/mediatypes.py
View file @
325e63a3
...
@@ -43,6 +43,7 @@ def add_media_type_param(media_type, key, val):
...
@@ -43,6 +43,7 @@ def add_media_type_param(media_type, key, val):
media_type
.
params
[
key
]
=
val
media_type
.
params
[
key
]
=
val
return
str
(
media_type
)
return
str
(
media_type
)
def
get_media_type_params
(
media_type
):
def
get_media_type_params
(
media_type
):
"""
"""
Return a dictionary of the parameters on the given media type.
Return a dictionary of the parameters on the given media type.
...
...
djangorestframework/views.py
View file @
325e63a3
...
@@ -18,8 +18,10 @@ __all__ = (
...
@@ -18,8 +18,10 @@ __all__ = (
class
BaseView
(
ResourceMixin
,
RequestMixin
,
ResponseMixin
,
AuthMixin
,
View
):
class
BaseView
(
ResourceMixin
,
RequestMixin
,
ResponseMixin
,
AuthMixin
,
View
):
"""Handles incoming requests and maps them to REST operations.
"""
Performs request deserialization, response serialization, authentication and input validation."""
Handles incoming requests and maps them to REST operations.
Performs request deserialization, response serialization, authentication and input validation.
"""
# Use the base resource by default
# Use the base resource by default
resource
=
resources
.
Resource
resource
=
resources
.
Resource
...
@@ -78,7 +80,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -78,7 +80,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
set_script_prefix
(
prefix
)
set_script_prefix
(
prefix
)
try
:
try
:
# Authenticate and check request
is
has the relevant permissions
# Authenticate and check request has the relevant permissions
self
.
_check_permissions
()
self
.
_check_permissions
()
# Get the appropriate handler method
# Get the appropriate handler method
...
@@ -98,7 +100,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
...
@@ -98,7 +100,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
response
=
Response
(
status
.
HTTP_204_NO_CONTENT
)
response
=
Response
(
status
.
HTTP_204_NO_CONTENT
)
# Pre-serialize filtering (eg filter complex objects into natively serializable types)
# Pre-serialize filtering (eg filter complex objects into natively serializable types)
response
.
cleaned_content
=
self
.
object_to_data
(
response
.
raw_content
)
response
.
cleaned_content
=
self
.
filter_response
(
response
.
raw_content
)
except
ErrorResponse
,
exc
:
except
ErrorResponse
,
exc
:
response
=
exc
.
response
response
=
exc
.
response
...
@@ -118,7 +120,7 @@ class ModelView(BaseView):
...
@@ -118,7 +120,7 @@ class ModelView(BaseView):
"""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
class
InstanceModelView
(
ReadModelMixin
,
UpdateModelMixin
,
DeleteModelMixin
,
ModelView
):
class
InstanceModelView
(
InstanceMixin
,
ReadModelMixin
,
UpdateModelMixin
,
DeleteModelMixin
,
ModelView
):
"""A view which provides default operations for read/update/delete against a model instance."""
"""A view which provides default operations for read/update/delete against a model instance."""
pass
pass
...
...
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