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
2d8e4e05
Commit
2d8e4e05
authored
Mar 10, 2011
by
sebpiq
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
documentation
parent
26e10d0e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
11 deletions
+24
-11
djangorestframework/content.py
+5
-2
djangorestframework/parsers.py
+19
-9
No files found.
djangorestframework/content.py
View file @
2d8e4e05
...
@@ -28,10 +28,12 @@ class StandardContentMixin(ContentMixin):
...
@@ -28,10 +28,12 @@ class StandardContentMixin(ContentMixin):
class
OverloadedContentMixin
(
ContentMixin
):
class
OverloadedContentMixin
(
ContentMixin
):
"""HTTP request content behaviour that also allows arbitrary content to be tunneled in form data."""
"""HTTP request content behaviour that also allows arbitrary content to be tunneled in form data."""
"""The name to use for the content override field in the POST form. Set this to *None* to desactivate content overloading."""
"""The name to use for the content override field in the POST form.
Set this to *None* to desactivate content overloading."""
CONTENT_PARAM
=
'_content'
CONTENT_PARAM
=
'_content'
"""The name to use for the content-type override field in the POST form. Taken into account only if content overloading is activated."""
"""The name to use for the content-type override field in the POST form.
Taken into account only if content overloading is activated."""
CONTENTTYPE_PARAM
=
'_contenttype'
CONTENTTYPE_PARAM
=
'_contenttype'
def
determine_content
(
self
,
request
):
def
determine_content
(
self
,
request
):
...
@@ -48,6 +50,7 @@ class OverloadedContentMixin(ContentMixin):
...
@@ -48,6 +50,7 @@ class OverloadedContentMixin(ContentMixin):
content_type
=
None
content_type
=
None
if
self
.
CONTENTTYPE_PARAM
and
request
.
POST
.
get
(
self
.
CONTENTTYPE_PARAM
,
None
):
if
self
.
CONTENTTYPE_PARAM
and
request
.
POST
.
get
(
self
.
CONTENTTYPE_PARAM
,
None
):
content_type
=
request
.
POST
.
get
(
self
.
CONTENTTYPE_PARAM
,
None
)
content_type
=
request
.
POST
.
get
(
self
.
CONTENTTYPE_PARAM
,
None
)
request
.
META
[
'CONTENT_TYPE'
]
=
content_type
return
(
content_type
,
request
.
POST
[
self
.
CONTENT_PARAM
])
return
(
content_type
,
request
.
POST
[
self
.
CONTENT_PARAM
])
else
:
else
:
...
...
djangorestframework/parsers.py
View file @
2d8e4e05
...
@@ -79,11 +79,11 @@ class XMLParser(BaseParser):
...
@@ -79,11 +79,11 @@ class XMLParser(BaseParser):
media_type
=
'application/xml'
media_type
=
'application/xml'
class
DataFlatener
(
object
):
class
DataFlatener
(
object
):
#TODO : document + test
"""Utility object for flatening dictionaries of lists. Useful for "urlencoded" decoded data."""
def
flatten_data
(
self
,
data
):
def
flatten_data
(
self
,
data
):
"""Given a data dictionary
``{<key>: <value_list>}``, returns a flattened dictionary according to :meth:`FormParser.is_a_list`.
"""Given a data dictionary
{<key>: <value_list>}, returns a flattened dictionary
"""
with information provided by the method "is_a_list".
"""
flatdata
=
dict
()
flatdata
=
dict
()
for
key
,
attr_value
in
data
.
items
():
for
key
,
attr_value
in
data
.
items
():
if
self
.
is_a_list
(
key
):
if
self
.
is_a_list
(
key
):
...
@@ -98,20 +98,27 @@ class DataFlatener(object):
...
@@ -98,20 +98,27 @@ class DataFlatener(object):
flatdata
[
key
]
=
attr_value
flatdata
[
key
]
=
attr_value
return
flatdata
return
flatdata
def
is_a_list
(
self
,
key
):
def
is_a_list
(
self
,
key
,
val
):
""" """
"""Returns True if the parameter with name *key* is expected to be a list, or False otherwise.
*val* which is the received value for parameter *key* can be used to guess the answer."""
return
False
return
False
class
FormParser
(
BaseParser
,
DataFlatener
):
class
FormParser
(
BaseParser
,
DataFlatener
):
"""The default parser for form data.
"""The default parser for form data.
Return a dict containing a single value for each non-reserved parameter.
Return a dict containing a single value for each non-reserved parameter.
In order to handle select multiple (and having possibly more than a single value for each parameter),
you can customize the output by subclassing the method 'is_a_list'.
"""
"""
# TODO: document flatening
# TODO: writing tests for PUT files + normal data
# TODO: writing tests for PUT files + normal data
# TODO: document EMPTY workaround
media_type
=
'application/x-www-form-urlencoded'
media_type
=
'application/x-www-form-urlencoded'
EMPTY_VALUE
=
'EMPTY'
"""The value of the parameter when the select multiple is empty.
Browsers are usually stripping the select multiple that have no option selected from the parameters sent.
A common hack to avoid this is to send the parameter with a value specifying that the list is empty.
This value will always be stripped before the data is returned."""
EMPTY_VALUE
=
'_empty'
def
parse
(
self
,
input
):
def
parse
(
self
,
input
):
data
=
parse_qs
(
input
)
data
=
parse_qs
(
input
)
...
@@ -142,8 +149,11 @@ class MultipartParser(BaseParser, DataFlatener):
...
@@ -142,8 +149,11 @@ class MultipartParser(BaseParser, DataFlatener):
media_type
=
'multipart/form-data'
media_type
=
'multipart/form-data'
def
parse
(
self
,
input
):
def
parse
(
self
,
input
):
request
=
self
.
resource
.
request
request
=
self
.
resource
.
request
#TODO : that's pretty dumb : files are loaded with
#upload_handlers, but as we read the request body completely (input),
#then it kind of misses the point. Why not input as a stream ?
upload_handlers
=
request
.
_get_upload_handlers
()
upload_handlers
=
request
.
_get_upload_handlers
()
django_mpp
=
DjangoMPParser
(
request
.
META
,
StringIO
(
input
),
upload_handlers
)
django_mpp
=
DjangoMPParser
(
request
.
META
,
StringIO
(
input
),
upload_handlers
)
data
,
files
=
django_mpp
.
parse
()
data
,
files
=
django_mpp
.
parse
()
...
...
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