forked from Kamva-Academy/Kamva-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: add a command for deleting ashbaria redundant survey responses
- Loading branch information
1 parent
e853c01
commit 36c71fc
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
apps/fsm/management/commands/delete_ashbaria_redundant_survay_answer_sheets.py
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,32 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from apps.fsm.models.form import AnswerSheet | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Remove duplicate AnswerSheets for a given form_id, keeping only the first one' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--formid', type=int, help='Form ID to process') | ||
|
||
def handle(self, *args, **options): | ||
form_id = options['formid'] | ||
if not form_id: | ||
self.stdout.write(self.style.ERROR( | ||
'Please provide a form ID using --formid')) | ||
return | ||
|
||
answer_sheets = AnswerSheet.objects.filter( | ||
form_id=form_id).order_by('user_id', 'created_at') | ||
users_processed = set() | ||
deleted_count = 0 | ||
|
||
for answer_sheet in answer_sheets: | ||
if answer_sheet.user_id in users_processed: | ||
answer_sheet.delete() | ||
deleted_count += 1 | ||
else: | ||
users_processed.add(answer_sheet.user_id) | ||
|
||
self.stdout.write(self.style.SUCCESS( | ||
f'Successfully deleted {deleted_count} duplicate AnswerSheets for form ID {form_id}')) |