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.
64 lines
1.8 KiB
64 lines
1.8 KiB
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
|
|
from apps.hadis.models import HadisCorrection # adjust import if app/model name is different
|
|
|
|
|
|
def normalize_translation_items(value):
|
|
"""
|
|
Convert items like:
|
|
[{"lang": "en", "text": "..."}, {"lang": "fa", "text": "..."}]
|
|
to:
|
|
[{"language_code": "en", "text": "..."}, {"language_code": "fa", "text": "..."}]
|
|
|
|
If already correct, leave as-is. Ignore items without text.
|
|
"""
|
|
if not value:
|
|
return []
|
|
|
|
if not isinstance(value, list):
|
|
# Unexpected format, just return as-is
|
|
return value
|
|
|
|
cleaned = []
|
|
for item in value:
|
|
if not isinstance(item, dict):
|
|
continue
|
|
|
|
# Prefer existing language_code, else use lang
|
|
lang = item.get("language_code") or item.get("lang")
|
|
text = item.get("text")
|
|
|
|
if not lang or not text:
|
|
continue
|
|
|
|
cleaned.append(
|
|
{
|
|
"language_code": str(lang),
|
|
"text": text,
|
|
}
|
|
)
|
|
|
|
return cleaned
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Rename 'lang' to 'language_code' in HadisCorrection.translation JSON list entries."
|
|
|
|
@transaction.atomic
|
|
def handle(self, *args, **options):
|
|
qs = HadisCorrection.objects.all()
|
|
total = qs.count()
|
|
self.stdout.write(f"Found {total} HadisCorrection objects")
|
|
|
|
changed = 0
|
|
for obj in qs:
|
|
old = obj.translation
|
|
new = normalize_translation_items(old)
|
|
|
|
if new != old:
|
|
obj.translation = new
|
|
obj.save(update_fields=["translation"])
|
|
changed += 1
|
|
|
|
self.stdout.write(self.style.SUCCESS(f"Updated {changed} HadisCorrection objects"))
|