-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Added a form deletion script for genie requests #35514
Changes from 2 commits
b000b30
9ca88fd
4fccff9
0537ba7
620c669
1d208a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from django.core.management.base import BaseCommand | ||
import csv | ||
import itertools | ||
from dimagi.utils.chunked import chunked | ||
from corehq.form_processor.models import XFormInstance | ||
|
||
|
||
INDEX_FORM_ID = 1 | ||
CHUNK_SIZE = 100 | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument('domain', help='Domain name that owns the forms to be deleted') | ||
parser.add_argument('filename', help='path to the CSV file') | ||
parser.add_argument('--resume_id', help='form ID to start at, within the CSV file') | ||
|
||
def handle(self, domain, filename, resume_id=None, **options): | ||
# expects the filename to have a CSV with a header containing "Deletion Status" and "Form ID" fields | ||
with open(filename) as csvfile: | ||
reader = csv.reader(csvfile, delimiter=',') | ||
self._process_rows(reader, domain, resume_id) | ||
|
||
def _process_rows(self, rows, domain, resume_id): | ||
next(rows) # skip header line | ||
|
||
num_deleted = 0 | ||
|
||
if resume_id: | ||
print('resuming at: ', resume_id) | ||
rows = itertools.dropwhile(lambda row: row[INDEX_FORM_ID] != resume_id, rows) | ||
|
||
print('Starting form deletion') | ||
for chunk in chunked(rows, CHUNK_SIZE): | ||
form_ids = [row[INDEX_FORM_ID] for row in chunk] | ||
|
||
try: | ||
deleted_form_ids = set(XFormInstance.objects.hard_delete_forms( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel squeemish about hard-delete, though obviously it is what we're being asked to do. An alternate pattern would be to soft delete and mark for automatic hard-deletion in 90 days. But that is probably overkill for the present situation. Maybe what I would ask is just to rename the command to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, I can definitely do that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed with 4fccff9 |
||
domain, form_ids, return_ids=True)) | ||
except Exception: | ||
print('failed during processing of: ', form_ids) | ||
raise | ||
|
||
for form_id in form_ids: | ||
if form_id in deleted_form_ids: | ||
print('Deleted: ', form_id) | ||
else: | ||
print('Not found:', form_id) | ||
|
||
num_deleted += len(deleted_form_ids) | ||
|
||
print(f'Complete -- removed {num_deleted} forms') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually use the "Deletion Status" field? I only see the "Form ID" field being used. My thinking here is if there is a Deletion Status field with values under it, someone's going to expect those values to be used for something, and if we're not going to use them, then we should require the CSV not have that column.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is working with the CSV that Enveritas submitted to us. If I simplify this script to expect a sheet without that column, then I'd need to write another script to port the Envertias CSV over. Do you think that's something we should do long term?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 4fccff9. As mentioned offline, I think the process going forward should be that customers making this request provide us a CSV file that only contains a single column representing form ID. In this case, the customer provided an initial column called "Deletion Status" which only seems to contain
ready_to_request
. However, if one of the rows contained something likedo_not_delete
instead, we wouldn't want to have the responsibility for interpreting that field.