Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Fix: warn with proper error on ASN exists in trash (#8176)

shamoonef329fc68710
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index aeb901f81..6f38904da 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -805,6 +805,22 @@ class DocumentSerializer(
doc["content"] = doc.get("content")[0:550]
return doc
+ def validate(self, attrs):
+ if (
+ "archive_serial_number" in attrs
+ and Document.deleted_objects.filter(
+ archive_serial_number=attrs["archive_serial_number"],
+ ).exists()
+ ):
+ raise serializers.ValidationError(
+ {
+ "archive_serial_number": [
+ "Document with this Archive Serial Number already exists in the trash.",
+ ],
+ },
+ )
+ return super().validate(attrs)
+
def update(self, instance: Document, validated_data):
if "created_date" in validated_data and "created" not in validated_data:
new_datetime = datetime.datetime.combine(
diff --git a/src/documents/tests/test_api_documents.py b/src/documents/tests/test_api_documents.py
index 2e2b02f0d..08d86d24e 100644
--- a/src/documents/tests/test_api_documents.py
+++ b/src/documents/tests/test_api_documents.py
@@ -2540,6 +2540,50 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(resp.content, b"1")
+ def test_asn_not_unique_with_trashed_doc(self):
+ """
+ GIVEN:
+ - Existing document with ASN that is trashed
+ WHEN:
+ - API request to update document with same ASN
+ THEN:
+ - Explicit error is returned
+ """
+ user1 = User.objects.create_superuser(username="test1")
+
+ self.client.force_authenticate(user1)
+
+ doc1 = Document.objects.create(
+ title="test",
+ mime_type="application/pdf",
+ content="this is a document 1",
+ checksum="1",
+ archive_serial_number=1,
+ )
+ doc1.delete()
+
+ doc2 = Document.objects.create(
+ title="test2",
+ mime_type="application/pdf",
+ content="this is a document 2",
+ checksum="2",
+ )
+ result = self.client.patch(
+ f"/api/documents/{doc2.pk}/",
+ {
+ "archive_serial_number": 1,
+ },
+ )
+ self.assertEqual(result.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(
+ result.json(),
+ {
+ "archive_serial_number": [
+ "Document with this Archive Serial Number already exists in the trash.",
+ ],
+ },
+ )
+
def test_remove_inbox_tags(self):
"""
GIVEN: