Browse Source

data synced for admin panel

master
Mohsen Taba 3 weeks ago
parent
commit
dad644b5f2
  1. 65
      apps/course/management/commands/fill_english_translations.py
  2. 9
      apps/course/serializers/admin.py

65
apps/course/management/commands/fill_english_translations.py

@ -0,0 +1,65 @@
from django.core.management.base import BaseCommand
from django.apps import apps
from django.db import models
class Command(BaseCommand):
help = "Populate missing 'en' translations with other available language data for JSONFields"
def handle(self, *args, **options):
target_app_labels = ['course', 'quiz', 'blog', 'hadis', 'library']
updated_records = 0
for app_label in target_app_labels:
try:
app_config = apps.get_app_config(app_label)
except LookupError:
continue
for model in app_config.get_models():
json_fields = [f for f in model._meta.get_fields() if isinstance(f, models.JSONField)]
if not json_fields:
continue
for instance in model.objects.all():
changed = False
for field in json_fields:
val = getattr(instance, field.name)
if isinstance(val, list):
has_en = False
fallback_text = None
valid = True
for item in val:
if not isinstance(item, dict) or 'language_code' not in item or 'title' not in item:
valid = False
break
title_val = item.get('title')
if isinstance(title_val, str):
title_str = title_val.strip()
elif title_val is not None:
title_str = str(title_val).strip()
else:
title_str = ''
if item.get('language_code') == 'en' and title_str:
has_en = True
elif title_str:
if not fallback_text:
fallback_text = title_val
if valid and not has_en and fallback_text:
val.append({
"language_code": "en",
"title": fallback_text
})
setattr(instance, field.name, val)
changed = True
if changed:
instance.save()
updated_records += 1
self.stdout.write(f"Updated {model.__name__} ID {instance.pk}")
self.stdout.write(self.style.SUCCESS(f"Finished updating {updated_records} records."))

9
apps/course/serializers/admin.py

@ -269,13 +269,8 @@ class AdminCourseDetailSerializer(serializers.ModelSerializer):
def to_representation(self, instance): def to_representation(self, instance):
ret = super().to_representation(instance) ret = super().to_representation(instance)
lang = get_request_lang(self)
ret['title'] = get_localized_field(lang, instance.title)
ret['slug'] = get_localized_field(lang, instance.slug)
ret['level'] = get_localized_field(lang, instance.level)
ret['status'] = get_localized_field(lang, instance.status)
ret['description'] = get_localized_field(lang, instance.description)
ret['short_description'] = get_localized_field(lang, instance.short_description)
# Do not extract localized field strings for these fields.
# The admin panel edit form requires the raw JSON arrays to populate multilingual inputs.
# Inject legacy fields # Inject legacy fields
first_prof = instance.professors.first() first_prof = instance.professors.first()
ret['professor'] = first_prof.id if first_prof else None ret['professor'] = first_prof.id if first_prof else None

Loading…
Cancel
Save