Skip to content

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

Add "Created" as additional (optional) parameter for post_documents (#965)

Philippbb15b744c8f9
diff --git a/docs/api.rst b/docs/api.rst
index 7cbf08718..c23866b60 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -240,11 +240,13 @@ be instructed to consume the document from there.
The endpoint supports the following optional form fields:
* ``title``: Specify a title that the consumer should use for the document.
+* ``created``: Specify a DateTime where the document was created (e.g. "2016-04-19" or "2016-04-19 06:15:00+02:00").
* ``correspondent``: Specify the ID of a correspondent that the consumer should use for the document.
* ``document_type``: Similar to correspondent.
* ``tags``: Similar to correspondent. Specify this multiple times to have multiple tags added
to the document.
+
The endpoint will immediately return "OK" if the document consumption process
was started successfully. No additional status information about the consumption
process itself is available, since that happens in a different process.
diff --git a/src/documents/consumer.py b/src/documents/consumer.py
index 061f17ee1..5e3d01fbc 100644
--- a/src/documents/consumer.py
+++ b/src/documents/consumer.py
@@ -189,6 +189,7 @@ class Consumer(LoggingMixin):
override_document_type_id=None,
override_tag_ids=None,
task_id=None,
+ override_created=None,
) -> Document:
"""
Return the document object if it was successfully created.
@@ -201,6 +202,7 @@ class Consumer(LoggingMixin):
self.override_document_type_id = override_document_type_id
self.override_tag_ids = override_tag_ids
self.task_id = task_id or str(uuid.uuid4())
+ self.override_created = override_created
self._send_progress(0, 100, "STARTING", MESSAGE_NEW_FILE)
@@ -394,7 +396,13 @@ class Consumer(LoggingMixin):
self.log("debug", "Saving record to database")
- if file_info.created is not None:
+ if self.override_created is not None:
+ create_date = self.override_created
+ self.log(
+ "debug",
+ f"Creation date from post_documents parameter: {create_date}",
+ )
+ elif file_info.created is not None:
create_date = file_info.created
self.log("debug", f"Creation date from FileInfo: {create_date}")
elif date is not None:
diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py
index a257a1dd6..1bdcbab9e 100644
--- a/src/documents/serialisers.py
+++ b/src/documents/serialisers.py
@@ -412,6 +412,13 @@ class BulkEditSerializer(DocumentListSerializer):
class PostDocumentSerializer(serializers.Serializer):
+ created = serializers.DateTimeField(
+ label="Created",
+ allow_null=True,
+ write_only=True,
+ required=False,
+ )
+
document = serializers.FileField(
label="Document",
write_only=True,
diff --git a/src/documents/tasks.py b/src/documents/tasks.py
index 7fd1f5672..029c207c3 100644
--- a/src/documents/tasks.py
+++ b/src/documents/tasks.py
@@ -238,6 +238,7 @@ def consume_file(
override_document_type_id=None,
override_tag_ids=None,
task_id=None,
+ override_created=None,
):
# check for separators in current document
@@ -318,6 +319,7 @@ def consume_file(
override_document_type_id=override_document_type_id,
override_tag_ids=override_tag_ids,
task_id=task_id,
+ override_created=override_created,
)
if document:
diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py
index b492181d8..d8dfcf455 100644
--- a/src/documents/tests/test_api.py
+++ b/src/documents/tests/test_api.py
@@ -9,6 +9,11 @@ import zipfile
from unittest import mock
from unittest.mock import MagicMock
+try:
+ import zoneinfo
+except ImportError:
+ import backports.zoneinfo as zoneinfo
+
import pytest
from django.conf import settings
from django.contrib.auth.models import User
@@ -955,6 +960,34 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
async_task.assert_not_called()
+ @mock.patch("documents.views.async_task")
+ def test_upload_with_created(self, async_task):
+ created = datetime.datetime(
+ 2022,
+ 5,
+ 12,
+ 0,
+ 0,
+ 0,
+ 0,
+ tzinfo=zoneinfo.ZoneInfo("America/Los_Angeles"),
+ )
+ 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, "created": created},
+ )
+ self.assertEqual(response.status_code, 200)
+
+ async_task.assert_called_once()
+
+ args, kwargs = async_task.call_args
+
+ self.assertEqual(kwargs["override_created"], created)
+
def test_get_metadata(self):
doc = Document.objects.create(
title="test",
diff --git a/src/documents/views.py b/src/documents/views.py
index 5d179da02..414ccf070 100644
--- a/src/documents/views.py
+++ b/src/documents/views.py
@@ -504,6 +504,7 @@ class PostDocumentView(GenericAPIView):
document_type_id = serializer.validated_data.get("document_type")
tag_ids = serializer.validated_data.get("tags")
title = serializer.validated_data.get("title")
+ created = serializer.validated_data.get("created")
t = int(mktime(datetime.now().timetuple()))
@@ -530,6 +531,7 @@ class PostDocumentView(GenericAPIView):
override_tag_ids=tag_ids,
task_id=task_id,
task_name=os.path.basename(doc_name)[:100],
+ override_created=created,
)
return Response("OK")