init
This commit is contained in:
198
backend-django/backend/signals.py
Normal file
198
backend-django/backend/signals.py
Normal file
@@ -0,0 +1,198 @@
|
||||
"""
|
||||
Les signaux permettent à certains expéditeurs d’avertir un ensemble de destinataires qu’une action a eu lieu.
|
||||
Dans ce projet, les signaux ont été utilisés pour mettre à jour les compteurs des postes appartenant à la FE lorsque le taux d'armement change en fonction de la catégorie.
|
||||
"""
|
||||
import numpy as np
|
||||
|
||||
from . import constants
|
||||
from .models import FormationEmploi, Poste, SousVivierAssociation, Administre
|
||||
from django.db.models.signals import post_save
|
||||
|
||||
|
||||
|
||||
def update_taux_mdr(sender, instance, **kwargs):
|
||||
"""Met à jour les compteurs des postes appartenant à la FE dont on change le taux d'armement cible pour la catégorie MDR
|
||||
|
||||
:type instance: objet du modèle FormationEmploi
|
||||
:param instance: FE dont on change le taux d'armement cible pour la catégorie MDR
|
||||
|
||||
|
||||
"""
|
||||
|
||||
taux = instance.fe_taux_armement_cible_mdr
|
||||
fe_id = instance.fe_code
|
||||
|
||||
postes = Poste.objects.filter(formation_emploi_id=fe_id, p_categorie='MDR')
|
||||
if (len(postes) > 0) and (taux is not None):
|
||||
taux = taux * 0.01
|
||||
for poste in postes:
|
||||
new_nb_reevalue = poste.p_nb_reevalue * taux
|
||||
new_nb_vacant = new_nb_reevalue - poste.p_nb_occupe
|
||||
if new_nb_reevalue >= poste.p_nb_reevalue:
|
||||
new_nb_p4 = poste.p_nb_p4 + new_nb_vacant - poste.p_nb_vacant
|
||||
poste.p_nb_p4 = new_nb_p4
|
||||
|
||||
else:
|
||||
if new_nb_reevalue > poste.p_nb_occupe:
|
||||
new_nb_non_etudie = poste.p_nb_non_etudie + poste.p_nb_reevalue - new_nb_reevalue
|
||||
poste.p_nb_non_etudie = new_nb_non_etudie
|
||||
delta_vacant = new_nb_vacant - poste.p_nb_vacant
|
||||
if delta_vacant <= poste.p_nb_p4:
|
||||
new_poste_p4 = poste.p_nb_p4 - delta_vacant
|
||||
poste.p_nb_p4 = new_poste_p4
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p4
|
||||
poste.p_nb_p4 = 0
|
||||
if delta_vacant <= poste.p_nb_p3:
|
||||
new_poste_p3 = poste.p_nb_p3 - delta_vacant
|
||||
poste.p_nb_p3 = new_poste_p3
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p3
|
||||
poste.p_nb_p3 = 0
|
||||
if delta_vacant <= poste.p_nb_p2:
|
||||
new_poste_p2 = poste.p_nb_p2 - delta_vacant
|
||||
poste.p_nb_p2 = new_poste_p2
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p2
|
||||
poste.p_nb_p2 = 0
|
||||
new_nb_p1 = poste.p_nb_p1 - delta_vacant
|
||||
poste.p_nb_p1 = new_nb_p1
|
||||
else:
|
||||
poste.p_nb_p3 = 0
|
||||
poste.p_nb_p2 = 0
|
||||
poste.p_nb_p1 = 0
|
||||
poste.p_nb_p4 = 0
|
||||
|
||||
poste.p_nb_reevalue = new_nb_reevalue
|
||||
poste.p_nb_vacant = new_nb_vacant
|
||||
poste.save(
|
||||
update_fields=['p_nb_reevalue', 'p_nb_vacant', 'p_nb_non_etudie', 'p_nb_p4', 'p_nb_p3', 'p_nb_p2',
|
||||
'p_nb_p1'])
|
||||
|
||||
|
||||
post_save.connect(update_taux_mdr, sender=FormationEmploi)
|
||||
|
||||
|
||||
def update_taux_off(sender, instance, **kwargs):
|
||||
"""Met à jour les compteurs des postes appartenant à la FE dont on change le taux d'armement cible pour la catégorie OFF
|
||||
|
||||
:type instance: objet du modèle FormationEmploi
|
||||
:param instance: FE dont on change le taux d'armement cible pour la catégorie OFF
|
||||
|
||||
"""
|
||||
taux = instance.fe_taux_armement_cible_off
|
||||
fe_id = instance.fe_code
|
||||
|
||||
postes = Poste.objects.filter(formation_emploi_id=fe_id, p_categorie='OFF')
|
||||
if (len(postes) > 0) and (taux is not None):
|
||||
taux = taux * 0.01
|
||||
for poste in postes:
|
||||
new_nb_reevalue = poste.p_nb_reevalue * taux
|
||||
new_nb_vacant = new_nb_reevalue - poste.p_nb_occupe
|
||||
if new_nb_reevalue >= poste.p_nb_reevalue:
|
||||
new_nb_p4 = poste.p_nb_p4 + new_nb_vacant - poste.p_nb_vacant
|
||||
poste.p_nb_p4 = new_nb_p4
|
||||
|
||||
else:
|
||||
if new_nb_reevalue > poste.p_nb_occupe:
|
||||
new_nb_non_etudie = poste.p_nb_non_etudie + poste.p_nb_reevalue - new_nb_reevalue
|
||||
poste.p_nb_non_etudie = new_nb_non_etudie
|
||||
delta_vacant = new_nb_vacant - poste.p_nb_vacant
|
||||
if delta_vacant <= poste.p_nb_p4:
|
||||
new_poste_p4 = poste.p_nb_p4 - delta_vacant
|
||||
poste.p_nb_p4 = new_poste_p4
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p4
|
||||
poste.p_nb_p4 = 0
|
||||
if delta_vacant <= poste.p_nb_p3:
|
||||
new_poste_p3 = poste.p_nb_p3 - delta_vacant
|
||||
poste.p_nb_p3 = new_poste_p3
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p3
|
||||
poste.p_nb_p3 = 0
|
||||
if delta_vacant <= poste.p_nb_p2:
|
||||
new_poste_p2 = poste.p_nb_p2 - delta_vacant
|
||||
poste.p_nb_p2 = new_poste_p2
|
||||
else:
|
||||
delta_vacant -= poste.p_nb_p2
|
||||
poste.p_nb_p2 = 0
|
||||
new_nb_p1 = poste.p_nb_p1 - delta_vacant
|
||||
poste.p_nb_p1 = new_nb_p1
|
||||
else:
|
||||
poste.p_nb_p3 = 0
|
||||
poste.p_nb_p2 = 0
|
||||
poste.p_nb_p1 = 0
|
||||
poste.p_nb_p4 = 0
|
||||
|
||||
poste.p_nb_reevalue = new_nb_reevalue
|
||||
poste.p_nb_vacant = new_nb_vacant
|
||||
poste.save(
|
||||
update_fields=['p_nb_reevalue', 'p_nb_vacant', 'p_nb_non_etudie', 'p_nb_p4', 'p_nb_p3', 'p_nb_p2',
|
||||
'p_nb_p1'])
|
||||
|
||||
|
||||
post_save.connect(update_taux_off, sender=FormationEmploi)
|
||||
|
||||
|
||||
def update_taux_soff(sender, instance, **kwargs):
|
||||
"""Met à jour les compteurs des postes appartenant à la FE dont on change le taux d'armement cible pour la catégorie SOFF
|
||||
|
||||
:type instance: objet du modèle FormationEmploi
|
||||
:param instance: FE dont on change le taux d'armement cible pour la catégorie SOFF
|
||||
|
||||
"""
|
||||
taux = instance.fe_taux_armement_cible_soff
|
||||
fe_id = instance.fe_code
|
||||
|
||||
postes = Poste.objects.filter(formation_emploi_id=fe_id, p_categorie='SOFF')
|
||||
if (len(postes) > 0) and (taux is not None):
|
||||
taux = taux * 0.01
|
||||
|
||||
for poste in postes:
|
||||
new_nb_reevalue = np.ceil(poste.p_nb_reo * taux)
|
||||
if new_nb_reevalue != poste.p_nb_reevalue:
|
||||
if new_nb_reevalue > poste.p_nb_reevalue:
|
||||
new_nb_p4 = poste.p_nb_p4 + new_nb_reevalue - poste.p_nb_reevalue
|
||||
new_nb_non_etudie = (new_nb_reevalue - new_nb_p4) if (new_nb_reevalue - new_nb_p4) > 0 else 0
|
||||
|
||||
elif new_nb_reevalue > poste.p_nb_occupe:
|
||||
new_nb_p4 = new_nb_reevalue - poste.p_nb_occupe
|
||||
new_nb_non_etudie = new_nb_reevalue - new_nb_p4
|
||||
poste.p_nb_p3 = 0
|
||||
poste.p_nb_p2 = 0
|
||||
poste.p_nb_p1 = 0
|
||||
else:
|
||||
new_nb_p4 = 0
|
||||
new_nb_non_etudie = 0
|
||||
poste.p_nb_p3 = 0
|
||||
poste.p_nb_p2 = 0
|
||||
poste.p_nb_p1 = 0
|
||||
poste.p_nb_p4 = 0
|
||||
new_nb_reevalue = poste.p_nb_occupe
|
||||
|
||||
poste.p_nb_non_etudie = new_nb_non_etudie
|
||||
poste.p_nb_p4 = new_nb_p4
|
||||
poste.p_nb_reevalue = new_nb_reevalue
|
||||
poste.save(
|
||||
update_fields=['p_nb_reevalue', 'p_nb_non_etudie', 'p_nb_p4', 'p_nb_p3', 'p_nb_p2',
|
||||
'p_nb_p1'])
|
||||
|
||||
|
||||
post_save.connect(update_taux_soff, sender=FormationEmploi)
|
||||
|
||||
|
||||
def add_sva(sender, instance, **kwargs):
|
||||
"""Met à jour les sous-viviers des militaires ayant la même filière de la nouvelle instance de SousVivierAssociation créée
|
||||
|
||||
:param instance: La nouvelle instance de SousVivierAssociation créée
|
||||
:type instance: objet du modèle SousVivierAssociation
|
||||
|
||||
"""
|
||||
filiere_id = instance.filiere_id
|
||||
sva_categorie = instance.sva_categorie
|
||||
sous_vivier_id = instance.sous_vivier_id
|
||||
administres = Administre.objects.filter(a_filiere_id=filiere_id, a_categorie=sva_categorie)
|
||||
administres.update(sous_vivier_id=sous_vivier_id)
|
||||
|
||||
|
||||
post_save.connect(add_sva, sender=SousVivierAssociation)
|
||||
Reference in New Issue
Block a user