Elyos_FI_Back_Office/ent_student_common.py

276 lines
10 KiB
Python

"""
-- Gestion de l'espace ENT des apprenants
Ce fichier permet de gerer les fonction de ENT ETUDIANT
"""
import bson
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
import jinja2
from flask import send_file
from xhtml2pdf import pisa
from email.message import EmailMessage
from email.mime.text import MIMEText
from email import encoders
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
"""
Cette fonctions retourne les class_id d'un apprenant return : tab_class_id : [] - Uniquement les class_id
"""
def Get_Apprenant_Tab_Class_Id(diction):
try:
diction = mycommon.strip_dictionary(diction)
"""
Verification des input acceptés
"""
field_list = ['token', 'apprenant_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'existe pas")
return False, " Les informations fournies sont incorrectes",
"""
Verification des champs obligatoires
"""
field_list_obligatoire = ['token', 'apprenant_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 la liste des arguments ")
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 la validité de l'apprenant
"""
is_apprenant_valide_count = MYSY_GV.dbname['apprenant'].count_documents(
{'_id': ObjectId(str(diction['apprenant_id'])),
'valide': '1',
'locked': '0'})
if (is_apprenant_valide_count != 1):
mycommon.myprint(
str(inspect.stack()[0][
3]) + " L'identifiant de l'apprenant est invalide ")
return False, " L'identifiant de l'apprenant est invalide ",
apprenant_data = MYSY_GV.dbname['apprenant'].find_one(
{'_id': ObjectId(str(diction['apprenant_id'])),
'valide': '1',
'locked': '0'})
qry = {}
qry['partner_owner_recid'] = apprenant_data['partner_owner_recid']
qry['apprenant_id'] = diction['apprenant_id']
qry['valide'] = "1"
qry['locked'] = "0"
query = [{'$match': {'$and': [qry]}},
{'$sort': {'_id': -1}},
{'$lookup':
{
'from': 'myclass',
'localField': 'class_internal_url',
'foreignField': 'internal_url',
'pipeline': [{'$match': {'$and': [{}, {}]}},
{'$project': {'_id': 1, }}],
'as': 'myclass_collection'
}
},
{'$lookup':
{
'from': 'apprenant',
"let": {'apprenant_id': "$apprenant_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [{'$match':
{'$expr': {'$and': [
{'$eq': ["$valide", "1"]},
{'$eq': ["$_id", {'$convert': {
'input': "$$apprenant_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
]}}},
], 'as': 'apprenant_collection'}}
]
print("#### Get_Apprenant_Tab_Class_Id laa 01 : query = ", query)
RetObject = []
cpt = 0
for retVal in MYSY_GV.dbname['inscription'].aggregate(query):
val = {}
if ('myclass_collection' in retVal.keys() and len(retVal['myclass_collection']) > 0):
for class_data in retVal['myclass_collection']:
RetObject.append(str(class_data['_id']))
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 la liste des class_id de l'apprenant "
"""
Recuperer la liste des formation d'un apprenant pour son espace ENT
"""
def Get_Ent_Student_List_Class(diction):
try:
diction = mycommon.strip_dictionary(diction)
"""
Verification des input acceptés
"""
field_list = ['token', 'apprenant_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'existe pas")
return False, " Les informations fournies sont incorrectes",
"""
Verification des champs obligatoires
"""
field_list_obligatoire = ['token', 'apprenant_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 la liste des arguments ")
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 la validité de l'apprenant
"""
is_apprenant_valide_count = MYSY_GV.dbname['apprenant'].count_documents({'_id':ObjectId(str(diction['apprenant_id'])),
'valide':'1',
'locked':'0'})
if( is_apprenant_valide_count != 1):
mycommon.myprint(
str(inspect.stack()[0][
3]) + " L'identifiant de l'apprenant est invalide ")
return False, " L'identifiant de l'apprenant est invalide ",
apprenant_data = MYSY_GV.dbname['apprenant'].find_one(
{'_id': ObjectId(str(diction['apprenant_id'])),
'valide': '1',
'locked': '0'})
qry = {}
qry['partner_owner_recid'] = apprenant_data['partner_owner_recid']
qry['apprenant_id'] = diction['apprenant_id']
qry['valide'] = "1"
qry['locked'] = "0"
query = [{'$match': {'$and': [qry ]}},
{'$sort': {'_id': -1}},
{'$lookup':
{
'from': 'myclass',
'localField': 'class_internal_url',
'foreignField': 'internal_url',
'pipeline': [{'$match': {'$and': [{}, {}]}},
{'$project': {'title': 1, 'domaine': 1,
'duration': 1,
'duration_unit': 1,
'_id': 1,
'recyclage_delai': 1,
'recyclage_periodicite': 1,
'recyclage_alert': 1}}],
'as': 'myclass_collection'
}
},
{'$lookup':
{
'from': 'apprenant',
"let": {'apprenant_id': "$apprenant_id", 'partner_owner_recid': '$partner_owner_recid'},
'pipeline': [{'$match':
{'$expr': {'$and': [
{'$eq': ["$valide", "1"]},
{'$eq': ["$_id", {'$convert': {
'input': "$$apprenant_id",
'to': "objectId",
'onError': {'error': 'true'},
'onNull': {'isnull': 'true'}
}}]},
]}}},
], 'as': 'apprenant_collection'}}
]
print("#### Get_Ent_Student_Class laa 01 : query = ", query)
RetObject = []
cpt = 0
for retVal in MYSY_GV.dbname['inscription'].aggregate(query):
val = {}
if ('myclass_collection' in retVal.keys() and len(retVal['myclass_collection']) > 0):
val['id'] = str(cpt)
cpt = cpt + 1
val['_id'] = retVal['_id']
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 formations de l'apprenant "