You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
2.0 KiB
49 lines
2.0 KiB
import random
|
|
from django.core.management.base import BaseCommand
|
|
from apps.hadis.models.transmitter import Transmitters
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Update all transmitters with random generation (1-10) and random madhab'
|
|
|
|
def handle(self, *args, **options):
|
|
# We use the active madhab choices define in the Transmitters model
|
|
# Currently active choices in transmitter.py: SHIA, SUNNI, OTHER, UNKNOWN
|
|
madhab_choices = [
|
|
Transmitters.MadhhabChoices.SHIA,
|
|
Transmitters.MadhhabChoices.SUNNI,
|
|
Transmitters.MadhhabChoices.OTHER,
|
|
Transmitters.MadhhabChoices.UNKNOWN,
|
|
]
|
|
|
|
# Get all transmitters
|
|
transmitters = Transmitters.objects.all()
|
|
count = transmitters.count()
|
|
|
|
if count == 0:
|
|
self.stdout.write(self.style.WARNING("No Transmitters found in the database."))
|
|
return
|
|
|
|
self.stdout.write(self.style.WARNING(f'Starting update for {count} transmitters...'))
|
|
|
|
updated_count = 0
|
|
|
|
# It's better to iterate through the queryset and save each instance to trigger any save() logic if present
|
|
for transmitter in transmitters:
|
|
try:
|
|
# Assign random generation between 1-10
|
|
transmitter.generation = random.randint(1, 10)
|
|
|
|
# Assign random madhab from available choices
|
|
transmitter.madhhab = random.choice(madhab_choices)
|
|
|
|
# Save the record
|
|
transmitter.save()
|
|
updated_count += 1
|
|
|
|
if updated_count % 100 == 0:
|
|
self.stdout.write(f'Updated {updated_count}/{count}...')
|
|
|
|
except Exception as e:
|
|
self.stderr.write(self.style.ERROR(f'Error updating transmitter ID {transmitter.id}: {str(e)}'))
|
|
|
|
self.stdout.write(self.style.SUCCESS(f'Successfully updated {updated_count} transmitters.'))
|