From 7af94038002c86345ef0659b100cdaef11b84bcb Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Sun, 14 Oct 2018 15:29:47 +0100 Subject: [PATCH] REST: Allow unsetting of delegate While we recently fixed setting of this field via the API, we didn't resolve unsetting. Fix this now. Signed-off-by: Stephen Finucane (cherry picked from commit 7be99581f1d1b35274987e43ec57272daff94f02) --- patchwork/api/patch.py | 5 ++++- patchwork/tests/api/test_patch.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py index 6b5cc0d1..7b8e12e3 100644 --- a/patchwork/api/patch.py +++ b/patchwork/api/patch.py @@ -82,7 +82,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() @@ -116,6 +116,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 " diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py index 01770714..6a8df512 100644 --- a/patchwork/tests/api/test_patch.py +++ b/patchwork/tests/api/test_patch.py @@ -224,6 +224,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() -- 2.47.3