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
cc4c9a95
Commit
cc4c9a95
authored
May 10, 2011
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bits of cleanup
parent
144d52c7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
25 deletions
+39
-25
djangorestframework/mixins.py
+23
-15
djangorestframework/renderers.py
+16
-10
No files found.
djangorestframework/mixins.py
View file @
cc4c9a95
...
@@ -4,7 +4,7 @@ from django.contrib.auth.models import AnonymousUser
...
@@ -4,7 +4,7 @@ from django.contrib.auth.models import AnonymousUser
from
django.db.models.query
import
QuerySet
from
django.db.models.query
import
QuerySet
from
django.db.models.fields.related
import
RelatedField
from
django.db.models.fields.related
import
RelatedField
from
django.http
import
HttpResponse
from
django.http
import
HttpResponse
from
django.http.multipartparser
import
LimitBytes
# TODO: Use LimitedStream in compat
from
django.http.multipartparser
import
LimitBytes
from
djangorestframework
import
status
from
djangorestframework
import
status
from
djangorestframework.parsers
import
FormParser
,
MultiPartParser
from
djangorestframework.parsers
import
FormParser
,
MultiPartParser
...
@@ -87,7 +87,8 @@ class RequestMixin(object):
...
@@ -87,7 +87,8 @@ class RequestMixin(object):
content_length
=
int
(
request
.
META
.
get
(
'CONTENT_LENGTH'
,
request
.
META
.
get
(
'HTTP_CONTENT_LENGTH'
)))
content_length
=
int
(
request
.
META
.
get
(
'CONTENT_LENGTH'
,
request
.
META
.
get
(
'HTTP_CONTENT_LENGTH'
)))
except
(
ValueError
,
TypeError
):
except
(
ValueError
,
TypeError
):
content_length
=
0
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
# Currently only supports parsing request body as a stream with 1.3
if
content_length
==
0
:
if
content_length
==
0
:
return
None
return
None
...
@@ -116,6 +117,8 @@ class RequestMixin(object):
...
@@ -116,6 +117,8 @@ class RequestMixin(object):
#except (ValueError, TypeError):
#except (ValueError, TypeError):
# content_length = 0
# content_length = 0
# self._stream = LimitedStream(request, content_length)
# self._stream = LimitedStream(request, content_length)
#
# UPDATE: http://code.djangoproject.com/ticket/15785
self
.
_stream
=
request
self
.
_stream
=
request
else
:
else
:
self
.
_stream
=
StringIO
(
request
.
raw_post_data
)
self
.
_stream
=
StringIO
(
request
.
raw_post_data
)
...
@@ -227,7 +230,7 @@ class RequestMixin(object):
...
@@ -227,7 +230,7 @@ class RequestMixin(object):
@property
@property
def
default_parser
(
self
):
def
default_parser
(
self
):
"""Return the view's most preferred renderer.
"""Return the view's most preferred renderer.
(This has no behavio
u
ral effect, but is may be used by documenting renderers)"""
(This has no behavioral effect, but is may be used by documenting renderers)"""
return
self
.
parsers
[
0
]
return
self
.
parsers
[
0
]
...
@@ -244,17 +247,17 @@ class ResponseMixin(object):
...
@@ -244,17 +247,17 @@ class ResponseMixin(object):
"""
"""
Adds behavior for pluggable Renderers to a :class:`.BaseView` or Django :class:`View`. class.
Adds behavior for pluggable Renderers to a :class:`.BaseView` or Django :class:`View`. class.
Default behavio
u
r is to use standard HTTP Accept header content negotiation.
Default behavior is to use standard HTTP Accept header content negotiation.
Also supports over
id
ding the content type by specifying an _accept= parameter in the URL.
Also supports over
ri
ding the content type by specifying an _accept= parameter in the URL.
Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead.
Ignores Accept headers from Internet Explorer user agents and uses a sensible browser Accept header instead.
"""
"""
ACCEPT_QUERY_PARAM
=
'_accept'
# Allow override of Accept header in URL query params
ACCEPT_QUERY_PARAM
=
'_accept'
# Allow override of Accept header in URL query params
REWRITE_IE_ACCEPT_HEADER
=
True
REWRITE_IE_ACCEPT_HEADER
=
True
renderers
=
()
renderers
=
()
# TODO: wrap this behavior around dispatch(), ensuring it works well with
# existing Django classes that use render_to_response.
def
render
(
self
,
response
):
def
render
(
self
,
response
):
"""
"""
Takes a ``Response`` object and returns an ``HttpResponse``.
Takes a ``Response`` object and returns an ``HttpResponse``.
...
@@ -284,10 +287,12 @@ class ResponseMixin(object):
...
@@ -284,10 +287,12 @@ class ResponseMixin(object):
def
_determine_renderer
(
self
,
request
):
def
_determine_renderer
(
self
,
request
):
"""Return the appropriate renderer for the output, given the client's 'Accept' header,
"""
Return the appropriate renderer for the output, given the client's 'Accept' header,
and the content types that this mixin knows how to serve.
and the content types that this mixin knows how to serve.
See: RFC 2616, Section 14 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html"""
See: RFC 2616, Section 14 - http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
"""
if
self
.
ACCEPT_QUERY_PARAM
and
request
.
GET
.
get
(
self
.
ACCEPT_QUERY_PARAM
,
None
):
if
self
.
ACCEPT_QUERY_PARAM
and
request
.
GET
.
get
(
self
.
ACCEPT_QUERY_PARAM
,
None
):
# Use _accept parameter override
# Use _accept parameter override
...
@@ -351,13 +356,17 @@ class ResponseMixin(object):
...
@@ -351,13 +356,17 @@ class ResponseMixin(object):
@property
@property
def
renderted_media_types
(
self
):
def
renderted_media_types
(
self
):
"""Return an list of all the media types that this resource can render."""
"""
Return an list of all the media types that this resource can render.
"""
return
[
renderer
.
media_type
for
renderer
in
self
.
renderers
]
return
[
renderer
.
media_type
for
renderer
in
self
.
renderers
]
@property
@property
def
default_renderer
(
self
):
def
default_renderer
(
self
):
"""Return the resource's most preferred renderer.
"""
(This renderer is used if the client does not send and Accept: header, or sends Accept: */*)"""
Return the resource's most preferred renderer.
(This renderer is used if the client does not send and Accept: header, or sends Accept: */*)
"""
return
self
.
renderers
[
0
]
return
self
.
renderers
[
0
]
...
@@ -367,8 +376,6 @@ class AuthMixin(object):
...
@@ -367,8 +376,6 @@ class AuthMixin(object):
"""
"""
Simple mixin class to provide authentication and permission checking,
Simple mixin class to provide authentication and permission checking,
by adding a set of authentication and permission classes on a ``View``.
by adding a set of authentication and permission classes on a ``View``.
TODO: wrap this behavior around dispatch()
"""
"""
authentication
=
()
authentication
=
()
permissions
=
()
permissions
=
()
...
@@ -391,6 +398,7 @@ class AuthMixin(object):
...
@@ -391,6 +398,7 @@ class AuthMixin(object):
return
user
return
user
return
AnonymousUser
()
return
AnonymousUser
()
# TODO: wrap this behavior around dispatch()
def
_check_permissions
(
self
):
def
_check_permissions
(
self
):
"""
"""
Check user permissions and either raise an ``ErrorResponse`` or return.
Check user permissions and either raise an ``ErrorResponse`` or return.
...
...
djangorestframework/renderers.py
View file @
cc4c9a95
"""
"""
Renderers are used to serialize a View's output into specific media types.
Renderers are used to serialize a View's output into specific media types.
django-rest-framework also provides HTML and PlainText renderers that help self-document the API,
by serializing the output along with documentation regarding the Resource, output status and headers,
Django REST framework also provides HTML and PlainText renderers that help self-document the API,
and providing forms and links depending on the allowed methods, renderers and parsers on the Resource.
by serializing the output along with documentation regarding the View, output status and headers,
and providing forms and links depending on the allowed methods, renderers and parsers on the View.
"""
"""
from
django
import
forms
from
django
import
forms
from
django.conf
import
settings
from
django.conf
import
settings
...
@@ -33,8 +34,8 @@ __all__ = (
...
@@ -33,8 +34,8 @@ __all__ = (
class
BaseRenderer
(
object
):
class
BaseRenderer
(
object
):
"""
"""
All renderers must extend this class, set the media_type attribute,
and
All renderers must extend this class, set the media_type attribute,
override the render() function.
and
override the render() function.
"""
"""
media_type
=
None
media_type
=
None
...
@@ -43,13 +44,20 @@ class BaseRenderer(object):
...
@@ -43,13 +44,20 @@ class BaseRenderer(object):
def
render
(
self
,
obj
=
None
,
media_type
=
None
):
def
render
(
self
,
obj
=
None
,
media_type
=
None
):
"""
"""
Given an object render it into a string.
The requested media type is also passed to this method,
as it may contain parameters relevant to how the parser
should render the output.
EG: 'application/json; indent=4'
By default render simply returns the ouput as-is.
By default render simply returns the ouput as-is.
Override this method to provide for other behavior.
Override this method to provide for other behavior.
"""
"""
if
obj
is
None
:
if
obj
is
None
:
return
''
return
''
return
obj
return
str
(
obj
)
class
TemplateRenderer
(
BaseRenderer
):
class
TemplateRenderer
(
BaseRenderer
):
...
@@ -101,10 +109,8 @@ class DocumentingTemplateRenderer(BaseRenderer):
...
@@ -101,10 +109,8 @@ class DocumentingTemplateRenderer(BaseRenderer):
"""Get a form, possibly bound to either the input or output data.
"""Get a form, possibly bound to either the input or output data.
In the absence on of the Resource having an associated form then
In the absence on of the Resource having an associated form then
provide a form that can be used to submit arbitrary content."""
provide a form that can be used to submit arbitrary content."""
# Get the form instance if we have one bound to the input
#form_instance = resource.form_instance
# TODO! Reinstate this
# Get the form instance if we have one bound to the input
form_instance
=
getattr
(
view
,
'bound_form_instance'
,
None
)
form_instance
=
getattr
(
view
,
'bound_form_instance'
,
None
)
if
not
form_instance
and
hasattr
(
view
,
'get_bound_form'
):
if
not
form_instance
and
hasattr
(
view
,
'get_bound_form'
):
...
@@ -183,7 +189,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
...
@@ -183,7 +189,7 @@ class DocumentingTemplateRenderer(BaseRenderer):
if
apply_markdown
:
if
apply_markdown
:
try
:
try
:
markeddown
=
apply_markdown
(
description
)
markeddown
=
apply_markdown
(
description
)
except
AttributeError
:
# TODO: possibly split the get_description / get_name into a mixin class
except
AttributeError
:
markeddown
=
None
markeddown
=
None
breadcrumb_list
=
get_breadcrumbs
(
self
.
view
.
request
.
path
)
breadcrumb_list
=
get_breadcrumbs
(
self
.
view
.
request
.
path
)
...
...
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