Skip to content

Commit

Permalink
Fix: add a command for deleting ashbaria redundant survey responses
Browse files Browse the repository at this point in the history
  • Loading branch information
AmooHashem committed Jan 19, 2025
1 parent e853c01 commit 36c71fc
Showing 1 changed file with 32 additions and 0 deletions.
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}'))

0 comments on commit 36c71fc

Please sign in to comment.