diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index f9fde9ac8..d9f1833bf 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -246,8 +246,55 @@ class BulkEditSerializer(serializers.Serializer): else: raise serializers.ValidationError("Unsupported method.") + def _validate_parameters_tags(self, parameters): + if 'tag' in parameters: + tag_id = parameters['tag'] + try: + Tag.objects.get(id=tag_id) + except Tag.DoesNotExist: + raise serializers.ValidationError("Tag does not exist") + else: + raise serializers.ValidationError("tag not specified") + + def _validate_parameters_document_type(self, parameters): + if 'document_type' in parameters: + document_type_id = parameters['document_type'] + if document_type_id is None: + # None is ok + return + try: + DocumentType.objects.get(id=document_type_id) + except DocumentType.DoesNotExist: + raise serializers.ValidationError( + "Document type does not exist") + else: + raise serializers.ValidationError("document_type not specified") + + def _validate_parameters_correspondent(self, parameters): + if 'correspondent' in parameters: + correspondent_id = parameters['correspondent'] + if correspondent_id is None: + return + try: + Correspondent.objects.get(id=correspondent_id) + except Correspondent.DoesNotExist: + raise serializers.ValidationError( + "Correspondent does not exist") + else: + raise serializers.ValidationError("correspondent not specified") + def validate(self, attrs): + method = attrs['method'] + parameters = attrs['parameters'] + + if method == bulk_edit.set_correspondent: + self._validate_parameters_correspondent(parameters) + elif method == bulk_edit.set_document_type: + self._validate_parameters_document_type(parameters) + elif method == bulk_edit.add_tag or method == bulk_edit.remove_tag: + self._validate_parameters_tags(parameters) + return attrs diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index 494183d2d..dc4d49d4e 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -669,6 +669,7 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): self.assertEqual(v1.filter_rules.count(), 0) + class TestBulkEdit(DirectoriesMixin, APITestCase): def setUp(self): @@ -891,7 +892,7 @@ class TestBulkEdit(DirectoriesMixin, APITestCase): doc2 = Document.objects.get(id=self.doc2.id) self.assertEqual(doc2.document_type, self.dt1) - def test_api_invalid_tag(self): + def test_api_add_invalid_tag(self): self.assertEqual(list(self.doc2.tags.all()), [self.t1]) response = self.client.post("/api/documents/bulk_edit/", json.dumps({ "documents": [self.doc2.id], @@ -901,3 +902,14 @@ class TestBulkEdit(DirectoriesMixin, APITestCase): self.assertEqual(response.status_code, 400) self.assertEqual(list(self.doc2.tags.all()), [self.t1]) + + def test_api_delete_invalid_tag(self): + self.assertEqual(list(self.doc2.tags.all()), [self.t1]) + response = self.client.post("/api/documents/bulk_edit/", json.dumps({ + "documents": [self.doc2.id], + "method": "remove_tag", + "parameters": {'tag': 345657} + }), content_type='application/json') + self.assertEqual(response.status_code, 400) + + self.assertEqual(list(self.doc2.tags.all()), [self.t1])