mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Add "Created" as additional (optional) parameter for post_documents (#965)
* Added "created" as optional parameter for post_documents. * Fixed Conflict * After Black Reformatting * Run "add-trailing-comma" * The right order between black and trailing comma is important.... * Added required=False * Adds unit test for optional created in document api POST * Fixes adding of settings override * And a mis-added print, sigh Co-authored-by: Philipp <philipp@invalid.invalid> Co-authored-by: Trenton Holmes <holmes.trenton@gmail.com>
This commit is contained in:
		| @@ -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. | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
| @@ -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", | ||||
|   | ||||
| @@ -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") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Philipp
					Philipp