From 98df3f6e242e3751cea9bb641e34b40ed8c820ad Mon Sep 17 00:00:00 2001 From: SilviaAmAm Date: Tue, 17 Dec 2024 16:27:28 +0100 Subject: [PATCH] :recycle: [#558] Update endpoint for queuing destruction to be a POST --- .../destruction/api/viewsets.py | 38 ++++++++++------ .../tests/endpoints/test_list_destruction.py | 43 +++++++++++-------- 2 files changed, 49 insertions(+), 32 deletions(-) diff --git a/backend/src/openarchiefbeheer/destruction/api/viewsets.py b/backend/src/openarchiefbeheer/destruction/api/viewsets.py index d43816d2..99888268 100644 --- a/backend/src/openarchiefbeheer/destruction/api/viewsets.py +++ b/backend/src/openarchiefbeheer/destruction/api/viewsets.py @@ -168,13 +168,15 @@ description=_("Retrieve details about a destruction list."), responses={200: DestructionListReadSerializer}, ), - destroy=extend_schema( + queue_destruction=extend_schema( tags=["Destruction list"], - summary=_("Destroy destruction list"), + summary=_("Queue destruction list destruction"), description=_( - "Calling this endpoint will start a background process that will " + "Calling this endpoint will queue a background process that will " "delete the cases in the list from the case system." ), + request=None, + responses={200: None}, ), make_final=extend_schema( tags=["Destruction list"], @@ -236,7 +238,7 @@ def get_permissions(self): permission_classes = [IsAuthenticated & CanStartDestructionPermission] elif self.action == "update": permission_classes = [IsAuthenticated & CanUpdateDestructionList] - elif self.action == "destroy": + elif self.action == "queue_destruction": permission_classes = [IsAuthenticated & CanTriggerDeletion] elif self.action == "make_final": permission_classes = [IsAuthenticated & CanMarkListAsFinal] @@ -268,10 +270,13 @@ def create(self, request, *args, **kwargs): def update(self, request, *args, **kwargs): return super().update(request, *args, **kwargs) - def perform_destroy(self, instance: DestructionList) -> None: + @action(detail=True, methods=["post"], name="queue-destruction") + def queue_destruction(self, request, *args, **kwargs) -> None: + destruction_list = self.get_object() + today = date.today() destruction_date = today + timedelta(days=WAITING_PERIOD) - if not instance.all_items_can_be_deleted_by_date(destruction_date): + if not destruction_list.all_items_can_be_deleted_by_date(destruction_date): raise ValidationError( _( "This list contains cases with archiving date later than %(destruction_date)s, " @@ -281,8 +286,8 @@ def perform_destroy(self, instance: DestructionList) -> None: ) if ( - instance.planned_destruction_date - and instance.planned_destruction_date > today + destruction_list.planned_destruction_date + and destruction_list.planned_destruction_date > today ): raise ValidationError( _( @@ -291,15 +296,20 @@ def perform_destroy(self, instance: DestructionList) -> None: % {"destruction_date": destruction_date.strftime("%d/%m/%Y")} ) - logevent.destruction_list_deletion_triggered(instance, self.request.user) + logevent.destruction_list_deletion_triggered( + destruction_list, self.request.user + ) - if instance.processing_status == InternalStatus.new: - instance.planned_destruction_date = today + timedelta(days=WAITING_PERIOD) - instance.save() - return + if destruction_list.processing_status == InternalStatus.new: + destruction_list.planned_destruction_date = today + timedelta( + days=WAITING_PERIOD + ) + destruction_list.save() + return Response() # If it is a retry, process immediately - delete_destruction_list(instance) + delete_destruction_list(destruction_list) + return Response() @action(detail=True, methods=["post"], name="make-final") def make_final(self, request, *args, **kwargs): diff --git a/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_list_destruction.py b/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_list_destruction.py index 086c9397..89ad8e91 100644 --- a/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_list_destruction.py +++ b/backend/src/openarchiefbeheer/destruction/tests/endpoints/test_list_destruction.py @@ -30,13 +30,14 @@ def test_plan_destruction(self): self.client.force_authenticate(user=record_manager) with freezegun.freeze_time("2024-01-01T21:36:00+02:00"): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) - self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + self.assertEqual(status.HTTP_200_OK, response.status_code) destruction_list.refresh_from_db() @@ -69,13 +70,14 @@ def test_retry_destruction_after_failure_queues_immediately( "openarchiefbeheer.destruction.api.viewsets.delete_destruction_list" ) as m_delete, ): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) - self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + self.assertEqual(status.HTTP_200_OK, response.status_code) m_delete.assert_called_once() self.assertEqual(m_delete.call_args_list[0].args[0].pk, destruction_list.pk) @@ -98,9 +100,10 @@ def test_retry_destruction_after_failure_with_planned_date_in_future_raises_erro ) self.client.force_authenticate(user=record_manager) with (freezegun.freeze_time("2024-01-01T21:36:00+02:00"),): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) @@ -122,13 +125,14 @@ def test_can_start_destruction_if_not_author(self): with patch( "openarchiefbeheer.destruction.api.viewsets.delete_destruction_list" ): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) - self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + self.assertEqual(status.HTTP_200_OK, response.status_code) def test_cannot_start_destruction_if_not_ready_to_delete(self): record_manager = UserFactory.create(post__can_start_destruction=True) @@ -143,9 +147,10 @@ def test_cannot_start_destruction_if_not_ready_to_delete(self): with patch( "openarchiefbeheer.destruction.api.viewsets.delete_destruction_list" ) as m_task: - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) @@ -176,9 +181,10 @@ def test_cannot_start_destruction_if_archiefactiedatum_in_the_future(self): self.client.force_authenticate(user=record_manager) with freezegun.freeze_time("2024-01-01T21:36:00+02:00"): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) @@ -216,13 +222,14 @@ def test_can_start_destruction_if_archiefactiedatum_in_the_future_but_removed(se self.client.force_authenticate(user=record_manager) with freezegun.freeze_time("2024-01-01T21:36:00+02:00"): - response = self.client.delete( + response = self.client.post( reverse( - "api:destructionlist-detail", kwargs={"uuid": destruction_list.uuid} + "api:destructionlist-queue-destruction", + kwargs={"uuid": destruction_list.uuid}, ), ) - self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) + self.assertEqual(status.HTTP_200_OK, response.status_code) logs = TimelineLog.objects.for_object(destruction_list).filter( template="logging/destruction_list_deletion_triggered.txt"