Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Jun 14, 2023
1 parent 7c6ad57 commit 3b374ac
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
20 changes: 20 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,26 @@ def test_permanent_instance_delete_inactive_form(self, send_message_mock):
self.assertEqual(self.xform.num_of_submissions, 2)
self.assertTrue(send_message_mock.called)

@override_settings(ENABLE_SUBMISSION_PERMANENT_DELETE=False)
def test_failed_permanent_deletion(self):
"""
Test that permanent submission deletion throws bad request when
functionality is disabled
"""
self._make_submissions()
formid = self.xform.pk
dataid = self.xform.instances.all().order_by("id")[0].pk
view = DataViewSet.as_view({"delete": "destroy"})

request = self.factory.delete(
"/", **self.extra, data={"permanent_delete": True}
)
response = view(request, pk=formid, dataid=dataid)
self.assertEqual(response.status_code, 400)
self.assertEqual(
response.data, {"error": "Permanent Submission deletion not allowed"}
)

@patch("onadata.apps.api.viewsets.data_viewset.send_message")
def test_delete_submission_inactive_form(self, send_message_mock):
self._make_submissions()
Expand Down
25 changes: 21 additions & 4 deletions onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,24 @@ def destroy(self, request, *args, **kwargs):
deleted_at__isnull=True,
)

error_msg = None
for instance in queryset.iterator():
if permanent_delete and enable_submission_permanent_delete:
instance.delete()
if permanent_delete:
if enable_submission_permanent_delete:
instance.delete()
else:
error_msg = {
"error": _("Permanent Submission deletion not allowed")
}

else:
# enable soft deletion
delete_instance(instance, request.user)

if error_msg:
# return error msg if permanent deletion not enabled
return Response(error_msg, status=status.HTTP_400_BAD_REQUEST)

# updates the num_of_submissions for the form.
after_count = self.object.submission_count(force_update=True)
number_of_records_deleted = initial_count - after_count
Expand All @@ -403,8 +414,14 @@ def destroy(self, request, *args, **kwargs):

if request.user.has_perm(CAN_DELETE_SUBMISSION, self.object.xform):
instance_id = self.object.pk
if permanent_delete and enable_submission_permanent_delete:
self.object.delete()
if permanent_delete:
if enable_submission_permanent_delete:
self.object.delete()
else:
error_msg = {
"error": _("Permanent Submission deletion not allowed")
}
return Response(error_msg, status=status.HTTP_400_BAD_REQUEST)
else:
# enable soft deletion
delete_instance(self.object, request.user)
Expand Down

0 comments on commit 3b374ac

Please sign in to comment.