-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add management command to restore soft deleted form (#2744)
* add ability to restore soft deleted XForm * add management command to restor soft deleted form * add test * update docs * add ability to restore form in Django admin * fix lint warnings, import error * fix lint warnings * update docs * fix lint warning invalid-name * update XForm django admin * add id to search fields * update XForm admin * disable delete_selected action for XForm admin * update XForm admin * add comment * re-enable deleted_selected action * fix lint warnings * update docs * update docs * update docs * add id to XForm admin search field * update XForm admin user messages * override delete_queryset for XFormAdmin to soft delete * soft-delete XForm deleted from the admin detail page soft-delete XForm deleted from the admin detail page using the Delete button * enhance XForm admin * enhance XForm admin messages * enhance XForm admin * disable VersionAdmin for XFormAdmin * update XForm admin user message
- Loading branch information
1 parent
1fddd09
commit 47d82b6
Showing
9 changed files
with
370 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
Management Commands | ||
=================== | ||
|
||
The following custom Django management commands are available: | ||
|
||
Regenerate submission JSON | ||
-------------------------- | ||
|
||
Regenerates the JSON for all submissions of a form. | ||
|
||
This is useful in the case where the JSON was saved incorrectly due to some bug when parsing the XML or when saving metadata. | ||
|
||
The form is identified by its ID. | ||
|
||
.. code-block:: bash | ||
python manage.py regenerate_submission_json form_id1 form_id2 | ||
Restore soft deleted form | ||
------------------------- | ||
|
||
Restores a soft deleted form. The form is identified by its ID. | ||
|
||
.. code-block:: bash | ||
python manage.py restore_form form_id | ||
You can also restore a form in Django admin interface: | ||
|
||
1. **Navigate to XForms**: Go to the XForm section in the Django admin interface. | ||
|
||
2. **Select Forms**: Select the soft-deleted forms you want to restore. | ||
|
||
3. **Run Action**: Choose the "Restore selected soft-deleted forms" action from the dropdown menu and click "Go". | ||
|
||
|
||
Soft delete user | ||
---------------- | ||
|
||
Softs deletes a user. The user is identified by their username and email | ||
|
||
.. code-block:: bash | ||
python manage.py delete_users --user_details username1:email username2:email |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
Restore a soft-deleted XForm object. | ||
""" | ||
|
||
from django.core.management.base import BaseCommand, CommandError | ||
|
||
from onadata.apps.logger.models import XForm | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Management command to restore soft-deleted XForm objects. | ||
Usage: | ||
python manage.py restore_form <form_id> | ||
""" | ||
|
||
help = "Restores a soft-deleted XForm." | ||
|
||
def add_arguments(self, parser): | ||
# Add an argument to specify the form ID | ||
parser.add_argument( | ||
"form_id", | ||
type=int, | ||
help="The ID of the soft-deleted form to restore", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
form_id = options["form_id"] | ||
|
||
try: | ||
# Retrieve the soft-deleted form | ||
xform = XForm.objects.get(pk=form_id) | ||
|
||
if xform.deleted_at is None: | ||
raise CommandError(f"Form with ID {form_id} is not soft-deleted") | ||
|
||
# Perform the restoration | ||
self.stdout.write(f"Restoring form with ID {form_id}...") | ||
was_deleted_by = xform.deleted_by.username if xform.deleted_by else None | ||
# was_deleted_at in the format Nov. 1, 2021, HH:MM UTC | ||
was_deleted_at = xform.deleted_at.strftime("%b. %d, %Y, %H:%M UTC") | ||
xform.restore() | ||
|
||
# Display success message | ||
success_msg = ( | ||
f"Successfully restored form '{xform.id_string}' with " | ||
f"ID {form_id} deleted by {was_deleted_by} at {was_deleted_at}." | ||
) | ||
self.stdout.write(self.style.SUCCESS(success_msg)) | ||
|
||
except XForm.DoesNotExist as exc: | ||
raise CommandError(f"Form with ID {form_id} does not exist") from exc | ||
|
||
except Exception as exc: | ||
raise CommandError( | ||
f"An error occurred while restoring the form: {exc}" | ||
) from exc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.