mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Ajout de la fratrie [#27]
This commit is contained in:
@ -27,7 +27,7 @@ class Guardian(models.Model):
|
||||
"""
|
||||
last_name = models.CharField(max_length=200, default="")
|
||||
first_name = models.CharField(max_length=200, default="")
|
||||
birth_date = models.CharField(max_length=200, default="", blank=True)
|
||||
birth_date = models.DateField(null=True, blank=True)
|
||||
address = models.CharField(max_length=200, default="", blank=True)
|
||||
phone = models.CharField(max_length=200, default="", blank=True)
|
||||
profession = models.CharField(max_length=200, default="", blank=True)
|
||||
@ -43,7 +43,7 @@ class Sibling(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
last_name = models.CharField(max_length=200, default="")
|
||||
first_name = models.CharField(max_length=200, default="")
|
||||
birth_date = models.CharField(max_length=200, default="", blank=True)
|
||||
birth_date = models.DateField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return "SIBLING"
|
||||
|
||||
@ -158,15 +158,33 @@ class StudentSerializer(serializers.ModelSerializer):
|
||||
guardians_ids.append(guardian_instance.id)
|
||||
return guardians_ids
|
||||
|
||||
def create_or_update_siblings(self, siblings_data):
|
||||
siblings_ids = []
|
||||
def create_or_update_siblings(self, siblings_data, student_instance):
|
||||
"""
|
||||
Crée ou met à jour les frères et sœurs associés à un étudiant.
|
||||
Supprime les frères et sœurs qui ne sont plus présents dans siblings_data.
|
||||
"""
|
||||
|
||||
# Si siblings_data est vide, supprimer tous les frères et sœurs associés
|
||||
if not siblings_data:
|
||||
student_instance.siblings.clear() # Supprime toutes les relations
|
||||
return []
|
||||
|
||||
# Récupérer les IDs des frères et sœurs existants
|
||||
existing_sibling_ids = set(student_instance.siblings.values_list('id', flat=True))
|
||||
|
||||
# Créer ou mettre à jour les frères et sœurs
|
||||
updated_sibling_ids = []
|
||||
for sibling_data in siblings_data:
|
||||
sibling_instance, created = Sibling.objects.update_or_create(
|
||||
id=sibling_data.get('id'),
|
||||
defaults=sibling_data
|
||||
)
|
||||
siblings_ids.append(sibling_instance.id)
|
||||
return siblings_ids
|
||||
updated_sibling_ids.append(sibling_instance.id)
|
||||
|
||||
# Supprimer les frères et sœurs qui ne sont plus dans siblings_data
|
||||
siblings_to_delete = existing_sibling_ids - set(updated_sibling_ids)
|
||||
Sibling.objects.filter(id__in=siblings_to_delete).delete()
|
||||
return updated_sibling_ids
|
||||
|
||||
def create_or_update_languages(self, languages_data):
|
||||
languages_ids = []
|
||||
@ -195,8 +213,10 @@ class StudentSerializer(serializers.ModelSerializer):
|
||||
languages_data = validated_data.pop('spoken_languages', [])
|
||||
if guardians_data:
|
||||
instance.guardians.set(self.create_or_update_guardians(guardians_data))
|
||||
if siblings_data:
|
||||
instance.siblings.set(self.create_or_update_siblings(siblings_data))
|
||||
|
||||
sibling_ids = self.create_or_update_siblings(siblings_data, instance)
|
||||
instance.siblings.set(sibling_ids)
|
||||
|
||||
if languages_data:
|
||||
instance.spoken_languages.set(self.create_or_update_languages(languages_data))
|
||||
|
||||
|
||||
@ -146,7 +146,6 @@ def rfToPDF(registerForm, filename):
|
||||
|
||||
# Vérifier si le fichier existe et le supprimer
|
||||
if os.path.exists(existing_file_path):
|
||||
print(f'exist ! REMOVE')
|
||||
os.remove(existing_file_path)
|
||||
registerForm.registration_file.delete(save=False)
|
||||
else:
|
||||
|
||||
@ -266,6 +266,23 @@ class RegisterFormWithIdView(APIView):
|
||||
# Sauvegarder la photo si elle est présente dans la requête
|
||||
if photo_file:
|
||||
student = registerForm.student
|
||||
|
||||
# Vérifier si une photo existante est déjà associée à l'étudiant
|
||||
if student.photo and student.photo.name:
|
||||
# Construire le chemin complet du fichier existant
|
||||
if os.path.isabs(student.photo.name):
|
||||
existing_file_path = student.photo.name
|
||||
else:
|
||||
existing_file_path = os.path.join(settings.MEDIA_ROOT, student.photo.name.lstrip('/'))
|
||||
|
||||
# Vérifier si le fichier existe et le supprimer
|
||||
if os.path.exists(existing_file_path):
|
||||
os.remove(existing_file_path)
|
||||
student.photo.delete(save=False)
|
||||
else:
|
||||
print(f'File does not exist: {existing_file_path}')
|
||||
|
||||
# Sauvegarder la nouvelle photo
|
||||
student.photo.save(photo_file.name, photo_file, save=True)
|
||||
else:
|
||||
return JsonResponse(studentForm_serializer.errors, safe=False, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
Reference in New Issue
Block a user