init
This commit is contained in:
155
backend-django/backend/utils.py
Normal file
155
backend-django/backend/utils.py
Normal file
@@ -0,0 +1,155 @@
|
||||
"""
|
||||
Ce module contient les Utilitaires du backend
|
||||
"""
|
||||
from datetime import datetime
|
||||
from random import randint
|
||||
import re
|
||||
from django.forms import model_to_dict
|
||||
|
||||
from backend import constants
|
||||
from backend.models import Administre, Poste, SousVivier, FormationEmploi
|
||||
|
||||
|
||||
def check_positive(valeur_nb):
|
||||
"""
|
||||
Vérifier si une valeur est positive et retourner 1, si elle est nulle ou négative retourner 0.
|
||||
|
||||
:type poste_nb: DataFrame
|
||||
:param poste_nb: ID du sous-vivier
|
||||
|
||||
|
||||
:return: - **valeur_nb_modifie** (*dataframe*): Dataframe contenant des valeurs 1 ou 0.
|
||||
"""
|
||||
valeur_nb_modifie = valeur_nb.apply(lambda x: 1 if x > 0 else 0)
|
||||
return valeur_nb_modifie
|
||||
|
||||
def cleanString(string):
|
||||
"""Cette fonction supprimera tous les caractères qui ne sont pas alphanumériques.
|
||||
|
||||
:type string: chaîne de caractères
|
||||
:param string: chaîne qui doit être nettoyée
|
||||
|
||||
:return: - **return** (*chaîne de caractères*): chaîne nettoyée.
|
||||
|
||||
"""
|
||||
# print(string)
|
||||
return ''.join([i for i in string if i.isalnum()])
|
||||
|
||||
|
||||
def intOrNone(value):
|
||||
"""Cette fonction renvoie l'entier de la valeur ou renvoie None.
|
||||
|
||||
:type value: float
|
||||
:param value: valeur a con
|
||||
|
||||
:return: - **res** (*int or None*): valeur à convertir.
|
||||
|
||||
"""
|
||||
try:
|
||||
res = int(float(value))
|
||||
except:
|
||||
res = None
|
||||
|
||||
return res
|
||||
|
||||
def impact_decisions(old_administre, administre, old_avis, avis, eip, fe_code, categorie):
|
||||
"""
|
||||
|
||||
:type administre: objet
|
||||
:param administre: instance du model Administre
|
||||
|
||||
:type old_avis: chaine de caractères
|
||||
:param old_avis: avis de l'administré avant la mise à jour
|
||||
|
||||
:type avis: chaine de caractère
|
||||
:param avis: nouvel avis de l'administré
|
||||
|
||||
:type eip: chaine de caractère
|
||||
:param eip: eip actuel du militaire
|
||||
|
||||
:type categorie: dataframe pandas
|
||||
:param categorie: Dataframe contenant les données pretraités à inserer
|
||||
|
||||
:return: - **list_error** (*liste*): liste des identifiants SAP pour lesquels il y a eu une erreur.
|
||||
|
||||
|
||||
"""
|
||||
|
||||
list_error = []
|
||||
if (old_avis == 'NON_ETUDIE' or old_avis == 'A_MAINTENIR' or
|
||||
old_avis == 'A_ETUDIER') and (
|
||||
avis == 'A_MUTER' or avis == 'PARTANT' or avis == 'NON_DISPONIBLE'):
|
||||
poste_qs = Poste.objects.filter(p_eip__iexact=eip, formation_emploi_id=fe_code).exclude(
|
||||
p_nb_occupe=0)
|
||||
poste = poste_qs.first()
|
||||
print(fe_code)
|
||||
fe = FormationEmploi.objects.get(fe_code=fe_code)
|
||||
if categorie == constants.CATEGORIE_MDR:
|
||||
fe.fe_nb_poste_vacant_mdr = fe.fe_nb_poste_vacant_mdr + 1
|
||||
fe.fe_nb_poste_occupe_mdr = fe.fe_nb_poste_occupe_mdr - 1
|
||||
elif categorie == constants.CATEGORIE_SOFF:
|
||||
fe.fe_nb_poste_vacant_soff = fe.fe_nb_poste_vacant_soff + 1
|
||||
fe.fe_nb_poste_occupe_soff = fe.fe_nb_poste_occupe_soff - 1
|
||||
elif categorie == constants.CATEGORIE_OFF:
|
||||
fe.fe_nb_poste_vacant_off = fe.fe_nb_poste_vacant_off + 1
|
||||
fe.fe_nb_poste_occupe_off = fe.fe_nb_poste_occupe_off - 1
|
||||
|
||||
fe.save()
|
||||
|
||||
|
||||
if poste and poste.p_nb_non_etudie > 0:
|
||||
print(poste)
|
||||
new_nb_p4, new_nb_non_etudie, new_nb_vacant, new_nb_occupe = poste.p_nb_p4 + 1, poste.p_nb_non_etudie - 1, poste.p_nb_vacant + 1, poste.p_nb_occupe - 1
|
||||
poste_qs.update(p_nb_p4=new_nb_p4, p_nb_non_etudie=new_nb_non_etudie,
|
||||
p_nb_vacant=new_nb_vacant,
|
||||
p_nb_occupe=new_nb_occupe)
|
||||
|
||||
else:
|
||||
list_error.append(administre.a_id_sap)
|
||||
|
||||
if (old_avis == 'A_MUTER' or old_avis == 'PARTANT' or
|
||||
old_avis == 'NON_DISPONIBLE') and (
|
||||
avis == 'NON_ETUDIE' or avis == 'A_MAINTENIR' or avis == 'A_ETUDIER'):
|
||||
poste_qs = Poste.objects.filter(p_eip__iexact=eip, formation_emploi_id=fe_code).exclude(p_nb_occupe=0)
|
||||
poste = poste_qs.first()
|
||||
fe = FormationEmploi.objects.get(fe_code=fe_code)
|
||||
if categorie == constants.CATEGORIE_MDR:
|
||||
fe.fe_nb_poste_vacant_mdr = fe.fe_nb_poste_vacant_mdr - 1
|
||||
fe.fe_nb_poste_occupe_mdr = fe.fe_nb_poste_occupe_mdr + 1
|
||||
elif categorie == constants.CATEGORIE_SOFF:
|
||||
fe.fe_nb_poste_vacant_soff = fe.fe_nb_poste_vacant_soff - 1
|
||||
fe.fe_nb_poste_occupe_soff = fe.fe_nb_poste_occupe_soff + 1
|
||||
elif categorie == constants.CATEGORIE_OFF:
|
||||
fe.fe_nb_poste_vacant_off = fe.fe_nb_poste_vacant_off - 1
|
||||
fe.fe_nb_poste_occupe_off = fe.fe_nb_poste_occupe_off + 1
|
||||
|
||||
fe.save()
|
||||
print(model_to_dict(fe))
|
||||
|
||||
if poste and poste.p_nb_p4 > 0:
|
||||
print(poste)
|
||||
new_nb_p4, new_nb_non_etudie, new_nb_vacant, new_nb_occupe = poste.p_nb_p4 - 1, poste.p_nb_non_etudie + 1, poste.p_nb_vacant - 1, poste.p_nb_occupe + 1
|
||||
poste_qs.update(p_nb_p4=new_nb_p4, p_nb_non_etudie=new_nb_non_etudie,
|
||||
p_nb_vacant=new_nb_vacant,
|
||||
p_nb_occupe=new_nb_occupe)
|
||||
if poste and poste.p_nb_p4 == 0 and poste.p_nb_p3 > 0:
|
||||
new_nb_p3, new_nb_non_etudie, new_nb_vacant, new_nb_occupe = poste.p_nb_p3 - 1, poste.p_nb_non_etudie + 1, poste.p_nb_vacant - 1, poste.p_nb_occupe + 1
|
||||
poste_qs.update(p_nb_p3=new_nb_p3, p_nb_non_etudie=new_nb_non_etudie,
|
||||
p_nb_vacant=new_nb_vacant,
|
||||
p_nb_occupe=new_nb_occupe)
|
||||
if poste and poste.p_nb_p4 == 0 and poste.p_nb_p3 == 0 and poste.p_nb_p2 > 0:
|
||||
new_nb_p2, new_nb_non_etudie, new_nb_vacant, new_nb_occupe = poste.p_nb_p2 - 1, poste.p_nb_non_etudie + 1, poste.p_nb_vacant - 1, poste.p_nb_occupe + 1
|
||||
poste_qs.update(p_nb_p2=new_nb_p2, p_nb_non_etudie=new_nb_non_etudie,
|
||||
p_nb_vacant=new_nb_vacant,
|
||||
p_nb_occupe=new_nb_occupe)
|
||||
if poste and poste.p_nb_p4 == 0 and poste.p_nb_p3 == 0 and poste.p_nb_p2 == 0 and poste.p_nb_p1 > 0:
|
||||
new_nb_p1, new_nb_non_etudie, new_nb_vacant, new_nb_occupe = poste.p_nb_p1 - 1, poste.p_nb_non_etudie + 1, poste.p_nb_vacant - 1, poste.p_nb_occupe + 1
|
||||
poste_qs.update(p_nb_p1=new_nb_p1, p_nb_non_etudie=new_nb_non_etudie,
|
||||
p_nb_vacant=new_nb_vacant,
|
||||
p_nb_occupe=new_nb_occupe)
|
||||
|
||||
else:
|
||||
list_error.append(administre.a_id_sap)
|
||||
|
||||
print(list_error)
|
||||
return list_error
|
||||
Reference in New Issue
Block a user