]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
REST: Allow unsetting of delegate
authorStephen Finucane <stephen@that.guru>
Sun, 14 Oct 2018 14:29:47 +0000 (15:29 +0100)
committerStephen Finucane <stephen@that.guru>
Sun, 14 Oct 2018 14:30:42 +0000 (15:30 +0100)
While we recently fixed setting of this field via the API, we didn't
resolve unsetting. Fix this now.

Signed-off-by: Stephen Finucane <stephen@that.guru>
patchwork/api/patch.py
patchwork/tests/api/test_patch.py

index b9a134b1724acab9c153092d6281e80781c4c894..92423cbf297f5d645052c9a488f488777f4d6069 100644 (file)
@@ -68,7 +68,7 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer):
     project = ProjectSerializer(read_only=True)
     state = StateField()
     submitter = PersonSerializer(read_only=True)
-    delegate = UserSerializer()
+    delegate = UserSerializer(allow_null=True)
     mbox = SerializerMethodField()
     series = SeriesSerializer(many=True, read_only=True)
     comments = SerializerMethodField()
@@ -102,6 +102,9 @@ class PatchListSerializer(BaseHyperlinkedModelSerializer):
 
     def validate_delegate(self, value):
         """Check that the delgate is a maintainer of the patch's project."""
+        if not value:
+            return value
+
         if not self.instance.project.maintainer_project.filter(
                 id=value.id).exists():
             raise ValidationError("User '%s' is not a maintainer for project "
index 497cb2de77124132210306b41bfdc16cdc1597a0..df30d5102c500df7983918f9c3d853d3259e52dd 100644 (file)
@@ -210,6 +210,13 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(Patch.objects.get(id=patch.id).state, state)
         self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)
 
+        # (who can unset fields too)
+        # we need to send as JSON due to https://stackoverflow.com/q/30677216/
+        resp = self.client.patch(self.api_url(patch.id), {'delegate': None},
+                                 format='json')
+        self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
+        self.assertIsNone(Patch.objects.get(id=patch.id).delegate)
+
     def test_update_invalid(self):
         """Ensure we handle invalid Patch updates."""
         project = create_project()