agdar/ot/management/commands/populate_distraction_tolerance.py
Marwan Alwali a04817ef6e update
2025-11-02 19:25:08 +03:00

32 lines
1.2 KiB
Python

"""
Management command to populate distraction_tolerance for existing OT sessions.
"""
import random
from django.core.management.base import BaseCommand
from ot.models import OTSession
class Command(BaseCommand):
help = 'Populate distraction_tolerance field for existing OT sessions'
def handle(self, *args, **options):
self.stdout.write('Populating distraction_tolerance for existing OT sessions...')
# Get all OT sessions where distraction_tolerance is None
sessions = OTSession.objects.filter(distraction_tolerance__isnull=True)
count = sessions.count()
if count == 0:
self.stdout.write(self.style.SUCCESS('No sessions need updating. All sessions already have distraction_tolerance values.'))
return
# Update each session with a random value between 1 and 4
updated = 0
for session in sessions:
session.distraction_tolerance = random.randint(1, 4)
session.save(update_fields=['distraction_tolerance'])
updated += 1
self.stdout.write(self.style.SUCCESS(f'✓ Successfully updated {updated} OT sessions with distraction_tolerance values (1-4)'))