From a64d457c30da0b3a7aa6cd57b1666914c61e751e Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:46:49 -0700 Subject: [PATCH] Enhancement: use request user as owner of split / merge docs (#7112) --- src/documents/bulk_edit.py | 14 +++++++++++++- src/documents/tests/test_api_bulk_edit.py | 3 +++ src/documents/tests/test_bulk_edit.py | 6 ++++-- src/documents/views.py | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/documents/bulk_edit.py b/src/documents/bulk_edit.py index 7f56a1207..1f7a2a403 100644 --- a/src/documents/bulk_edit.py +++ b/src/documents/bulk_edit.py @@ -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.", ) diff --git a/src/documents/tests/test_api_bulk_edit.py b/src/documents/tests/test_api_bulk_edit.py index ebef6a329..ba0e1139f 100644 --- a/src/documents/tests/test_api_bulk_edit.py +++ b/src/documents/tests/test_api_bulk_edit.py @@ -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( diff --git a/src/documents/tests/test_bulk_edit.py b/src/documents/tests/test_bulk_edit.py index 37ebc35fc..fed93cd01 100644 --- a/src/documents/tests/test_bulk_edit.py +++ b/src/documents/tests/test_bulk_edit.py @@ -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)") diff --git a/src/documents/views.py b/src/documents/views.py index 2862809f9..575b1666b 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -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(