Skip to content
Snippets Groups Projects
Commit 62fb3d63 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Valentin Rigal
Browse files

Support null values on image and polygon in CreateElement

parent 6386dd26
No related branches found
No related tags found
1 merge request!2410Support null values on image and polygon in CreateElement
......@@ -693,6 +693,7 @@ class ElementCreateSerializer(ElementLightSerializer):
image = serializers.PrimaryKeyRelatedField(
queryset=Image.objects.using("default").all().select_related("server"),
required=False,
allow_null=True,
help_text="Link this element to an image by UUID via a polygon.",
style={"base_template": "input.html"},
)
......@@ -704,6 +705,7 @@ class ElementCreateSerializer(ElementLightSerializer):
)
polygon = LinearRingField(
required=False,
allow_null=True,
help_text="Set the polygon linking this element to the image. "
"Either `image` must be set, or a `parent` element with an image must be set, to use a polygon. "
"Defaults to a rectangle taking up the whole image.",
......
......@@ -546,6 +546,32 @@ class TestCreateElements(FixtureAPITestCase):
"confidence": [expected_message]
})
def test_null_image_polygon(self):
"""
Null values on `image` and `polygon` should be treated like the fields are absent from the payload
"""
self.client.force_login(self.user)
with self.assertNumQueries(7):
response = self.client.post(
reverse("api:elements-create"),
{
"corpus": str(self.corpus.id),
"name": "Castle story",
"type": "act",
"image": None,
"polygon": None,
},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
act = Element.objects.get(id=response.json()["id"])
self.assertEqual(act.name, "Castle story")
self.assertEqual(act.type, self.act_type)
self.assertIsNone(act.image_id)
self.assertIsNone(act.polygon)
def test_worker_run_local(self):
"""
A regular user can create an element with a WorkerRun of their own local process
......
......@@ -62,6 +62,10 @@ class LinearRingField(serializers.ListField):
def run_validation(self, data):
value = super().run_validation(data)
# Ignore nothingness
if value is None:
return
# Ensure the LineString is closed
if value[0] != value[-1]:
value.append(value[0])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment