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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
66 additions
and
16 deletions
+66
-16
djangorestframework/mixins.py
+56
-9
djangorestframework/resources.py
+0
-0
djangorestframework/tests/resources.py
+1
-1
djangorestframework/utils/mediatypes.py
+1
-0
djangorestframework/views.py
+8
-6
No files found.
djangorestframework/mixins.py
View file @
325e63a3
...
...
@@ -25,6 +25,8 @@ __all__ = (
'ResponseMixin'
,
'AuthMixin'
,
'ResourceMixin'
,
#
'InstanceMixin'
,
# Model behavior mixins
'ReadModelMixin'
,
'CreateModelMixin'
,
...
...
@@ -137,7 +139,7 @@ class RequestMixin(object):
content_length
=
0
# 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
:
return
None
elif
hasattr
(
request
,
'read'
):
...
...
@@ -379,8 +381,8 @@ class AuthMixin(object):
if
not
hasattr
(
self
,
'_user'
):
self
.
_user
=
self
.
_authenticate
()
return
self
.
_user
def
_authenticate
(
self
):
"""
Attempt to authenticate the request using each authentication class in turn.
...
...
@@ -405,26 +407,71 @@ class AuthMixin(object):
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 ##########
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
def
CONTENT
(
self
):
if
not
hasattr
(
self
,
'_content'
):
self
.
_content
=
self
.
_get_content
(
)
self
.
_content
=
self
.
validate_request
(
self
.
DATA
,
self
.
FILES
)
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
)
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
):
resource
=
self
.
resource
(
self
)
return
resource
.
get_bound_form
(
content
)
def
object_to_data
(
self
,
obj
):
resource
=
self
.
resource
(
self
)
return
resource
.
object_to_data
(
obj
)
########## Model Mixins ##########
...
...
djangorestframework/resources.py
View file @
325e63a3
This diff is collapsed.
Click to expand it.
djangorestframework/tests/resources.py
View file @
325e63a3
...
...
@@ -11,7 +11,7 @@ class TestObjectToData(TestCase):
def
test_decimal
(
self
):
"""Decimals need to be converted to a string representation."""
self
.
assertEquals
(
_object_to_data
(
decimal
.
Decimal
(
'1.5'
)),
'1.5'
)
def
test_function
(
self
):
"""Functions with no arguments should be called."""
def
foo
():
...
...
djangorestframework/utils/mediatypes.py
View file @
325e63a3
...
...
@@ -43,6 +43,7 @@ def add_media_type_param(media_type, key, val):
media_type
.
params
[
key
]
=
val
return
str
(
media_type
)
def
get_media_type_params
(
media_type
):
"""
Return a dictionary of the parameters on the given media type.
...
...
djangorestframework/views.py
View file @
325e63a3
...
...
@@ -18,8 +18,10 @@ __all__ = (
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
resource
=
resources
.
Resource
...
...
@@ -77,8 +79,8 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
prefix
=
'
%
s://
%
s'
%
(
request
.
is_secure
()
and
'https'
or
'http'
,
request
.
get_host
())
set_script_prefix
(
prefix
)
try
:
# Authenticate and check request
is
has the relevant permissions
try
:
# Authenticate and check request has the relevant permissions
self
.
_check_permissions
()
# Get the appropriate handler method
...
...
@@ -98,7 +100,7 @@ class BaseView(ResourceMixin, RequestMixin, ResponseMixin, AuthMixin, View):
response
=
Response
(
status
.
HTTP_204_NO_CONTENT
)
# 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
:
response
=
exc
.
response
...
...
@@ -118,7 +120,7 @@ class ModelView(BaseView):
"""A RESTful view that maps to a model in the database."""
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."""
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