From a1b83134f5cf19e0627180269ba74c3b04d5f8e8 Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Wed, 27 Nov 2024 09:50:53 +0300 Subject: [PATCH] enhance XForm admin --- onadata/apps/logger/admin.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/onadata/apps/logger/admin.py b/onadata/apps/logger/admin.py index 33b80d04cf..9edd62314a 100644 --- a/onadata/apps/logger/admin.py +++ b/onadata/apps/logger/admin.py @@ -43,8 +43,9 @@ def internal_id(self, obj): def restore_form(self, request, queryset): """Custom admin action to restore soft-deleted XForms.""" restored_count = 0 + for xform in queryset: - if xform.deleted_at: + if xform.deleted_at is not None: try: call_command("restore_form", xform.id) restored_count += 1 @@ -54,6 +55,7 @@ def restore_form(self, request, queryset): _(f"Failed to restore XForm {xform.id_string}: {exc}"), level=messages.ERROR, ) + if restored_count > 0: self.message_user( request, @@ -67,19 +69,32 @@ def delete_queryset(self, request, queryset): """ Override delete_queryset to perform soft deletion on XForms. """ + deleted_count = 0 + for xform in queryset: - xform.soft_delete(user=request.user) + if xform.deleted_at is None: + xform.soft_delete(user=request.user) + deleted_count += 1 - self.message_user( - request, - _("Selected XForms have been soft-deleted."), - level=messages.SUCCESS, - ) + if deleted_count > 0: + self.message_user( + request, + _(f"Successfully soft-deleted {deleted_count} XForms."), + level=messages.SUCCESS, + ) def delete_model(self, request, obj): """ Override delete_model to perform soft deletion on a single XForm. """ + if obj.deleted_at is not None: + self.message_user( + request, + _("The XForm has already been soft-deleted."), + level=messages.WARNING, + ) + return + obj.soft_delete(user=request.user) self.message_user( request, _("The XForm has been soft-deleted."), level=messages.SUCCESS