mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Gestion multi-profil multi-école
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth.models import User
|
||||
from Subscriptions.models import (
|
||||
RegistrationForm,
|
||||
Student,
|
||||
@ -10,9 +9,8 @@ from Subscriptions.models import (
|
||||
RegistrationTemplateMaster,
|
||||
RegistrationTemplate
|
||||
)
|
||||
from Auth.models import Profile
|
||||
from Auth.models import Profile, ProfileRole
|
||||
from School.models import (
|
||||
Establishment,
|
||||
FeeType,
|
||||
Speciality,
|
||||
Teacher,
|
||||
@ -20,8 +18,7 @@ from School.models import (
|
||||
PaymentMode,
|
||||
PaymentModeType,
|
||||
PaymentPlan,
|
||||
PaymentPlanType,
|
||||
StructureType,
|
||||
PaymentPlanType,
|
||||
DiscountType
|
||||
)
|
||||
from django.utils import timezone
|
||||
@ -34,6 +31,19 @@ from faker import Faker
|
||||
import random
|
||||
import json
|
||||
|
||||
from School.serializers import (
|
||||
FeeSerializer,
|
||||
DiscountSerializer,
|
||||
PaymentModeSerializer,
|
||||
PaymentPlanSerializer,
|
||||
SpecialitySerializer,
|
||||
TeacherSerializer,
|
||||
SchoolClassSerializer
|
||||
)
|
||||
from Auth.serializers import ProfileSerializer, ProfileRoleSerializer
|
||||
from Establishment.serializers import EstablishmentSerializer
|
||||
from Subscriptions.serializers import RegistrationFormSerializer, GuardianSerializer
|
||||
|
||||
# Définir le chemin vers le dossier mock_datas
|
||||
MOCK_DATAS_PATH = os.path.join(settings.BASE_DIR, 'School', 'management', 'mock_datas')
|
||||
|
||||
@ -41,176 +51,306 @@ class Command(BaseCommand):
|
||||
help = 'Initialise toutes les données mock'
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
self.create_or_update_establishments()
|
||||
self.create_or_update_fees()
|
||||
self.create_or_update_discounts()
|
||||
self.create_or_update_payment_modes()
|
||||
self.create_or_update_payment_plans()
|
||||
self.create_or_update_specialities()
|
||||
self.create_or_update_teachers()
|
||||
self.create_or_update_school_classes()
|
||||
self.create_or_update_registration_file_group()
|
||||
self.create_register_form()
|
||||
self.init_establishments()
|
||||
self.init_profiles()
|
||||
self.init_fees()
|
||||
self.init_discounts()
|
||||
self.init_payment_modes()
|
||||
self.init_payment_plans()
|
||||
self.init_specialities()
|
||||
self.init_teachers()
|
||||
self.init_guardians()
|
||||
self.init_school_classes()
|
||||
self.init_file_group()
|
||||
self.init_register_form()
|
||||
|
||||
def load_data(self, filename):
|
||||
with open(os.path.join(MOCK_DATAS_PATH, filename), 'r') as file:
|
||||
return json.load(file)
|
||||
|
||||
def create_or_update_establishments(self):
|
||||
def init_establishments(self):
|
||||
establishments_data = self.load_data('establishments.json')
|
||||
|
||||
self.establishments = []
|
||||
for establishment_data in establishments_data:
|
||||
establishment, created = Establishment.objects.update_or_create(
|
||||
name=establishment_data["name"],
|
||||
defaults=establishment_data
|
||||
)
|
||||
self.establishments.append(establishment)
|
||||
if created:
|
||||
self.stdout.write(self.style.SUCCESS(f'Establishment {establishment.name} created successfully'))
|
||||
serializer = EstablishmentSerializer(data=establishment_data)
|
||||
if serializer.is_valid():
|
||||
establishment = serializer.save()
|
||||
self.establishments.append(establishment)
|
||||
self.stdout.write(self.style.SUCCESS(f'Establishment {establishment.name} created or updated successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS(f'Establishment {establishment.name} updated successfully'))
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for establishment: {serializer.errors}'))
|
||||
|
||||
def create_or_update_fees(self):
|
||||
def init_profiles(self):
|
||||
profiles_data = self.load_data('profiles.json')
|
||||
|
||||
for profile_data in profiles_data:
|
||||
# Randomize the number of roles to create (between 1 et 3)
|
||||
num_roles = random.randint(1, 3)
|
||||
selected_roles = []
|
||||
|
||||
for _ in range(num_roles):
|
||||
establishment = random.choice(self.establishments)
|
||||
role_type = random.choice([ProfileRole.RoleType.PROFIL_ECOLE, ProfileRole.RoleType.PROFIL_ADMIN, ProfileRole.RoleType.PROFIL_PARENT])
|
||||
|
||||
# Ensure no duplicate ADMIN role for the same establishment
|
||||
if role_type == ProfileRole.RoleType.PROFIL_ADMIN:
|
||||
if any(role['role_type'] == ProfileRole.RoleType.PROFIL_ADMIN and role['establishment'] == establishment.id for role in selected_roles):
|
||||
continue
|
||||
|
||||
selected_roles.append({
|
||||
"role_type": role_type,
|
||||
"establishment": establishment.id,
|
||||
"establishment_name": establishment.name
|
||||
})
|
||||
|
||||
# Generate email based on the selected roles and establishment
|
||||
role_types = '-'.join([f"{ProfileRole.RoleType(role['role_type']).name.replace('PROFIL_', '')}_{role['establishment_name'].replace(' ', '')}" for role in selected_roles])
|
||||
email = f"{profile_data['username']}-{role_types}@exemple.com"
|
||||
|
||||
# Add email to profile data
|
||||
profile_data['email'] = email
|
||||
|
||||
serializer = ProfileSerializer(data=profile_data)
|
||||
if serializer.is_valid():
|
||||
profile = serializer.save()
|
||||
profile.set_password(profile_data["password"])
|
||||
profile.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Profile {profile.email} created successfully'))
|
||||
|
||||
# Create or update the profile role for each selected role using ProfileRoleSerializer
|
||||
for role in selected_roles:
|
||||
role_data = {
|
||||
"profile": profile.id,
|
||||
"establishment": role["establishment"],
|
||||
"role_type": role["role_type"],
|
||||
"is_active": True
|
||||
}
|
||||
role_serializer = ProfileRoleSerializer(data=role_data)
|
||||
if role_serializer.is_valid():
|
||||
role_serializer.save()
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile role: {role_serializer.errors}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile: {serializer.errors}'))
|
||||
|
||||
def init_fees(self):
|
||||
fees_data = self.load_data('fees.json')
|
||||
|
||||
for fee_data in fees_data:
|
||||
establishment = random.choice(self.establishments)
|
||||
print(f'establishment : {establishment}')
|
||||
fee_data["name"] = f"{fee_data['name']} - {establishment.name}"
|
||||
fee_data["establishment"] = establishment
|
||||
Fee.objects.update_or_create(
|
||||
name=fee_data["name"],
|
||||
type=fee_data["type"],
|
||||
defaults=fee_data
|
||||
)
|
||||
fee_data["establishment"] = establishment.id
|
||||
fee_data["type"] = random.choice([FeeType.REGISTRATION_FEE, FeeType.TUITION_FEE])
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Fees initialized or updated successfully'))
|
||||
serializer = FeeSerializer(data=fee_data)
|
||||
if serializer.is_valid():
|
||||
fee = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Fee {fee.name} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for fee: {serializer.errors}'))
|
||||
|
||||
def create_or_update_discounts(self):
|
||||
def init_discounts(self):
|
||||
discounts_data = self.load_data('discounts.json')
|
||||
|
||||
for discount_data in discounts_data:
|
||||
establishment = random.choice(self.establishments)
|
||||
discount_data["name"] = f"{discount_data['name']} - {establishment.name}"
|
||||
discount_data["establishment"] = establishment
|
||||
Discount.objects.update_or_create(
|
||||
name=discount_data["name"],
|
||||
type=discount_data["type"],
|
||||
discount_type=discount_data["discount_type"],
|
||||
defaults=discount_data
|
||||
)
|
||||
discount_data["establishment"] = establishment.id
|
||||
discount_data["type"] = random.choice([FeeType.REGISTRATION_FEE, FeeType.TUITION_FEE])
|
||||
discount_data["discount_type"] = random.choice([DiscountType.CURRENCY, DiscountType.PERCENT])
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Discounts initialized or updated successfully'))
|
||||
serializer = DiscountSerializer(data=discount_data)
|
||||
if serializer.is_valid():
|
||||
discount = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Discount {discount.name} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for discount: {serializer.errors}'))
|
||||
|
||||
def create_or_update_payment_modes(self):
|
||||
payment_modes_data = self.load_data('payment_modes.json')
|
||||
def init_payment_modes(self):
|
||||
modes = [PaymentModeType.SEPA, PaymentModeType.TRANSFER, PaymentModeType.CHECK, PaymentModeType.CASH]
|
||||
types = [FeeType.REGISTRATION_FEE, FeeType.TUITION_FEE]
|
||||
|
||||
for payment_mode_data in payment_modes_data:
|
||||
establishment = random.choice(self.establishments)
|
||||
payment_mode_data["establishment"] = establishment
|
||||
PaymentMode.objects.update_or_create(
|
||||
mode=payment_mode_data["mode"],
|
||||
type=payment_mode_data["type"],
|
||||
defaults=payment_mode_data
|
||||
)
|
||||
for establishment in self.establishments:
|
||||
for mode in modes:
|
||||
for type in types:
|
||||
payment_mode_data = {
|
||||
"mode": mode,
|
||||
"type": type,
|
||||
"is_active": random.choice([True, False]),
|
||||
"establishment": establishment.id
|
||||
}
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Payment Modes initialized or updated successfully'))
|
||||
serializer = PaymentModeSerializer(data=payment_mode_data)
|
||||
if serializer.is_valid():
|
||||
payment_mode = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Payment Mode {payment_mode} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for payment mode: {serializer.errors}'))
|
||||
|
||||
def create_or_update_payment_plans(self):
|
||||
payment_plans_data = self.load_data('payment_plans.json')
|
||||
def init_payment_plans(self):
|
||||
frequencies = [PaymentPlanType.ONE_TIME, PaymentPlanType.THREE_TIMES, PaymentPlanType.TEN_TIMES, PaymentPlanType.TWELVE_TIMES]
|
||||
types = [FeeType.REGISTRATION_FEE, FeeType.TUITION_FEE]
|
||||
current_date = timezone.now().date()
|
||||
|
||||
for payment_plan_data in payment_plans_data:
|
||||
establishment = random.choice(self.establishments)
|
||||
payment_plan_data["establishment"] = establishment
|
||||
payment_plan_data["due_dates"] = [current_date + relativedelta(months=1)]
|
||||
if payment_plan_data["frequency"] == PaymentPlanType.THREE_TIMES:
|
||||
payment_plan_data["due_dates"] = [current_date + relativedelta(months=1+4*i) for i in range(3)]
|
||||
elif payment_plan_data["frequency"] == PaymentPlanType.TEN_TIMES:
|
||||
payment_plan_data["due_dates"] = [current_date + relativedelta(months=1+i) for i in range(10)]
|
||||
elif payment_plan_data["frequency"] == PaymentPlanType.TWELVE_TIMES:
|
||||
payment_plan_data["due_dates"] = [current_date + relativedelta(months=1+i) for i in range(12)]
|
||||
for establishment in self.establishments:
|
||||
for frequency in frequencies:
|
||||
for type in types:
|
||||
payment_plan_data = {
|
||||
"frequency": frequency,
|
||||
"type": type,
|
||||
"is_active": random.choice([True, False]),
|
||||
"establishment": establishment.id,
|
||||
"due_dates": self.generate_due_dates(frequency, current_date)
|
||||
}
|
||||
|
||||
PaymentPlan.objects.update_or_create(
|
||||
frequency=payment_plan_data["frequency"],
|
||||
type=payment_plan_data["type"],
|
||||
defaults=payment_plan_data
|
||||
)
|
||||
serializer = PaymentPlanSerializer(data=payment_plan_data)
|
||||
if serializer.is_valid():
|
||||
payment_plan = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Payment Plan {payment_plan} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for payment plan: {serializer.errors}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Payment Plans initialized or updated successfully'))
|
||||
def generate_due_dates(self, frequency, start_date):
|
||||
if frequency == PaymentPlanType.ONE_TIME:
|
||||
return [start_date + relativedelta(months=1)]
|
||||
elif frequency == PaymentPlanType.THREE_TIMES:
|
||||
return [start_date + relativedelta(months=1+4*i) for i in range(3)]
|
||||
elif frequency == PaymentPlanType.TEN_TIMES:
|
||||
return [start_date + relativedelta(months=1+i) for i in range(10)]
|
||||
elif frequency == PaymentPlanType.TWELVE_TIMES:
|
||||
return [start_date + relativedelta(months=1+i) for i in range(12)]
|
||||
|
||||
def create_or_update_specialities(self):
|
||||
def init_specialities(self):
|
||||
specialities_data = self.load_data('specialities.json')
|
||||
|
||||
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 = self.load_data('teachers.json')
|
||||
|
||||
for teacher_data in teachers_data:
|
||||
specialities = teacher_data.pop("specialities")
|
||||
email = teacher_data["email"]
|
||||
droit = teacher_data.pop("droit")
|
||||
|
||||
establishment = random.choice(self.establishments)
|
||||
speciality_data["name"] = f"{speciality_data['name']} - {establishment.name}"
|
||||
speciality_data["establishment"] = establishment.id
|
||||
|
||||
# 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,
|
||||
"establishment": establishment
|
||||
serializer = SpecialitySerializer(data=speciality_data)
|
||||
if serializer.is_valid():
|
||||
speciality = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Speciality {speciality.name} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for speciality: {serializer.errors}'))
|
||||
|
||||
def init_teachers(self):
|
||||
fake = Faker()
|
||||
|
||||
# Récupérer tous les profils dont le role_type est soit ECOLE soit ADMIN
|
||||
profiles = Profile.objects.filter(roles__role_type__in=[ProfileRole.RoleType.PROFIL_ECOLE, ProfileRole.RoleType.PROFIL_ADMIN]).distinct()
|
||||
|
||||
for profile in profiles:
|
||||
# Récupérer les rôles associés au profil
|
||||
profile_roles = ProfileRole.objects.filter(profile=profile, role_type__in=[ProfileRole.RoleType.PROFIL_ECOLE, ProfileRole.RoleType.PROFIL_ADMIN])
|
||||
|
||||
for profile_role in profile_roles:
|
||||
establishment = profile_role.establishment
|
||||
teacher_data = {
|
||||
"last_name": fake.last_name(),
|
||||
"first_name": f"{fake.first_name()} - {establishment.name}",
|
||||
"profile_role": profile_role.id
|
||||
}
|
||||
)
|
||||
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()
|
||||
establishment_specialities = list(Speciality.objects.filter(establishment=establishment))
|
||||
num_specialities = min(random.randint(1, 3), len(establishment_specialities))
|
||||
selected_specialities = random.sample(establishment_specialities, num_specialities)
|
||||
|
||||
# Créer l'enseignant si il n'existe pas
|
||||
teacher_serializer = TeacherSerializer(data=teacher_data)
|
||||
if teacher_serializer.is_valid():
|
||||
teacher = teacher_serializer.save()
|
||||
# Associer les spécialités
|
||||
teacher.specialities.set(selected_specialities)
|
||||
teacher.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Teacher {teacher.last_name} created successfully for establishment {establishment.name}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for teacher: {teacher_serializer.errors}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Teachers initialized or updated successfully'))
|
||||
|
||||
def create_or_update_school_classes(self):
|
||||
|
||||
def init_guardians(self):
|
||||
fake = Faker()
|
||||
|
||||
# Récupérer tous les profils dont le role_type est PROFIL_PARENT
|
||||
profiles = Profile.objects.filter(roles__role_type=ProfileRole.RoleType.PROFIL_PARENT).distinct()
|
||||
|
||||
for profile in profiles:
|
||||
# Récupérer les rôles associés au profil
|
||||
profile_roles = ProfileRole.objects.filter(profile=profile, role_type=ProfileRole.RoleType.PROFIL_PARENT)
|
||||
|
||||
for profile_role in profile_roles:
|
||||
establishment = profile_role.establishment
|
||||
guardian_data = {
|
||||
"last_name": fake.last_name(),
|
||||
"first_name": f"{fake.first_name()} - {establishment.name}",
|
||||
"profile_role": profile_role.id,
|
||||
"birth_date": fake.date_of_birth().strftime('%Y-%m-%d'), # Convertir en chaîne de caractères valide
|
||||
"address": fake.address(),
|
||||
"phone": fake.phone_number(),
|
||||
"profession": fake.job()
|
||||
}
|
||||
|
||||
# Créer le guardian si il n'existe pas
|
||||
guardian_serializer = GuardianSerializer(data=guardian_data)
|
||||
if guardian_serializer.is_valid():
|
||||
guardian = guardian_serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'Guardian {guardian.last_name} created successfully for establishment {establishment.name}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for guardian: {guardian_serializer.errors}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('Guardians initialized or updated successfully'))
|
||||
|
||||
def init_school_classes(self):
|
||||
school_classes_data = self.load_data('school_classes.json')
|
||||
|
||||
for index, class_data in enumerate(school_classes_data, start=1):
|
||||
teachers_ids = class_data.pop("teachers")
|
||||
# Randomize establishment
|
||||
establishment = random.choice(self.establishments)
|
||||
class_data["atmosphere_name"] = f"Classe {index} - {establishment.name}"
|
||||
class_data["establishment"] = establishment
|
||||
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()
|
||||
class_data["establishment"] = establishment.id
|
||||
|
||||
# Randomize levels
|
||||
class_data["levels"] = random.sample(range(1, 10), random.randint(1, 5))
|
||||
|
||||
# Randomize teachers
|
||||
establishment_teachers = list(Teacher.objects.filter(profile_role__establishment=establishment))
|
||||
num_teachers = min(random.randint(1, 10), len(establishment_teachers))
|
||||
selected_teachers = random.sample(establishment_teachers, num_teachers)
|
||||
teachers_ids = [teacher.id for teacher in selected_teachers]
|
||||
|
||||
# Use the serializer to create or update the school class
|
||||
class_data["teachers"] = teachers_ids
|
||||
serializer = SchoolClassSerializer(data=class_data)
|
||||
if serializer.is_valid():
|
||||
school_class = serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'SchoolClass {school_class.atmosphere_name} created or updated successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for school class: {serializer.errors}'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('SchoolClasses initialized or updated successfully'))
|
||||
|
||||
def create_or_update_registration_file_group(self):
|
||||
file_groups_data = self.load_data('file_groups.json')
|
||||
def init_file_group(self):
|
||||
fake = Faker()
|
||||
|
||||
for group_data in file_groups_data:
|
||||
RegistrationFileGroup.objects.update_or_create(name=group_data["name"], defaults=group_data)
|
||||
self.stdout.write(self.style.SUCCESS(f'RegistrationFileGroup {group_data["name"]} initialized or updated successfully'))
|
||||
for establishment in self.establishments:
|
||||
for i in range(1, 4): # Créer 3 groupes de fichiers par établissement
|
||||
name = f"Fichiers d'inscription - {fake.word()} - {establishment.name}"
|
||||
description = fake.sentence()
|
||||
group_data = {
|
||||
"name": name,
|
||||
"description": description,
|
||||
"establishment": establishment
|
||||
}
|
||||
RegistrationFileGroup.objects.update_or_create(name=name, defaults=group_data)
|
||||
self.stdout.write(self.style.SUCCESS(f'RegistrationFileGroup {name} initialized or updated successfully'))
|
||||
|
||||
def create_register_form(self):
|
||||
self.stdout.write(self.style.SUCCESS('All RegistrationFileGroups initialized or updated successfully'))
|
||||
|
||||
def init_register_form(self):
|
||||
fake = Faker('fr_FR') # Utiliser le locale français pour Faker
|
||||
|
||||
file_group_count = RegistrationFileGroup.objects.count()
|
||||
@ -218,40 +358,13 @@ class Command(BaseCommand):
|
||||
for _ in range(50):
|
||||
establishment = random.choice(self.establishments)
|
||||
|
||||
# Générer des données fictives pour le profil
|
||||
profile_data = {
|
||||
"email": fake.email(),
|
||||
"droit": 2,
|
||||
"username": fake.user_name(),
|
||||
"is_active": True,
|
||||
"password": "Provisoire01!",
|
||||
"establishment": establishment
|
||||
}
|
||||
|
||||
user, created = Profile.objects.update_or_create(
|
||||
email=profile_data["email"],
|
||||
defaults={
|
||||
"username": profile_data["username"],
|
||||
"email": profile_data["email"],
|
||||
"is_active": profile_data["is_active"],
|
||||
"droit": profile_data["droit"],
|
||||
"establishment": profile_data["establishment"]
|
||||
}
|
||||
)
|
||||
if created:
|
||||
user.set_password(profile_data["password"])
|
||||
user.save()
|
||||
|
||||
# Générer des données fictives pour le guardian
|
||||
guardian_data = {
|
||||
"associated_profile_id": user.id,
|
||||
"email": profile_data["email"],
|
||||
}
|
||||
# Récupérer un guardian aléatoire déjà créé
|
||||
guardian = Guardian.objects.order_by('?').first()
|
||||
|
||||
# Générer des données fictives pour l'étudiant
|
||||
student_data = {
|
||||
"last_name": f"{fake.last_name()} - {establishment.name}",
|
||||
"first_name": fake.first_name(),
|
||||
"last_name": fake.last_name(),
|
||||
"first_name": f"{fake.first_name()} - {establishment.name}",
|
||||
"address": fake.address(),
|
||||
"birth_date": fake.date_of_birth(),
|
||||
"birth_place": fake.city(),
|
||||
@ -261,16 +374,12 @@ class Command(BaseCommand):
|
||||
"level": fake.random_int(min=1, max=6)
|
||||
}
|
||||
|
||||
# Créer ou mettre à jour l'étudiant et le guardian
|
||||
# Créer ou mettre à jour l'étudiant
|
||||
student, created = Student.objects.get_or_create(
|
||||
last_name=student_data["last_name"],
|
||||
first_name=student_data["first_name"],
|
||||
defaults=student_data
|
||||
)
|
||||
guardian, created = Guardian.objects.get_or_create(
|
||||
last_name=guardian_data["email"],
|
||||
defaults=guardian_data
|
||||
)
|
||||
student.guardians.add(guardian)
|
||||
|
||||
# Récupérer les frais et les réductions
|
||||
@ -279,18 +388,23 @@ class Command(BaseCommand):
|
||||
|
||||
# Créer les données du formulaire d'inscription
|
||||
register_form_data = {
|
||||
"student": student,
|
||||
"fileGroup": RegistrationFileGroup.objects.get(id=fake.random_int(min=1, max=file_group_count)),
|
||||
"establishment": establishment,
|
||||
"status": fake.random_int(min=1, max=3)
|
||||
}
|
||||
|
||||
# Créer ou mettre à jour le formulaire d'inscription
|
||||
register_form, created = RegistrationForm.objects.get_or_create(student=student, defaults=register_form_data)
|
||||
register_form.fees.set(fees)
|
||||
register_form.discounts.set(discounts)
|
||||
if not created:
|
||||
register_form.fileGroup = file_group
|
||||
register_form.save()
|
||||
register_form, created = RegistrationForm.objects.get_or_create(
|
||||
student=student,
|
||||
establishment=establishment,
|
||||
defaults=register_form_data
|
||||
)
|
||||
|
||||
if created:
|
||||
register_form.fees.set(fees)
|
||||
register_form.discounts.set(discounts)
|
||||
self.stdout.write(self.style.SUCCESS(f'RegistrationForm for student {student.last_name} created successfully'))
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS(f'RegistrationForm for student {student.last_name} already exists'))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS('50 RegistrationForms initialized or updated successfully'))
|
||||
Reference in New Issue
Block a user