import json import os from django.db.models.signals import post_migrate from django.dispatch import receiver from Establishment.models import Establishment from School.models import Domain, Category, Competency, EstablishmentCompetency @receiver(post_migrate) def load_json_data(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}")