import json import os from django.db.models.signals import post_migrate from django.dispatch import receiver from School.models import Domain, Category, Competency, PaymentModeType, PaymentPlanType @receiver(post_migrate) def school_post_migrate(sender, **kwargs): if sender.name != "School": return # Chemin absolu vers le répertoire Back-End backend_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Chemins vers les fichiers JSON json_files = [ ("Cycle1.json", 1), ("Cycle2.json", 2), ("Cycle3.json", 3), ("Cycle4.json", 4), ] for file_name, cycle in json_files: json_file_path = os.path.join(backend_dir, "competences", file_name) if not os.path.exists(json_file_path): print(f"Fichier JSON introuvable : {json_file_path}") continue with open(json_file_path, 'r', encoding='utf-8') as file: data = json.load(file) for domain_data in data['domaines']: # Vérifiez si le domaine existe déjà domain, _ = Domain.objects.get_or_create(name=domain_data['nom'], cycle=cycle) for category_data in domain_data['categories']: # Vérifiez si la catégorie existe déjà category, _ = Category.objects.get_or_create(name=category_data['nom'], domain=domain) for competency_data in category_data['competences']: # Vérifiez si la compétence existe déjà competency, _ = Competency.objects.get_or_create( name=competency_data['nom'], end_of_cycle=competency_data.get('fin_cycle', False), level=competency_data.get('niveau'), category=category ) print(f"Données importées depuis : {json_file_path}") payment_mode_types = [ {"code": "SEPA", "label": "Prélèvement SEPA"}, {"code": "TRANSFER", "label": "Virement"}, {"code": "CHECK", "label": "Chèque"}, {"code": "CASH", "label": "Espèce"}, ] for mode in payment_mode_types: PaymentModeType.objects.get_or_create(code=mode["code"], defaults={"label": mode["label"]}) # ... après la création des PaymentModeType ... payment_plan_types = [ {"code": "ONE_TIME", "label": "1 fois"}, {"code": "THREE_TIMES", "label": "3 fois"}, {"code": "TEN_TIMES", "label": "10 fois"}, {"code": "TWELVE_TIMES", "label": "12 fois"}, ] for plan in payment_plan_types: PaymentPlanType.objects.get_or_create(code=plan["code"], defaults={"label": plan["label"]})