From c1633ef37a671f591bf991ba1855a5dd501725a4 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 6 Mar 2024 12:28:42 -0800 Subject: [PATCH] Enhancement: rename and move files when storage path deleted Revert "Enhancement: rename and move files when storage path deleted" This reverts commit 86602a3bc23dcc9ba328a71419a328f75b27ab89. yes this --- src/documents/tests/test_api_objects.py | 29 +++++++++++++++++++++++++ src/documents/views.py | 16 ++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/documents/tests/test_api_objects.py b/src/documents/tests/test_api_objects.py index 9a0ccd598..65f379261 100644 --- a/src/documents/tests/test_api_objects.py +++ b/src/documents/tests/test_api_objects.py @@ -224,6 +224,35 @@ class TestApiStoragePaths(DirectoriesMixin, APITestCase): self.assertCountEqual([document.pk], args[0]) + @mock.patch("documents.bulk_edit.bulk_update_documents.delay") + def test_api_delete_storage_path(self, bulk_update_mock): + """ + GIVEN: + - API request to delete a storage + WHEN: + - API is called + THEN: + - Documents using the storage path are updated + """ + document = Document.objects.create( + mime_type="application/pdf", + storage_path=self.sp1, + ) + response = self.client.delete( + f"{self.ENDPOINT}{self.sp1.pk}/", + ) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + # sp with no documents + sp2 = StoragePath.objects.create(name="sp2", path="Something2/{checksum}") + response = self.client.delete( + f"{self.ENDPOINT}{sp2.pk}/", + ) + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + + # only called once + bulk_update_mock.assert_called_once_with([document.pk]) + class TestBulkEditObjects(APITestCase): # See test_api_permissions.py for bulk tests on permissions diff --git a/src/documents/views.py b/src/documents/views.py index 006ca0822..99db0a5d8 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -1211,6 +1211,22 @@ class StoragePathViewSet(ModelViewSet, PermissionsAwareDocumentCountMixin): filterset_class = StoragePathFilterSet ordering_fields = ("name", "path", "matching_algorithm", "match", "document_count") + def destroy(self, request, *args, **kwargs): + """ + When a storage path is deleted, see if documents + using it require a rename/move + """ + instance = self.get_object() + doc_ids = [doc.id for doc in instance.documents.all()] + + # perform the deletion so renaming/moving can happen + response = super().destroy(request, *args, **kwargs) + + if len(doc_ids): + bulk_edit.bulk_update_documents.delay(doc_ids) + + return response + class UiSettingsView(GenericAPIView): queryset = UiSettings.objects.all()