mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Gestion des rattachements de Guardian à des RF déjà existants
This commit is contained in:
@ -52,7 +52,7 @@ class Command(BaseCommand):
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
self.init_establishments()
|
||||
#self.init_profiles()
|
||||
self.init_profiles()
|
||||
self.init_fees()
|
||||
self.init_discounts()
|
||||
self.init_payment_modes()
|
||||
@ -81,57 +81,47 @@ class Command(BaseCommand):
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for establishment: {serializer.errors}'))
|
||||
|
||||
def init_profiles(self):
|
||||
profiles_data = self.load_data('profiles.json')
|
||||
fake = Faker()
|
||||
|
||||
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(50):
|
||||
# Générer des données fictives pour le profil
|
||||
profile_data = {
|
||||
"username": fake.user_name(),
|
||||
"email": fake.email(),
|
||||
"password": "Provisoire01!",
|
||||
"code": "",
|
||||
"datePeremption": ""
|
||||
}
|
||||
|
||||
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()
|
||||
# Créer le profil
|
||||
profile_serializer = ProfileSerializer(data=profile_data)
|
||||
if profile_serializer.is_valid():
|
||||
profile = 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, # Passer l'ID du profil au lieu de l'instance de modèle Profile
|
||||
"establishment": role["establishment"],
|
||||
"role_type": role["role_type"],
|
||||
# Créer entre 1 et 3 ProfileRole pour chaque profil
|
||||
num_roles = random.randint(1, 3)
|
||||
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])
|
||||
|
||||
profile_role_data = {
|
||||
"profile": profile.id,
|
||||
"establishment": establishment.id,
|
||||
"role_type": role_type,
|
||||
"is_active": True
|
||||
}
|
||||
role_serializer = ProfileRoleSerializer(data=role_data)
|
||||
if role_serializer.is_valid():
|
||||
role_serializer.save()
|
||||
|
||||
profile_role_serializer = ProfileRoleSerializer(data=profile_role_data)
|
||||
if profile_role_serializer.is_valid():
|
||||
profile_role_serializer.save()
|
||||
self.stdout.write(self.style.SUCCESS(f'ProfileRole for {profile.email} created successfully with role type {role_type}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile role: {role_serializer.errors}'))
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile role: {profile_role_serializer.errors}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile: {serializer.errors}'))
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for profile: {profile_serializer.errors}'))
|
||||
|
||||
def init_fees(self):
|
||||
fees_data = self.load_data('fees.json')
|
||||
@ -239,35 +229,42 @@ class Command(BaseCommand):
|
||||
def init_teachers(self):
|
||||
fake = Faker()
|
||||
|
||||
# Récupérer tous les établissements
|
||||
establishments = self.establishments
|
||||
# Récupérer tous les profils existants avec un rôle ECOLE ou ADMIN
|
||||
profiles_with_roles = Profile.objects.filter(roles__role_type__in=[ProfileRole.RoleType.PROFIL_ECOLE, ProfileRole.RoleType.PROFIL_ADMIN]).distinct()
|
||||
|
||||
for establishment in establishments:
|
||||
# Générer des données fictives pour le profil
|
||||
profile_data = {
|
||||
"username": fake.user_name(),
|
||||
"email": fake.email(),
|
||||
"password": "Provisoire01!",
|
||||
"code": "",
|
||||
"datePeremption": ""
|
||||
}
|
||||
if not profiles_with_roles.exists():
|
||||
self.stdout.write(self.style.ERROR('No profiles with role_type ECOLE or ADMIN found'))
|
||||
return
|
||||
|
||||
# Générer des données fictives pour le profile_role
|
||||
profile_role_data = {
|
||||
"establishment": establishment.id,
|
||||
"role_type": fake.random_int(min=ProfileRole.RoleType.PROFIL_ECOLE, max=ProfileRole.RoleType.PROFIL_ADMIN),
|
||||
"is_active": True,
|
||||
"profile": profile_data
|
||||
}
|
||||
used_profiles = set()
|
||||
|
||||
for _ in range(15):
|
||||
# Récupérer un profil aléatoire qui n'a pas encore été utilisé
|
||||
available_profiles = profiles_with_roles.exclude(id__in=used_profiles)
|
||||
if not available_profiles.exists():
|
||||
self.stdout.write(self.style.ERROR('Not enough profiles with role_type ECOLE or ADMIN available'))
|
||||
break
|
||||
|
||||
profile = random.choice(available_profiles)
|
||||
used_profiles.add(profile.id)
|
||||
|
||||
# Récupérer les ProfileRole associés au profil avec les rôles ECOLE ou ADMIN
|
||||
profile_roles = ProfileRole.objects.filter(profile=profile, role_type__in=[ProfileRole.RoleType.PROFIL_ECOLE, ProfileRole.RoleType.PROFIL_ADMIN])
|
||||
|
||||
if not profile_roles.exists():
|
||||
self.stdout.write(self.style.ERROR(f'No ProfileRole with role_type ECOLE or ADMIN found for profile {profile.email}'))
|
||||
continue
|
||||
|
||||
profile_role = random.choice(profile_roles)
|
||||
|
||||
# Générer des données fictives pour l'enseignant
|
||||
teacher_data = {
|
||||
"last_name": fake.last_name(),
|
||||
"first_name": f"{fake.first_name()} - {establishment.name}",
|
||||
"profile_role": profile_role_data
|
||||
"first_name": f"{fake.first_name()} - {profile_role.establishment.name}",
|
||||
"profile_role": profile_role.id
|
||||
}
|
||||
|
||||
establishment_specialities = list(Speciality.objects.filter(establishment=establishment))
|
||||
establishment_specialities = list(Speciality.objects.filter(establishment=profile_role.establishment))
|
||||
num_specialities = min(random.randint(1, 3), len(establishment_specialities))
|
||||
selected_specialities = random.sample(establishment_specialities, num_specialities)
|
||||
|
||||
@ -278,7 +275,7 @@ class Command(BaseCommand):
|
||||
# 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}'))
|
||||
self.stdout.write(self.style.SUCCESS(f'Teacher {teacher.last_name} created successfully for establishment {profile_role.establishment.name}'))
|
||||
else:
|
||||
self.stdout.write(self.style.ERROR(f'Error in data for teacher: {teacher_serializer.errors}'))
|
||||
|
||||
@ -335,26 +332,33 @@ class Command(BaseCommand):
|
||||
|
||||
file_group_count = RegistrationFileGroup.objects.count()
|
||||
|
||||
# Récupérer tous les profils existants avec un ProfileRole Parent
|
||||
profiles_with_parent_role = Profile.objects.filter(roles__role_type=ProfileRole.RoleType.PROFIL_PARENT).distinct()
|
||||
|
||||
if not profiles_with_parent_role.exists():
|
||||
self.stdout.write(self.style.ERROR('No profiles with ProfileRole Parent found'))
|
||||
return
|
||||
|
||||
used_profiles = set()
|
||||
|
||||
for _ in range(50):
|
||||
establishment = random.choice(self.establishments)
|
||||
|
||||
# Générer des données fictives pour le profil
|
||||
profile_data = {
|
||||
"username": fake.user_name(),
|
||||
"email": fake.email(),
|
||||
"password": "Provisoire01!",
|
||||
"code": "",
|
||||
"datePeremption": ""
|
||||
}
|
||||
# Récupérer un profil aléatoire qui n'a pas encore été utilisé
|
||||
available_profiles = profiles_with_parent_role.exclude(id__in=used_profiles)
|
||||
if not available_profiles.exists():
|
||||
self.stdout.write(self.style.ERROR('Not enough profiles with ProfileRole Parent available'))
|
||||
break
|
||||
|
||||
profile = random.choice(available_profiles)
|
||||
used_profiles.add(profile.id)
|
||||
|
||||
# Récupérer le ProfileRole Parent associé au profil
|
||||
profile_role = ProfileRole.objects.filter(profile=profile, role_type=ProfileRole.RoleType.PROFIL_PARENT).first()
|
||||
|
||||
# Générer des données fictives pour le guardian
|
||||
guardian_data = {
|
||||
"profile_role": {
|
||||
"establishment": establishment.id,
|
||||
"role_type": ProfileRole.RoleType.PROFIL_PARENT,
|
||||
"is_active": True,
|
||||
"profile": profile_data
|
||||
},
|
||||
"profile_role": profile_role.id,
|
||||
"last_name": fake.last_name(),
|
||||
"first_name": fake.first_name(),
|
||||
"birth_date": fake.date_of_birth().strftime('%Y-%m-%d'),
|
||||
|
||||
Reference in New Issue
Block a user