mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 16:24:18 +03:00
Merge flashingpumpkin's fix
This commit is contained in:
commit
d1beedadbd
|
@ -11,12 +11,11 @@ We need a method to be able to:
|
||||||
and multipart/form-data. (eg also handle multipart/json)
|
and multipart/form-data. (eg also handle multipart/json)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from django.http import QueryDict
|
||||||
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
||||||
from django.utils import simplejson as json
|
from django.utils import simplejson as json
|
||||||
from djangorestframework import status
|
from djangorestframework import status
|
||||||
from djangorestframework.compat import parse_qs
|
|
||||||
from djangorestframework.response import ErrorResponse
|
from djangorestframework.response import ErrorResponse
|
||||||
from djangorestframework.utils import as_tuple
|
|
||||||
from djangorestframework.utils.mediatypes import media_type_matches
|
from djangorestframework.utils.mediatypes import media_type_matches
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
@ -117,7 +116,7 @@ class FormParser(BaseParser):
|
||||||
`data` will be a :class:`QueryDict` containing all the form parameters.
|
`data` will be a :class:`QueryDict` containing all the form parameters.
|
||||||
`files` will always be :const:`None`.
|
`files` will always be :const:`None`.
|
||||||
"""
|
"""
|
||||||
data = parse_qs(stream.read(), keep_blank_values=True)
|
data = QueryDict(stream.read())
|
||||||
return (data, None)
|
return (data, None)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -241,14 +241,12 @@ class FormResource(Resource):
|
||||||
# In addition to regular validation we also ensure no additional fields are being passed in...
|
# In addition to regular validation we also ensure no additional fields are being passed in...
|
||||||
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
||||||
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
|
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
|
||||||
|
|
||||||
# Check using both regular validation, and our stricter no additional fields rule
|
# Check using both regular validation, and our stricter no additional fields rule
|
||||||
if bound_form.is_valid() and not unknown_fields:
|
if bound_form.is_valid() and not unknown_fields:
|
||||||
# Validation succeeded...
|
# Validation succeeded...
|
||||||
cleaned_data = bound_form.cleaned_data
|
cleaned_data = bound_form.cleaned_data
|
||||||
|
|
||||||
cleaned_data.update(bound_form.files)
|
|
||||||
|
|
||||||
# Add in any extra fields to the cleaned content...
|
# Add in any extra fields to the cleaned content...
|
||||||
for key in (allowed_extra_fields_set & seen_fields_set) - set(cleaned_data.keys()):
|
for key in (allowed_extra_fields_set & seen_fields_set) - set(cleaned_data.keys()):
|
||||||
cleaned_data[key] = data[key]
|
cleaned_data[key] = data[key]
|
||||||
|
|
|
@ -131,3 +131,30 @@
|
||||||
# self.assertEqual(data['key1'], 'val1')
|
# self.assertEqual(data['key1'], 'val1')
|
||||||
# self.assertEqual(files['file1'].read(), 'blablabla')
|
# self.assertEqual(files['file1'].read(), 'blablabla')
|
||||||
|
|
||||||
|
from StringIO import StringIO
|
||||||
|
from cgi import parse_qs
|
||||||
|
from django import forms
|
||||||
|
from django.test import TestCase
|
||||||
|
from djangorestframework.parsers import FormParser
|
||||||
|
|
||||||
|
class Form(forms.Form):
|
||||||
|
field1 = forms.CharField(max_length=3)
|
||||||
|
field2 = forms.CharField()
|
||||||
|
|
||||||
|
class TestFormParser(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.string = "field1=abc&field2=defghijk"
|
||||||
|
|
||||||
|
def test_fail(self):
|
||||||
|
""" Demonstrate that `parse_qs` fails on forms """
|
||||||
|
data = parse_qs(self.string, keep_blank_values=True)
|
||||||
|
self.assertEqual(Form(data).is_valid(), False)
|
||||||
|
|
||||||
|
def test_parse(self):
|
||||||
|
""" Make sure the `QueryDict` works OK """
|
||||||
|
parser = FormParser(None)
|
||||||
|
|
||||||
|
stream = StringIO(self.string)
|
||||||
|
(data, files) = parser.parse(stream)
|
||||||
|
|
||||||
|
self.assertEqual(Form(data).is_valid(), True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user