529 lines
23 KiB
Python
529 lines
23 KiB
Python
"""
|
|
Ce fichier permet de gerer les apprenants.
|
|
|
|
Un apprenant est crée apres la validation d'une inscription ou sans.
|
|
|
|
"""
|
|
import pymongo
|
|
from pymongo import MongoClient
|
|
import json
|
|
from bson import ObjectId
|
|
import re
|
|
from datetime import datetime
|
|
import prj_common as mycommon
|
|
import secrets
|
|
import inspect
|
|
import sys, os
|
|
import csv
|
|
import pandas as pd
|
|
from pymongo import ReturnDocument
|
|
import GlobalVariable as MYSY_GV
|
|
from math import isnan
|
|
import GlobalVariable as MYSY_GV
|
|
import ela_index_bdd_classes as eibdd
|
|
import email_mgt as email
|
|
|
|
|
|
"""
|
|
Ajout d'un apprenant.
|
|
La clé est l'adresse email
|
|
"""
|
|
|
|
# xxxx - log historique dans les fonction
|
|
def Add_Apprenant(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'email', 'nom', 'prenom', 'telephone', 'opco', 'employeur', 'comment',
|
|
'client_rattachement_id', 'adresse', 'code_postal', 'ville', 'pays',
|
|
'tuteur1_nom', 'tuteur1_prenom', 'tuteur1_email', 'tuteur1_telephone', 'tuteur1_adresse',
|
|
'tuteur1_cp', 'tuteur1_ville', 'tuteur1_pays', 'tuteur1_include_com',
|
|
'tuteur2_nom', 'tuteur2_prenom', 'tuteur2_email', 'tuteur2_telephone', 'tuteur2_adresse',
|
|
'tuteur2_cp', 'tuteur2_ville', 'tuteur2_pays', 'tuteur2_include_com',
|
|
]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token','email', 'nom', 'prenom' ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verifier que l'adresse email est valide
|
|
if( "email" in diction.keys() and diction['email']):
|
|
if( mycommon.isEmailValide(str(diction['email'])) is False ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '"+str(diction['email'])+"' n'est pas valide")
|
|
return False, " L'adresse email '"+str(diction['email'])+"' n'est pas valide ",
|
|
|
|
|
|
# Verifier l'adresse email du tuteur 1
|
|
if ("tuteur1_email" in diction.keys() and diction['tuteur1_email']):
|
|
if (mycommon.isEmailValide(str(diction['tuteur1_email'])) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '" + str(diction['tuteur1_email']) + "' n'est pas valide")
|
|
return False, " L'adresse email '" + str(diction['tuteur1_email']) + "' n'est pas valide ",
|
|
|
|
|
|
# Verifier l'adresse email du tuteur 2
|
|
if ("tuteur2_email" in diction.keys() and diction['tuteur2_email']):
|
|
if (mycommon.isEmailValide(str(diction['tuteur2_email'])) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '" + str(
|
|
diction['tuteur2_email']) + "' n'est pas valide")
|
|
return False, " L'adresse email '" + str(diction['tuteur2_email']) + "' n'est pas valide ",
|
|
|
|
|
|
# Verifier que le client_rattachement_id est valide
|
|
if ("client_rattachement_id" in diction.keys() and diction['client_rattachement_id']):
|
|
is_client_rattachement_id_valide = MYSY_GV.dbname['partner_client'].count_documents({'_id':ObjectId(str(diction['client_rattachement_id'])),
|
|
'valide':'1',
|
|
'locked':'0',
|
|
'partner_recid':str(my_partner['recid'])})
|
|
if( is_client_rattachement_id_valide <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant du client n'est pas valide")
|
|
return False, " L'identifiant du client n'est pas valide ",
|
|
|
|
|
|
|
|
# Verifier si un apprenant n'a pas deja ce meme email
|
|
is_apprenant_exist = MYSY_GV.dbname['apprenant'].count_documents({'email':str(diction['email']),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1'})
|
|
|
|
if( is_apprenant_exist > 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Il existe déjà un apprenant avec la même adresse email. ")
|
|
return False, " Il existe déjà un apprenant avec la même adresse email. ",
|
|
|
|
new_data = diction
|
|
del new_data['token']
|
|
new_data['valide'] = '1'
|
|
new_data['locked'] = '0'
|
|
new_data['date_update'] = str(datetime.now())
|
|
new_data['update_by'] = str(my_partner['_id'])
|
|
|
|
inserted_id = MYSY_GV.dbname['apprenant'].insert_one(new_data).inserted_id
|
|
if (not inserted_id):
|
|
mycommon.myprint(
|
|
" Impossible de créer l'apprenant (1) ")
|
|
return False, " Impossible de créer l'apprenant (1) "
|
|
|
|
return True, "L'apprenant a été correctement ajouté "
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de créer l'apprenant "
|
|
|
|
|
|
"""
|
|
Mise à jour d'un apprenant.
|
|
La clé : _id
|
|
"""
|
|
def Update_Apprenant(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id', 'email', 'nom', 'prenom', 'telephone', 'opco', 'employeur', 'comment',
|
|
'client_rattachement_id', 'adresse', 'code_postal', 'ville', 'pays',
|
|
'tuteur1_nom', 'tuteur1_prenom', 'tuteur1_email', 'tuteur1_telephone', 'tuteur1_adresse',
|
|
'tuteur1_cp', 'tuteur1_ville', 'tuteur1_pays', 'tuteur1_include_com',
|
|
'tuteur2_nom', 'tuteur2_prenom', 'tuteur2_email', 'tuteur2_telephone', 'tuteur2_adresse',
|
|
'tuteur2_cp', 'tuteur2_ville', 'tuteur2_pays', 'tuteur2_include_com',
|
|
]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id']
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verifier que l'apprenant existe deja
|
|
is_apprenant_exist = MYSY_GV.dbname['apprenant'].count_documents({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
if( is_apprenant_exist <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de l'apprenant n'est pas valide ")
|
|
return False, " L'identifiant de l'apprenant n'est pas valide ",
|
|
|
|
|
|
# Verifier que l'adresse email est valide
|
|
if ("email" in diction.keys() and diction['email']):
|
|
if (mycommon.isEmailValide(str(diction['email'])) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '" + str(diction['email']) + "' n'est pas valide")
|
|
return False, " L'adresse email '" + str(diction['email']) + "' n'est pas valide ",
|
|
|
|
# Verifier l'adresse email du tuteur 1
|
|
if ("tuteur1_email" in diction.keys() and diction['tuteur1_email']):
|
|
if (mycommon.isEmailValide(str(diction['tuteur1_email'])) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '" + str(
|
|
diction['tuteur1_email']) + "' n'est pas valide")
|
|
return False, " L'adresse email '" + str(diction['tuteur1_email']) + "' n'est pas valide ",
|
|
|
|
# Verifier l'adresse email du tuteur 2
|
|
if ("tuteur2_email" in diction.keys() and diction['tuteur2_email']):
|
|
if (mycommon.isEmailValide(str(diction['tuteur2_email'])) is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'adresse email '" + str(
|
|
diction['tuteur2_email']) + "' n'est pas valide")
|
|
return False, " L'adresse email '" + str(diction['tuteur2_email']) + "' n'est pas valide ",
|
|
|
|
# Verifier que le client_rattachement_id est valide
|
|
if ("client_rattachement_id" in diction.keys() and diction['client_rattachement_id']):
|
|
is_client_rattachement_id_valide = MYSY_GV.dbname['partner_client'].count_documents(
|
|
{'_id': ObjectId(str(diction['client_rattachement_id'])),
|
|
'valide': '1',
|
|
'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
if (is_client_rattachement_id_valide <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant du client n'est pas valide")
|
|
return False, " L'identifiant du client n'est pas valide ",
|
|
|
|
apprenant_id = str(diction['_id'])
|
|
new_data = diction
|
|
del new_data['token']
|
|
del new_data['_id']
|
|
|
|
new_data['valide'] = '1'
|
|
new_data['locked'] = '0'
|
|
new_data['date_update'] = str(datetime.now())
|
|
new_data['update_by'] = str(my_partner['_id'])
|
|
|
|
ret_val = MYSY_GV.dbname['apprenant'].find_one_and_update(
|
|
{'_id': ObjectId(str(apprenant_id)),
|
|
'partner_owner_recid':str(my_partner['recid'])},
|
|
{"$set": new_data},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ret_val is None or ret_val['_id'] is None:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Impossible de mettre à jour l'apprenant (1) ")
|
|
return False, " Impossible de mettre à jour l'apprenant (1) "
|
|
|
|
|
|
return True, "L'apprenant a été correctement mise à jour "
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de mettre à jour l'apprenant "
|
|
|
|
|
|
"""
|
|
Recuperer les données detaillée d'un apprenant
|
|
"""
|
|
def Get_Given_Apprenant_Data(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id']
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verifier que l'apprenant existe deja
|
|
is_apprenant_exist = MYSY_GV.dbname['apprenant'].count_documents({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
if( is_apprenant_exist <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de l'apprenant n'est pas valide ")
|
|
return False, " L'identifiant de l'apprenant n'est pas valide ",
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
for val in MYSY_GV.dbname['apprenant'].find_one({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1',
|
|
'locked':'0'}):
|
|
user = val
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
# Recuperer les données du client
|
|
if( "client_rattachement_id" in val.keys() and val['client_rattachement_id'] ):
|
|
client_data = MYSY_GV.dbname['partner_client'].find_one({'_id':ObjectId(str(val['client_rattachement_id'])),
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
user['client_nom'] = client_data['nom']
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
return True, RetObject
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les données de l'apprenant "
|
|
|
|
|
|
"""
|
|
Récuperer la liste des apprenants d'un partenaire
|
|
"""
|
|
def Get_List_Partner_Apprenant(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token']
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
for val in MYSY_GV.dbname['apprenant'].find_one({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1',
|
|
'locked': '0'}):
|
|
user = val
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
# Recuperer les données du client
|
|
if ("client_rattachement_id" in val.keys() and val['client_rattachement_id']):
|
|
client_data = MYSY_GV.dbname['partner_client'].find_one(
|
|
{'_id': ObjectId(str(val['client_rattachement_id'])),
|
|
'valide': '1',
|
|
'locked': '0'})
|
|
user['client_nom'] = client_data['nom']
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
return True, RetObject
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer la liste des apprenants "
|
|
|
|
|
|
"""
|
|
Inscrire un apprenant à un session de formation
|
|
"""
|
|
def Apprenant_Inscrire_Session(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id', 'session_id' ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id', 'session_id' ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verifier que l'apprenant existe
|
|
is_apprenant_exist = MYSY_GV.dbname['apprenant'].count_documents({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
if( is_apprenant_exist <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de l'apprenant n'est pas valide ")
|
|
return False, " L'identifiant de l'apprenant n'est pas valide ",
|
|
|
|
# Recuperer les données de l'apprenant
|
|
apprenant_data = MYSY_GV.dbname['apprenant'].count_documents({'_id': ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1',
|
|
'locked': '0'})
|
|
|
|
# Verifier que la session de formation existe et est valide
|
|
is_session_existe = MYSY_GV.dbname['session_formation'].count_documents({'_id':ObjectId(str(diction['session_id'])),
|
|
'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1',
|
|
})
|
|
|
|
if (is_apprenant_exist <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de la session de formation n'est pas valide ")
|
|
return False, " L'identifiant de la session de formation n'est pas valide ",
|
|
|
|
# Recuperer les données de la session
|
|
session_data = MYSY_GV.dbname['session_formation'].find_one(
|
|
{'_id': ObjectId(str(diction['session_id'])),
|
|
'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1',
|
|
})
|
|
|
|
|
|
new_data = {}
|
|
new_data['email'] = apprenant_data['email']
|
|
new_data['apprenant_id'] = str(apprenant_data['_id'])
|
|
new_data['session_id'] = str(session_data['_id'])
|
|
new_data['class_internal_url'] = session_data['class_internal_url']
|
|
new_data['status'] = "1"
|
|
|
|
|
|
new_data['inscription_validation_date'] = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
new_data['date_update'] = str(datetime.now())
|
|
new_data['update_by'] = str(my_partner['_id'])
|
|
new_data['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
"""
|
|
# Reste à faire : xxxx
|
|
- ajout dans la collection 'inscription'
|
|
- log historique
|
|
- envoie email inscription
|
|
"""
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible d'inscrire l'apprenant à la session de formation "
|
|
|
|
|