From dad644b5f275d329ec19b70d46028dbf5adc1643 Mon Sep 17 00:00:00 2001 From: mohsentaba Date: Tue, 30 Jun 2026 13:55:52 +0330 Subject: [PATCH] data synced for admin panel --- .../commands/fill_english_translations.py | 65 +++++++++++++++++++ apps/course/serializers/admin.py | 9 +-- 2 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 apps/course/management/commands/fill_english_translations.py diff --git a/apps/course/management/commands/fill_english_translations.py b/apps/course/management/commands/fill_english_translations.py new file mode 100644 index 0000000..d946680 --- /dev/null +++ b/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.")) diff --git a/apps/course/serializers/admin.py b/apps/course/serializers/admin.py index 1a3c7f3..45ab464 100644 --- a/apps/course/serializers/admin.py +++ b/apps/course/serializers/admin.py @@ -269,13 +269,8 @@ class AdminCourseDetailSerializer(serializers.ModelSerializer): def to_representation(self, 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 first_prof = instance.professors.first() ret['professor'] = first_prof.id if first_prof else None