From 3329c5f937415b60f7965954ea7b8ca2c6b161e3 Mon Sep 17 00:00:00 2001 From: apiyo Date: Thu, 9 Nov 2023 08:28:25 +0300 Subject: [PATCH] Soft delete xform from legacy UI Don't call xform.delete() method which soft deletes --- .../api/tests/viewsets/test_xform_viewset.py | 36 +++++++++++++++++++ onadata/apps/logger/views.py | 5 ++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_xform_viewset.py b/onadata/apps/api/tests/viewsets/test_xform_viewset.py index 3345631f4b..ae55a4d1bc 100644 --- a/onadata/apps/api/tests/viewsets/test_xform_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_xform_viewset.py @@ -59,6 +59,7 @@ from onadata.apps.logger.models import Attachment, Instance, Project, XForm from onadata.apps.logger.models.xform_version import XFormVersion from onadata.apps.logger.xform_instance_parser import XLSFormError +from onadata.apps.logger.views import delete_xform from onadata.apps.main.models import MetaData from onadata.apps.messaging.constants import FORM_UPDATED, XFORM from onadata.apps.viewer.models import Export @@ -4351,6 +4352,41 @@ def test_delete_xform_also_deletes_linked_dataviews(self): self.assertIsNotNone(self.data_view.deleted_at) self.assertIn("-deleted-at-", self.data_view.name) + def test_delete_xform_endpoint(self): + """ + Tests that the delete_xform view soft deletes xforms + """ + # publish form and make submissions + xlsform_path = os.path.join( + settings.PROJECT_ROOT, "libs", "tests", "utils", "fixtures", "tutorial.xlsx" + ) + self._publish_xls_form_to_project(xlsform_path=xlsform_path) + for x in range(1, 9): + path = os.path.join( + settings.PROJECT_ROOT, + "libs", + "tests", + "utils", + "fixtures", + "tutorial", + "instances", + "uuid{}".format(x), + "submission.xml", + ) + self._make_submission(path) + + # Make request to delete + request = self.factory.post("/", **self.extra) + request.user = self.xform.user + response = delete_xform( + request, username=self.xform.user.username, id_string=self.xform.id_string + ) + + self.assertEqual(response.status_code, 302) + self.assertEqual(response.content, b"") + self.xform.refresh_from_db() + self.assertIsNotNone(self.xform.deleted_at) + def test_multitple_enketo_urls(self): with HTTMock(enketo_mock): self._publish_xls_form_to_project() diff --git a/onadata/apps/logger/views.py b/onadata/apps/logger/views.py index d837be284e..21f75b2d69 100644 --- a/onadata/apps/logger/views.py +++ b/onadata/apps/logger/views.py @@ -46,7 +46,6 @@ PublishXForm, inject_instanceid, publish_form, - remove_xform, response_with_mimetype_and_name, safe_create_instance, ) @@ -504,8 +503,8 @@ def delete_xform(request, username, id_string): {"user__username__iexact": username, "id_string__iexact": id_string} ) - # delete xform and submissions - remove_xform(xform) + # Delete xform + xform.soft_delete(user=request.user) audit = {} audit_log(