mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
feat: Peuplement de la BDD avec les JSON d'entrée [#16]
This commit is contained in:
49
Back-End/School/signals.py
Normal file
49
Back-End/School/signals.py
Normal file
@ -0,0 +1,49 @@
|
||||
import json
|
||||
import os
|
||||
from django.db.models.signals import post_migrate
|
||||
from django.dispatch import receiver
|
||||
from School.models import Domain, Category, Competency
|
||||
|
||||
@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, created = 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, created = 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.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}")
|
||||
Reference in New Issue
Block a user