mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-11 04:07:39 +03:00
Merge 06177b8d4b
into 88a3e55ed4
This commit is contained in:
commit
59f211d2e6
|
@ -11,12 +11,11 @@ We need a method to be able to:
|
|||
and multipart/form-data. (eg also handle multipart/json)
|
||||
"""
|
||||
|
||||
from django.http import QueryDict
|
||||
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
|
||||
from django.utils import simplejson as json
|
||||
from djangorestframework import status
|
||||
from djangorestframework.compat import parse_qs
|
||||
from djangorestframework.response import ErrorResponse
|
||||
from djangorestframework.utils import as_tuple
|
||||
from djangorestframework.utils.mediatypes import media_type_matches
|
||||
|
||||
__all__ = (
|
||||
|
@ -117,7 +116,7 @@ class FormParser(BaseParser):
|
|||
`data` will be a :class:`QueryDict` containing all the form parameters.
|
||||
`files` will always be :const:`None`.
|
||||
"""
|
||||
data = parse_qs(stream.read(), keep_blank_values=True)
|
||||
data = QueryDict(stream.read())
|
||||
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...
|
||||
unknown_fields = seen_fields_set - (form_fields_set | allowed_extra_fields_set)
|
||||
unknown_fields = unknown_fields - set(('csrfmiddlewaretoken', '_accept', '_method')) # TODO: Ugh.
|
||||
|
||||
|
||||
# Check using both regular validation, and our stricter no additional fields rule
|
||||
if bound_form.is_valid() and not unknown_fields:
|
||||
# Validation succeeded...
|
||||
cleaned_data = bound_form.cleaned_data
|
||||
|
||||
cleaned_data.update(bound_form.files)
|
||||
|
||||
# Add in any extra fields to the cleaned content...
|
||||
for key in (allowed_extra_fields_set & seen_fields_set) - set(cleaned_data.keys()):
|
||||
cleaned_data[key] = data[key]
|
||||
|
|
|
@ -131,3 +131,30 @@
|
|||
# self.assertEqual(data['key1'], 'val1')
|
||||
# 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