Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WC2-615 Delete instances and entities: Add option to filter on entity_type_id #1861

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions iaso/management/commands/delete_all_instances_and_entities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.core.management.base import BaseCommand
from django.db import transaction

from iaso.models import Account, Entity, Instance, InstanceFile, StorageDevice, StorageLogEntry
from iaso.models import Account, Entity, EntityType, Instance, InstanceFile, StorageDevice, StorageLogEntry


class Command(BaseCommand):
Expand All @@ -15,6 +15,7 @@ class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument("--account", type=int, dest="account_id")
parser.add_argument("--entity_type_id", type=int, dest="entity_type_id")
parser.add_argument(
"--dry-run",
action="store_true",
Expand Down Expand Up @@ -43,15 +44,26 @@ def handle(self, *args, **options):
account = Account.objects.get(pk=account_id)
project_ids = list(account.project_set.values_list("id", flat=True))

entity_type_id = options.get("entity_type_id")
if entity_type_id:
entity_type = EntityType.objects.get(pk=entity_type_id, account=account)
self.stdout.write(f"NOTE: Deleting only for entity type {entity_type}")

with transaction.atomic():
self.stdout.write("--------------")
self.stdout.write(f"Deleting all form submissions and entities for account {account.name}")
self.stdout.write(f"Deleting form submissions and entities for account {account.name}")
if entity_type_id:
self.stdout.write(f"and entity type {entity_type}")
self.stdout.write("--------------")
self.stdout.write()

self.stdout.write("Deleting form submissions...")
instances_to_delete = Instance.objects.filter(form__projects__id__in=project_ids)
entities_to_delete = Entity.objects_include_deleted.filter(account=account)
if entity_type_id:
instances_to_delete = Instance.objects.filter(entity__entity_type=entity_type)
entities_to_delete = Entity.objects_include_deleted.filter(account=account, entity_type=entity_type)
else:
instances_to_delete = Instance.objects.filter(form__projects__id__in=project_ids)
entities_to_delete = Entity.objects_include_deleted.filter(account=account)

self.delete_resources(
"\tfile instances",
Expand All @@ -73,7 +85,10 @@ def handle(self, *args, **options):
self.stdout.write("DONE.")

self.stdout.write("Deleting storages...")
storages_to_delete = StorageDevice.objects.filter(account=account)
if entity_type_id:
storages_to_delete = StorageDevice.objects.filter(account=account, entity__entity_type=entity_type)
else:
storages_to_delete = StorageDevice.objects.filter(account=account)
self.delete_resources(
"\tStorageLogEntry",
StorageLogEntry.objects.filter(device__in=storages_to_delete),
Expand Down
20 changes: 14 additions & 6 deletions plugins/wfp/templates/delete_all_instances_and_entities.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ <h1>
<form method="post">
{% csrf_token %}

<label for="dry_run">Dry Run:</label><br />
<input type="checkbox" id="dry_run" name="dry_run" /><br />

<label for="account">Account:</label><br />
<input type="text" id="account" name="account" /><br />

<label for="dry_run">Dry Run:</label>
<input type="checkbox" id="dry_run" name="dry_run" />
<br />
<br />
<label for="account">Account ID:</label>
<input type="text" id="account" name="account" />
<br />
<br />
<label for="entity_type_id">
[Optional] only delete entities for a specific entity type:
</label>
<input type="text" id="entity_type_id" name="entity_type_id" />
<br />
<br />
<input type="submit" value="DELETE EVERYTHING!" />
</form>
</body>
Expand Down
2 changes: 2 additions & 0 deletions plugins/wfp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def delete_beneficiaries_analytics(request):
def delete_all_instances_and_entities(request):
dry_run = request.POST.get("dry_run", False)
account = request.POST.get("account", None)
entity_type_id = request.POST.get("entity_type_id", None)

if request.method == "POST":
out = io.StringIO()
call_command(
"delete_all_instances_and_entities",
dry_run=dry_run == "on",
account=account,
entity_type_id=entity_type_id,
stdout=out,
)
output = out.getvalue()
Expand Down
Loading