]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Enhancement: support custom fields in post_document endpoint (#6222)
authorshamoon <4887959+shamoon@users.noreply.github.com>
Sat, 30 Mar 2024 20:00:53 +0000 (13:00 -0700)
committerGitHub <noreply@github.com>
Sat, 30 Mar 2024 20:00:53 +0000 (13:00 -0700)
docs/api.md
src/documents/serialisers.py
src/documents/tests/test_api_documents.py
src/documents/views.py

index c2a83938d68f655b1dca3349d7d721dc139a5443..0e98b210d850ba8370db502063c6efc05bb0d61c 100644 (file)
@@ -288,6 +288,8 @@ The endpoint supports the following optional form fields:
 - `tags`: Similar to correspondent. Specify this multiple times to
   have multiple tags added to the document.
 - `archive_serial_number`: An optional archive serial number to set.
+- `custom_fields`: An array of custom field ids to assign (with an empty
+  value) to the document.
 
 The endpoint will immediately return HTTP 200 if the document consumption
 process was started successfully, with the UUID of the consumption task
index bdac7660edd8c353b3c1ba9e6a9ae48b070f0254..777edca6f00db4ecaa030ad7b5f7cac0f33c5330 100644 (file)
@@ -1113,6 +1113,14 @@ class PostDocumentSerializer(serializers.Serializer):
         max_value=Document.ARCHIVE_SERIAL_NUMBER_MAX,
     )
 
+    custom_fields = serializers.PrimaryKeyRelatedField(
+        many=True,
+        queryset=CustomField.objects.all(),
+        label="Custom fields",
+        write_only=True,
+        required=False,
+    )
+
     def validate_document(self, document):
         document_data = document.file.read()
         mime_type = magic.from_buffer(document_data, mime=True)
@@ -1148,6 +1156,12 @@ class PostDocumentSerializer(serializers.Serializer):
         else:
             return None
 
+    def validate_custom_fields(self, custom_fields):
+        if custom_fields:
+            return [custom_field.id for custom_field in custom_fields]
+        else:
+            return None
+
 
 class BulkDownloadSerializer(DocumentListSerializer):
     content = serializers.ChoiceField(
index d7ae1eeb7ed8b2e819b14a3a2e5c657c8b458253..4798fef9572fe73267238fb96710c4e15bf29569 100644 (file)
@@ -1134,6 +1134,38 @@ class TestDocumentApi(DirectoriesMixin, DocumentConsumeDelayMixin, APITestCase):
         self.assertIsNone(overrides.tag_ids)
         self.assertEqual(500, overrides.asn)
 
+    def test_upload_with_custom_fields(self):
+        self.consume_file_mock.return_value = celery.result.AsyncResult(
+            id=str(uuid.uuid4()),
+        )
+
+        custom_field = CustomField.objects.create(
+            name="stringfield",
+            data_type=CustomField.FieldDataType.STRING,
+        )
+
+        with open(
+            os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"),
+            "rb",
+        ) as f:
+            response = self.client.post(
+                "/api/documents/post_document/",
+                {
+                    "document": f,
+                    "custom_fields": [custom_field.id],
+                },
+            )
+
+        self.assertEqual(response.status_code, status.HTTP_200_OK)
+
+        self.consume_file_mock.assert_called_once()
+
+        input_doc, overrides = self.get_last_consume_delay_call_args()
+
+        self.assertEqual(input_doc.original_file.name, "simple.pdf")
+        self.assertEqual(overrides.filename, "simple.pdf")
+        self.assertEqual(overrides.custom_field_ids, [custom_field.id])
+
     def test_get_metadata(self):
         doc = Document.objects.create(
             title="test",
index 3e1996215d25139e4e79f0ce4d6a6bf058cbe0cb..3fcc540238e380e7391845e8e979aa99adf2150e 100644 (file)
@@ -930,6 +930,7 @@ class PostDocumentView(GenericAPIView):
         title = serializer.validated_data.get("title")
         created = serializer.validated_data.get("created")
         archive_serial_number = serializer.validated_data.get("archive_serial_number")
+        custom_field_ids = serializer.validated_data.get("custom_fields")
 
         t = int(mktime(datetime.now().timetuple()))
 
@@ -957,6 +958,7 @@ class PostDocumentView(GenericAPIView):
             created=created,
             asn=archive_serial_number,
             owner_id=request.user.id,
+            custom_field_ids=custom_field_ids,
         )
 
         async_task = consume_file.delay(