From: Stephen Finucane Date: Tue, 30 Apr 2019 06:03:02 +0000 (+1000) Subject: REST: Handle JSON requests X-Git-Tag: v2.1.2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b7aa6f1f62c56dade7dd524ebcba58546fb07f6b;p=thirdparty%2Fpatchwork.git REST: Handle JSON requests This was raising an attribute error when switching tests to use JSON bodies instead of form-data. AttributeError: 'dict' object has no attribute '_mutable' The easy fix is to check if it's a dictionary and avoid the mutability check if so. NOTE(daxtens): This is needed for JSON bodies sent by regular users, not just the tests. Signed-off-by: Stephen Finucane (cherry picked from commit dc48fbce99efe7d13987a3f510f7dee389636eba) --- diff --git a/patchwork/api/check.py b/patchwork/api/check.py index d76573a5..67062132 100644 --- a/patchwork/api/check.py +++ b/patchwork/api/check.py @@ -50,7 +50,12 @@ class CheckSerializer(HyperlinkedModelSerializer): def run_validation(self, data): for val, label in Check.STATE_CHOICES: - if label == data['state']: + if label != data['state']: + continue + + if isinstance(data, dict): # json request + data['state'] = val + else: # form-data request # 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 @@ -61,7 +66,8 @@ class CheckSerializer(HyperlinkedModelSerializer): data._mutable = True # noqa data['state'] = val data._mutable = mutable # noqa - break + + break return super(CheckSerializer, self).run_validation(data) def to_representation(self, instance):