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

Handle duplicated manual classification creation

parent e7b7133f
No related branches found
No related tags found
No related merge requests found
......@@ -170,6 +170,14 @@ class ClassificationCreate(CreateAPIView):
'internal': False,
}
)
if Classification.objects.filter(
element_id=serializer.validated_data['element'],
source_id=data_source.id,
ml_class_id=serializer.validated_data['ml_class']
).exists():
raise ValidationError(
{'__all__': ['A manual classification already exists for this `element` and this `class`']}
)
serializer.save(
source=data_source,
moderator=self.request.user,
......
......@@ -40,6 +40,27 @@ class TestClasses(FixtureAPITestCase):
self.assertEqual(response.data['confidence'], 1)
self.assertEqual(response.data['best'], True)
def test_classification_exists(self):
"""
If a classification exists, creation must respond a 400_BAD_REQUEST
with an explicit message
"""
self.client.force_login(self.user)
request = (
reverse('api:classification-create'),
{
'element': str(self.element.id),
'ml_class': str(self.text.id)
}
)
response = self.client.post(*request)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = self.client.post(*request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(response.json(), {
'__all__': ['A manual classification already exists for this `element` and this `class`']
})
def test_classification_ignored_params(self):
"""
Ensure confidence and best attribute cannot be changed
......
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