2 changed files with 43 additions and 55 deletions
@ -1,76 +1,64 @@ |
|||
from django.core.management.base import BaseCommand |
|||
from django.db import transaction |
|||
|
|||
from apps.hadis.models import Hadis |
|||
from apps.hadis.models import HadisCorrection # adjust import if app/model name is different |
|||
|
|||
|
|||
def normalize_translation(value): |
|||
def normalize_translation_items(value): |
|||
""" |
|||
Convert: |
|||
{"en": "translation", "fa": "ترجمه"} |
|||
Convert items like: |
|||
[{"lang": "en", "text": "..."}, {"lang": "fa", "text": "..."}] |
|||
to: |
|||
[{"text": "translation", "language_code": "en"}, |
|||
{"text": "ترجمه", "language_code": "fa"}] |
|||
[{"language_code": "en", "text": "..."}, {"language_code": "fa", "text": "..."}] |
|||
|
|||
Also clean existing list entries by: |
|||
- ensuring only 'text' + 'language_code' |
|||
- dropping any 'title' key |
|||
If already correct, leave as-is. Ignore items without text. |
|||
""" |
|||
if value is None: |
|||
if not value: |
|||
return [] |
|||
|
|||
# If it's already a list, clean its items |
|||
if isinstance(value, list): |
|||
cleaned = [] |
|||
for item in value: |
|||
if not isinstance(item, dict): |
|||
continue |
|||
lang = item.get("language_code") |
|||
# prefer 'text', fallback to 'title' |
|||
text = item.get("text") or item.get("title") |
|||
if not lang or not text: |
|||
continue |
|||
cleaned.append( |
|||
{ |
|||
"language_code": str(lang), |
|||
"text": text, |
|||
} |
|||
) |
|||
return cleaned |
|||
|
|||
# Old dict format: {"en": "...", "fa": "..."} |
|||
if isinstance(value, dict): |
|||
result = [] |
|||
for lang_code, text in value.items(): |
|||
if text is None or text == "": |
|||
continue |
|||
result.append( |
|||
{ |
|||
"language_code": str(lang_code), |
|||
"text": text, |
|||
} |
|||
) |
|||
return result |
|||
|
|||
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 = "Reformat Hadis.translation from {'en': '...', 'fa': '...'} to [{'text': '...', 'language_code': '...'}, ...]" # noqa: E501 |
|||
help = "Rename 'lang' to 'language_code' in HadisCorrection.translation JSON list entries." |
|||
|
|||
@transaction.atomic |
|||
def handle(self, *args, **options): |
|||
qs = Hadis.objects.all() |
|||
qs = HadisCorrection.objects.all() |
|||
total = qs.count() |
|||
self.stdout.write(f"Found {total} hadis objects") |
|||
self.stdout.write(f"Found {total} HadisCorrection objects") |
|||
|
|||
changed = 0 |
|||
for hadis in qs: |
|||
old = hadis.translation |
|||
new = normalize_translation(old) |
|||
for obj in qs: |
|||
old = obj.translation |
|||
new = normalize_translation_items(old) |
|||
|
|||
if new != old: |
|||
hadis.translation = new |
|||
hadis.save(update_fields=["translation"]) |
|||
obj.translation = new |
|||
obj.save(update_fields=["translation"]) |
|||
changed += 1 |
|||
|
|||
self.stdout.write(self.style.SUCCESS(f"Updated {changed} hadis objects")) |
|||
self.stdout.write(self.style.SUCCESS(f"Updated {changed} HadisCorrection objects")) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue