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'))