mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Ajout de la possibilité de supprimer une association
guardian/student + ajout de la possibilité de créer un guardian pour un student + tri chrologique
This commit is contained in:
@ -27,6 +27,7 @@ class ProfileRole(models.Model):
|
||||
role_type = models.IntegerField(choices=RoleType.choices, default=RoleType.PROFIL_UNDEFINED)
|
||||
establishment = models.ForeignKey(Establishment, on_delete=models.CASCADE, related_name='profile_roles')
|
||||
is_active = models.BooleanField(default=False)
|
||||
updated_date = models.DateTimeField(auto_now=True)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.profile.email} - {self.get_role_type_display()} - {self.establishment.name}"
|
||||
@ -3,6 +3,9 @@ from Auth.models import Profile, ProfileRole
|
||||
from Establishment.models import Establishment
|
||||
from Subscriptions.models import Guardian, RegistrationForm
|
||||
from School.models import Teacher
|
||||
from N3wtSchool import settings
|
||||
from django.utils import timezone
|
||||
import pytz
|
||||
|
||||
class ProfileSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(required=False)
|
||||
@ -69,10 +72,11 @@ class ProfileRoleSerializer(serializers.ModelSerializer):
|
||||
profile_data = ProfileSerializer(write_only=True, required=False)
|
||||
associated_profile_email = serializers.SerializerMethodField()
|
||||
associated_person = serializers.SerializerMethodField()
|
||||
updated_date_formatted = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = ProfileRole
|
||||
fields = ['id', 'role_type', 'establishment', 'is_active', 'profile', 'profile_data', 'associated_profile_email', 'associated_person']
|
||||
fields = ['id', 'role_type', 'establishment', 'is_active', 'profile', 'profile_data', 'associated_profile_email', 'associated_person', 'updated_date_formatted']
|
||||
|
||||
def create(self, validated_data):
|
||||
profile_data = validated_data.pop('profile_data', None)
|
||||
@ -142,4 +146,11 @@ class ProfileRoleSerializer(serializers.ModelSerializer):
|
||||
"classes": classes_list,
|
||||
"specialities": specialities_list
|
||||
}
|
||||
return None
|
||||
return None
|
||||
|
||||
def get_updated_date_formatted(self, obj):
|
||||
utc_time = timezone.localtime(obj.updated_date)
|
||||
local_tz = pytz.timezone(settings.TZ_APPLI)
|
||||
local_time = utc_time.astimezone(local_tz)
|
||||
|
||||
return local_time.strftime("%d-%m-%Y %H:%M")
|
||||
@ -538,7 +538,7 @@ class ProfileRoleView(APIView):
|
||||
|
||||
profiles_roles_List = bdd.getAllObjects(_objectName=ProfileRole)
|
||||
if profiles_roles_List:
|
||||
profiles_roles_List = profiles_roles_List.filter(establishment=establishment_id).distinct()
|
||||
profiles_roles_List = profiles_roles_List.filter(establishment=establishment_id).distinct().order_by('-updated_date')
|
||||
profile_roles_serializer = ProfileRoleSerializer(profiles_roles_List, many=True)
|
||||
return JsonResponse(profile_roles_serializer.data, safe=False)
|
||||
|
||||
|
||||
@ -67,7 +67,6 @@ class TeacherSerializer(serializers.ModelSerializer):
|
||||
specialities_data = validated_data.pop('specialities', None)
|
||||
associated_profile_email = validated_data.pop('associated_profile_email')
|
||||
establishment_id = validated_data.get('establishment')
|
||||
print(f'debug : {validated_data}')
|
||||
role_type = validated_data.pop('role_type')
|
||||
|
||||
profile, created = Profile.objects.get_or_create(
|
||||
|
||||
@ -26,7 +26,6 @@ def getStateMachineObjectState(etat):
|
||||
def updateStateMachine(rf, transition) :
|
||||
automateModel = load_config('Subscriptions/Configuration/automate.json')
|
||||
state_machine = getStateMachineObject(rf.status)
|
||||
print(f'etat DI : {state_machine.state}')
|
||||
if state_machine.trigger(transition, automateModel):
|
||||
rf.status = state_machine.state
|
||||
rf.save()
|
||||
|
||||
@ -29,7 +29,7 @@ class Guardian(models.Model):
|
||||
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)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='guardian_profile', null=True, blank=True)
|
||||
profile_role = models.OneToOneField(ProfileRole, on_delete=models.CASCADE, related_name='guardian_profile', blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.last_name + "_" + self.first_name
|
||||
|
||||
@ -5,6 +5,7 @@ from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
|
||||
from Subscriptions.models import Guardian, Student
|
||||
from Auth.models import ProfileRole
|
||||
from N3wtSchool import bdd
|
||||
|
||||
class GuardianView(APIView):
|
||||
@ -50,12 +51,34 @@ class DissociateGuardianView(APIView):
|
||||
# Supprimer la relation entre le student et le guardian
|
||||
student.guardians.remove(guardian)
|
||||
|
||||
if guardian.profile_role:
|
||||
guardian.profile_role.save()
|
||||
|
||||
isGuardianDeleted = False
|
||||
# Vérifier si le guardian n'est plus associé à aucun élève
|
||||
if guardian.student_set.count() == 0: # Utilise la relation ManyToMany inverse
|
||||
print(f'Le guardian {guardian} n\'est plus rattaché à aucun élève : on le supprime')
|
||||
isGuardianDeleted = True
|
||||
|
||||
# Vérifier si le guardian a un ProfileRole associé
|
||||
if guardian.profile_role:
|
||||
print(f'Suppression du ProfileRole associé au guardian {guardian}')
|
||||
guardian.profile_role.delete()
|
||||
|
||||
# Vérifier si le Profile n'a plus de ProfileRole associés
|
||||
profile = guardian.profile_role.profile
|
||||
if not ProfileRole.objects.filter(profile=profile).exists():
|
||||
print(f'Le profile {profile} n\'a plus de rôle associé : on le supprime')
|
||||
profile.delete()
|
||||
|
||||
# Supprimer le guardian
|
||||
guardian.delete()
|
||||
|
||||
return JsonResponse(
|
||||
{"message": f"Le guardian {guardian.last_name} {guardian.first_name} a été dissocié de l'étudiant {student.last_name} {student.first_name}."},
|
||||
{
|
||||
"message": f"Le guardian {guardian.last_name} {guardian.first_name} a été dissocié de l'étudiant {student.last_name} {student.first_name}.",
|
||||
"isGuardianDeleted": isGuardianDeleted
|
||||
},
|
||||
status=status.HTTP_200_OK
|
||||
)
|
||||
except Student.DoesNotExist:
|
||||
|
||||
@ -99,8 +99,7 @@ class RegisterFormView(APIView):
|
||||
registerForms_List = None
|
||||
|
||||
if registerForms_List:
|
||||
print(f'filtrate sur lestablishment : {establishment_id}')
|
||||
registerForms_List = registerForms_List.filter(establishment=establishment_id)
|
||||
registerForms_List = registerForms_List.filter(establishment=establishment_id).order_by('-last_update')
|
||||
|
||||
if not registerForms_List:
|
||||
return JsonResponse({'error': 'aucune donnée trouvée', 'count': 0}, safe=False)
|
||||
|
||||
Reference in New Issue
Block a user