]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Fix: warn with proper error on ASN exists in trash (#8176)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Mon, 4 Nov 2024 01:52:59 +0000 (17:52 -0800)
committerGitHub <noreply@github.com>
Mon, 4 Nov 2024 01:52:59 +0000 (17:52 -0800)
src/documents/serialisers.py
src/documents/tests/test_api_documents.py

index aeb901f81fae673449f20cffffd43ba8256793ca..6f38904dadee9ca738fa23f15c2e24f774719364 100644 (file)
@@ -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(
index 2e2b02f0d3ad1386cc65abd683c5319d5df50c69..08d86d24e977c12e29fa75d5219899e7deaca2de 100644 (file)
@@ -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: