Skip to content

Commit

Permalink
POST parsed as json does not use a QueryDict
Browse files Browse the repository at this point in the history
  • Loading branch information
bkg committed May 12, 2018
1 parent 8091bb9 commit 770cdce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
7 changes: 5 additions & 2 deletions spillway/forms/forms.py
Expand Up @@ -19,8 +19,11 @@ def __init__(self, data=None, queryset=None, *args, **kwargs):

@classmethod
def from_request(cls, request, queryset=None, view=None):
data = (request.query_params.dict() or
request.data and request.data.dict())
data = request.query_params or request.data
try:
data = data.dict()
except AttributeError:
pass
params = dict(data, **getattr(view, 'kwargs', {}))
params['format'] = request.accepted_renderer.format
return cls(params, queryset, files=request.FILES)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_forms.py
Expand Up @@ -4,10 +4,14 @@
from django.forms import fields
from django.test import SimpleTestCase, TestCase
from django.contrib.gis import geos
from rest_framework.views import APIView
from rest_framework.test import APIRequestFactory

from spillway import forms
from .models import _geom, Location

factory = APIRequestFactory()


class PKeyQuerySetForm(forms.QuerySetForm):
pk = fields.IntegerField()
Expand Down Expand Up @@ -35,6 +39,17 @@ def test_upload_field(self):
self.assertEqual(form.cleaned_data['g'], geom.ogr)
self.assertEqual(form.cleaned_data['g'].srs.srid, 4326)

def test_from_request(self):
request = factory.post('/', json.dumps({'g': _geom}),
content_type='application/json')
view = APIView()
request = view.initialize_request(request)
view.initial(request)
form = forms.RasterQueryForm.from_request(request)
self.assertTrue(form.is_valid())
geom = geos.GEOSGeometry(json.dumps(_geom))
self.assertEqual(form.cleaned_data['g'], geom.ogr)


class SpatialQueryFormTestCase(SimpleTestCase):
def _assert_form_data(self, form, key, expected):
Expand Down

0 comments on commit 770cdce

Please sign in to comment.