Browse Source
enhance transmitters section
enhance transmitters section
script to update madhhab and generation values enhance search query and filtering paramsmaster
3 changed files with 72 additions and 3 deletions
-
7apps/hadis/docs.py
-
49apps/hadis/management/commands/update_transmitters_data.py
-
19apps/hadis/views/transmitter.py
@ -0,0 +1,49 @@ |
|||
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.')) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue