From d3f1ae3d11015618406a1cd1796a58033ff24c4b Mon Sep 17 00:00:00 2001 From: N3WT DE COMPET Date: Wed, 12 Feb 2025 17:44:28 +0100 Subject: [PATCH] chore: Ajout d'un mode test au lancement du serveur pour ajouter des datas de test --- .../commands/init_school_configuration.py | 160 ++++++++++++++++++ .../{init_data.py => init_school_fees.py} | 4 +- Back-End/start.py | 15 +- docker-compose.yml | 1 + 4 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 Back-End/School/management/commands/init_school_configuration.py rename Back-End/School/management/commands/{init_data.py => init_school_fees.py} (97%) diff --git a/Back-End/School/management/commands/init_school_configuration.py b/Back-End/School/management/commands/init_school_configuration.py new file mode 100644 index 0000000..4402808 --- /dev/null +++ b/Back-End/School/management/commands/init_school_configuration.py @@ -0,0 +1,160 @@ +from django.core.management.base import BaseCommand +from Auth.models import Profile +from School.models import Speciality, Teacher, SchoolClass + +class Command(BaseCommand): + help = 'Initialize or update Fees and Discounts' + + def handle(self, *args, **kwargs): + self.create_or_update_specialities() + self.create_or_update_teachers() + self.create_or_update_schoolClasses() + + def create_or_update_specialities(self): + specialities_data = [ + { + "name": "GROUPE", + "color_code": "#FF0000" + }, + { + "name": "MATHS", + "color_code": "#0a98f0" + }, + { + "name": "ANGLAIS", + "color_code": "#f708d7" + }, + { + "name": "FRANCAIS", + "color_code": "#04f108" + }, + { + "name": "HISTOIRE", + "color_code": "#ffb005" + }, + { + "name": "SPORT", + "color_code": "#bbb9b9" + } + ] + + for speciality_data in specialities_data: + Speciality.objects.update_or_create( + name=speciality_data["name"], + defaults=speciality_data + ) + self.stdout.write(self.style.SUCCESS('Specialities initialized or updated successfully')) + + def create_or_update_teachers(self): + teachers_data = [ + { + "last_name": "DUMBLEDORE", + "first_name": "Albus", + "email": "albus.dumbledore@gmail.com", + "specialities": ["GROUPE"], + "droit": 1 + }, + { + "last_name": "ROGUE", + "first_name": "Severus", + "email": "severus.rogue@gmail.com", + "specialities": ["ANGLAIS"], + "droit": 0 + }, + { + "last_name": "MC GONAGALL", + "first_name": "Minerva", + "email": "minerva.mcgonagall@gmail.com", + "specialities": ["MATHS", "HISTOIRE"], + "droit": 0 + }, + { + "last_name": "CHOURAVE", + "first_name": "Pomona", + "email": "pomona.chourave@gmail.com", + "specialities": ["MATHS", "FRANCAIS", "SPORT"], + "droit": 0 + } + ] + + for teacher_data in teachers_data: + specialities = teacher_data.pop("specialities") + email = teacher_data["email"] + droit = teacher_data.pop("droit") + + # Create or update the user profile + user, created = Profile.objects.update_or_create( + email=email, + defaults={ + "username": email, + "email": email, + "is_active": True, + "password": "Provisoire01!", + "droit": droit + } + ) + if created: + user.set_password("Provisoire01!") + user.save() + + # Create or update the teacher + teacher, created = Teacher.objects.update_or_create( + email=email, + defaults={**teacher_data, "associated_profile_id": user.id} + ) + teacher.specialities.set(Speciality.objects.filter(name__in=specialities)) + teacher.save() + + self.stdout.write(self.style.SUCCESS('Teachers initialized or updated successfully')) + + def create_or_update_schoolClasses(self): + school_classes_data = [ + { + "atmosphere_name": "Classe A", + "age_range": "3-6", + "number_of_students": 14, + "teaching_language": "", + "school_year": "2024-2025", + "levels": [2, 3, 4], + "type": 1, + "time_range": ["08:30", "17:30"], + "opening_days": [1, 2, 4, 5], + "teachers": [2] # ID of Severus Rogue + }, + { + "atmosphere_name": "Classe B", + "age_range": "2-3", + "number_of_students": 5, + "teaching_language": "", + "school_year": "2024-2025", + "levels": [1], + "type": 1, + "time_range": ["08:30", "17:30"], + "opening_days": [1, 2, 4, 5], + "teachers": [3] # ID of Minerva McGonagall + }, + { + "atmosphere_name": "Classe C", + "age_range": "6-12", + "number_of_students": 21, + "teaching_language": "", + "school_year": "2024-2025", + "levels": [5, 6, 7, 8, 9], + "type": 1, + "time_range": ["08:30", "17:30"], + "opening_days": [1, 2, 4, 5], + "teachers": [4] # ID of Pomona Chourave + } + ] + + for class_data in school_classes_data: + teachers_ids = class_data.pop("teachers") + school_class, created = SchoolClass.objects.update_or_create( + atmosphere_name=class_data["atmosphere_name"], + school_year=class_data["school_year"], + defaults=class_data + ) + school_class.teachers.set(teachers_ids) + school_class.save() + + self.stdout.write(self.style.SUCCESS('SchoolClasses initialized or updated successfully')) \ No newline at end of file diff --git a/Back-End/School/management/commands/init_data.py b/Back-End/School/management/commands/init_school_fees.py similarity index 97% rename from Back-End/School/management/commands/init_data.py rename to Back-End/School/management/commands/init_school_fees.py index 2dee892..9c36f6e 100644 --- a/Back-End/School/management/commands/init_data.py +++ b/Back-End/School/management/commands/init_school_fees.py @@ -1,4 +1,5 @@ from django.core.management.base import BaseCommand +from Auth.models import Profile from School.models import Fee, Discount, FeeType, DiscountType class Command(BaseCommand): @@ -82,4 +83,5 @@ class Command(BaseCommand): defaults=discount_data ) - self.stdout.write(self.style.SUCCESS('Discounts initialized or updated successfully')) \ No newline at end of file + self.stdout.write(self.style.SUCCESS('Discounts initialized or updated successfully')) + \ No newline at end of file diff --git a/Back-End/start.py b/Back-End/start.py index c7de8d5..7ca950e 100644 --- a/Back-End/start.py +++ b/Back-End/start.py @@ -10,6 +10,8 @@ def run_command(command): print(f"stderr: {stderr.decode()}") return process.returncode +test_mode = os.getenv('TEST_MODE', 'False') == 'True' + commands = [ ["python", "manage.py", "collectstatic", "--noinput"], ["python", "manage.py", "flush", "--noinput"], @@ -20,14 +22,23 @@ commands = [ ["python", "manage.py", "makemigrations", "School", "--noinput"], ["python", "manage.py", "migrate", "--noinput"], ["python", "manage.py", "init_payment_plans"], - ["python", "manage.py", "init_payment_modes"], - ["python", "manage.py", "init_data"] + ["python", "manage.py", "init_payment_modes"] +] + +test_commands = [ + ["python", "manage.py", "init_school_configuration"], + ["python", "manage.py", "init_school_fees"] ] for command in commands: if run_command(command) != 0: exit(1) +if test_mode: + for test_command in test_commands: + if run_command(test_command) != 0: + exit(1) + # Lancer les processus en parallèle processes = [ subprocess.Popen(["python", "manage.py", "runserver", "0.0.0.0:8080"]), diff --git a/docker-compose.yml b/docker-compose.yml index cf8ff41..0c10cff 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,6 +47,7 @@ services: - ./Back-End:/Back-End environment: - TZ=Europe/Paris + - TEST_MODE=True links: - "database:database" - "redis:redis"