41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
from django.core.management.base import BaseCommand
|
|
from tqdm import tqdm
|
|
from fwd_api.models import SubscriptionRequestFile
|
|
from fwd_api.models.SemiAutoCorrection import SemiAutoCorrection
|
|
from fwd_api.exception.exceptions import InvalidException
|
|
|
|
# Mapping dictionary for reasons
|
|
REASON_MAP = {
|
|
'Invalid image': 'invalid_image',
|
|
'Missing information': 'missing_information',
|
|
'Too blurry text': 'too_blurry_text',
|
|
'Too small text': 'too_small_text',
|
|
'Handwritten': 'handwritten',
|
|
'Recheck': 'recheck',
|
|
}
|
|
|
|
class Command(BaseCommand):
|
|
help = 'Replace the reason field in SubscriptionRequestFile and SemiAutoCorrection based on the provided mapping dictionary'
|
|
|
|
def handle(self, *args, **options):
|
|
# Process SubscriptionRequestFile instances
|
|
self.update_reasons(SubscriptionRequestFile, "SubscriptionRequestFile")
|
|
|
|
# Process SemiAutoCorrection instances
|
|
self.update_reasons(SemiAutoCorrection, "SemiAutoCorrection")
|
|
|
|
self.stdout.write(self.style.SUCCESS('All applicable reasons updated successfully!'))
|
|
|
|
def update_reasons(self, model, model_name):
|
|
instances = model.objects.exclude(reason__isnull=True).exclude(reason='').iterator()
|
|
for instance in tqdm(instances, desc=f"Updating reasons in {model_name}"):
|
|
try:
|
|
original_reason = instance.reason
|
|
new_reason = REASON_MAP.get(original_reason)
|
|
if new_reason is not None:
|
|
instance.reason = new_reason
|
|
instance.save()
|
|
self.stdout.write(self.style.SUCCESS(f"Updated reason for {model_name} ID {instance.id}: {original_reason} -> {new_reason}"))
|
|
except Exception as e:
|
|
self.stdout.write(self.style.ERROR(f"Updated reason failed for {model_name} ID {instance.id} due to {e}"))
|
|
|