mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-10-30 03:56:23 -05:00 
			
		
		
		
	Enhancement: use request user as owner of split / merge docs (#7112)
This commit is contained in:
		| @@ -10,6 +10,7 @@ from celery import chord | ||||
| from celery import group | ||||
| from celery import shared_task | ||||
| from django.conf import settings | ||||
| from django.contrib.auth.models import User | ||||
| from django.db.models import Q | ||||
|  | ||||
| from documents.data_models import ConsumableDocument | ||||
| @@ -243,6 +244,7 @@ def merge( | ||||
|     doc_ids: list[int], | ||||
|     metadata_document_id: Optional[int] = None, | ||||
|     delete_originals: bool = False, | ||||
|     user: User = None, | ||||
| ): | ||||
|     logger.info( | ||||
|         f"Attempting to merge {len(doc_ids)} documents into a single document.", | ||||
| @@ -285,6 +287,9 @@ def merge( | ||||
|     else: | ||||
|         overrides = DocumentMetadataOverrides() | ||||
|  | ||||
|     if user is not None: | ||||
|         overrides.owner_id = user.id | ||||
|  | ||||
|     logger.info("Adding merged document to the task queue.") | ||||
|  | ||||
|     consume_task = consume_file.s( | ||||
| @@ -306,7 +311,12 @@ def merge( | ||||
|     return "OK" | ||||
|  | ||||
|  | ||||
| def split(doc_ids: list[int], pages: list[list[int]], delete_originals: bool = False): | ||||
| def split( | ||||
|     doc_ids: list[int], | ||||
|     pages: list[list[int]], | ||||
|     delete_originals: bool = False, | ||||
|     user: User = None, | ||||
| ): | ||||
|     logger.info( | ||||
|         f"Attempting to split document {doc_ids[0]} into {len(pages)} documents", | ||||
|     ) | ||||
| @@ -331,6 +341,8 @@ def split(doc_ids: list[int], pages: list[list[int]], delete_originals: bool = F | ||||
|  | ||||
|                 overrides = DocumentMetadataOverrides().from_document(doc) | ||||
|                 overrides.title = f"{doc.title} (split {idx + 1})" | ||||
|                 if user is not None: | ||||
|                     overrides.owner_id = user.id | ||||
|                 logger.info( | ||||
|                     f"Adding split document with pages {split_doc} to the task queue.", | ||||
|                 ) | ||||
|   | ||||
| @@ -20,6 +20,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): | ||||
|         super().setUp() | ||||
|  | ||||
|         user = User.objects.create_superuser(username="temp_admin") | ||||
|         self.user = user | ||||
|         self.client.force_authenticate(user=user) | ||||
|  | ||||
|         patcher = mock.patch("documents.bulk_edit.bulk_update_documents.delay") | ||||
| @@ -993,6 +994,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): | ||||
|         args, kwargs = m.call_args | ||||
|         self.assertCountEqual(args[0], [self.doc2.id, self.doc3.id]) | ||||
|         self.assertEqual(kwargs["metadata_document_id"], self.doc3.id) | ||||
|         self.assertEqual(kwargs["user"], self.user) | ||||
|  | ||||
|     @mock.patch("documents.serialisers.bulk_edit.merge") | ||||
|     def test_merge_and_delete_insufficient_permissions(self, m): | ||||
| @@ -1092,6 +1094,7 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase): | ||||
|         args, kwargs = m.call_args | ||||
|         self.assertCountEqual(args[0], [self.doc2.id]) | ||||
|         self.assertEqual(kwargs["pages"], [[1], [2, 3, 4], [5, 6], [7]]) | ||||
|         self.assertEqual(kwargs["user"], self.user) | ||||
|  | ||||
|     def test_split_invalid_params(self): | ||||
|         response = self.client.post( | ||||
|   | ||||
| @@ -422,8 +422,9 @@ class TestPDFActions(DirectoriesMixin, TestCase): | ||||
|         """ | ||||
|         doc_ids = [self.doc1.id, self.doc2.id, self.doc3.id] | ||||
|         metadata_document_id = self.doc1.id | ||||
|         user = User.objects.create(username="test_user") | ||||
|  | ||||
|         result = bulk_edit.merge(doc_ids) | ||||
|         result = bulk_edit.merge(doc_ids, None, False, user) | ||||
|  | ||||
|         expected_filename = ( | ||||
|             f"{'_'.join([str(doc_id) for doc_id in doc_ids])[:100]}_merged.pdf" | ||||
| @@ -525,7 +526,8 @@ class TestPDFActions(DirectoriesMixin, TestCase): | ||||
|         """ | ||||
|         doc_ids = [self.doc2.id] | ||||
|         pages = [[1, 2], [3]] | ||||
|         result = bulk_edit.split(doc_ids, pages) | ||||
|         user = User.objects.create(username="test_user") | ||||
|         result = bulk_edit.split(doc_ids, pages, False, user) | ||||
|         self.assertEqual(mock_consume_file.call_count, 2) | ||||
|         consume_file_args, _ = mock_consume_file.call_args | ||||
|         self.assertEqual(consume_file_args[1].title, "B (split 2)") | ||||
|   | ||||
| @@ -961,6 +961,11 @@ class BulkEditView(PassUserMixin): | ||||
|         method = serializer.validated_data.get("method") | ||||
|         parameters = serializer.validated_data.get("parameters") | ||||
|         documents = serializer.validated_data.get("documents") | ||||
|         if method in [ | ||||
|             bulk_edit.split, | ||||
|             bulk_edit.merge, | ||||
|         ]: | ||||
|             parameters["user"] = user | ||||
|  | ||||
|         if not user.is_superuser: | ||||
|             document_objs = Document.objects.select_related("owner").filter( | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 shamoon
					shamoon