Skip to content

Commit

Permalink
enhance XForm admin
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Nov 27, 2024
1 parent 02be232 commit a1b8313
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions onadata/apps/logger/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit a1b8313

Please sign in to comment.