]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Allow for mutability of request.POST
authorStephen Finucane <stephen@that.guru>
Fri, 19 May 2017 14:53:22 +0000 (15:53 +0100)
committerStephen Finucane <stephen@that.guru>
Sun, 3 Dec 2017 20:54:42 +0000 (20:54 +0000)
Using Django 1.11 yields the following error for the 'patchwork.tests
.test_rest_api.TestCheckAPI.test_create' test:

    AttributeError: This QueryDict instance is immutable

This occurs due to our modification of data to allow users to create
instances using a slugified state instead of the underlying integer
value, e.g. 'success' instead of 1.

Resolve this by unsetting the immutability of that queryset. As
suggested in the linked SO answer, there is limited side effects to
doing this.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Tested-by: Daniel Axtens <dja@axtens.net>
patchwork/api/check.py

index 5b3815050664f516fad538c2cd7c1797eb705b13..b37d6e01677017b89304a7a79d4ed33f4151fe35 100644 (file)
@@ -49,7 +49,16 @@ class CheckSerializer(HyperlinkedModelSerializer):
     def run_validation(self, data):
         for val, label in Check.STATE_CHOICES:
             if label == data['state']:
+                # NOTE(stephenfin): 'data' is essentially 'request.POST', which
+                # is immutable by default. However, there's no good reason for
+                # this to be this way [1], so temporarily unset that mutability
+                # to fix what we need to here.
+                #
+                # [1] http://stackoverflow.com/a/12619745/613428
+                mutable = data._mutable  # noqa
+                data._mutable = True  # noqa
                 data['state'] = val
+                data._mutable = mutable  # noqa
                 break
         return super(CheckSerializer, self).run_validation(data)