Skip to content

Commit

Permalink
add ability to restore form in Django admin
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Nov 26, 2024
1 parent 4c1d765 commit b3e97a7
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions onadata/apps/logger/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"""
Logger admin module.
"""
from django.contrib import admin

from django.contrib import admin, messages
from django.core.management import call_command
from django.utils.translation import gettext_lazy as _

from reversion.admin import VersionAdmin

Expand All @@ -12,7 +15,7 @@
class FilterByUserMixin: # pylint: disable=too-few-public-methods
"""Filter queryset by ``request.user``."""

# A user should only see forms/projects that belong to him.
# A user should only see forms/projects that belong to them.
def get_queryset(self, request):
"""Returns queryset filtered by the `request.user`."""
queryset = super().get_queryset(request)
Expand All @@ -25,9 +28,33 @@ class XFormAdmin(FilterByUserMixin, VersionAdmin, admin.ModelAdmin):
"""Customise the XForm admin view."""

exclude = ("user",)
list_display = ("id_string", "downloadable", "shared")
list_display = ("id_string", "downloadable", "shared", "deleted_at")
search_fields = ("id_string", "title")
user_lookup_field = "user"
actions = ["restore_form"]

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:
try:
call_command("restore_form", xform.id)
restored_count += 1
except Exception as e:
self.message_user(
request,
_(f"Failed to restore form {xform.id_string}: {e}"),
level=messages.ERROR,
)
if restored_count > 0:
self.message_user(
request,
_(f"Successfully restored {restored_count} forms."),
level=messages.SUCCESS,
)

restore_form.short_description = _("Restore selected soft-deleted forms")


admin.site.register(XForm, XFormAdmin)
Expand Down

0 comments on commit b3e97a7

Please sign in to comment.