""" Ce fichier permet de gerer les équipes d'employés. Ceci est utlisé par exemple pour gerer les éqiupes commerciales une équipe est defini par : - code - description - email group - chef d'équipe - membres (employé) """ 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 """ Ajout d'une équipe """ def Add_Equipe_Team(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'code', 'description', 'email_equipe', 'chef_equipe_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', 'code', 'description', ] 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 que ce code n'existe pas déjà is_existe_equipe_team = MYSY_GV.dbname['equipe_team'].count_documents({'code': str(diction['code']), 'valide': '1', 'partner_owner_recid': str( my_partner['recid'])}) if (is_existe_equipe_team > 0): mycommon.myprint( str(inspect.stack()[0][3]) + " Une équipe avec le code '" + str(diction['code']) + "' existe déjà ") return False, " Une équipe avec le code '" + str(diction['code']) + "' existe déjà " # Verifier si le chef d'equipe existe if( "chef_equipe_id" in diction.keys() and diction['chef_equipe_id']): # Verifier que l'id du chef d'equipe est valide is_chef_equipe_id_count = MYSY_GV.dbname['ressource_humaine'].count_documents({'_id':ObjectId(str(diction['chef_equipe_id'])), 'valide':'1', 'locked':'0', 'partner_recid':str(my_partner['recid'])}) if( is_chef_equipe_id_count != 1 ): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant du chef d'équipe est invalide ") return False, " L'identifiant du chef d'équipe est invalide " if ("email_equipe" in diction.keys() and diction['email_equipe']): if (mycommon.isEmailValide(str(diction['email_equipe'])) is False): mycommon.myprint( str(inspect.stack()[0][3]) + " L'adresse email de l'équipe est invalide ") return False, " L'adresse email de l'équipe est invalide " new_data = diction mytoken = diction['token'] del diction['token'] new_data['valide'] = "1" new_data['locked'] = "0" new_data['date_update'] = str(datetime.now()) new_data['date_creation'] = str(datetime.now()) new_data['update_by'] = str(my_partner['_id']) new_data['partner_owner_recid'] = str(my_partner['recid']) inserted_id = MYSY_GV.dbname['equipe_team'].insert_one(new_data).inserted_id if (not inserted_id): mycommon.myprint( " Impossible de créer l'équipe (2) ") return False, " Impossible de créer l'équipe (2) " """ Après la création de l'equipe, on ajoute le chef d'équipe dans liste de membres """ if( "chef_equipe_id" in diction.keys() and diction['chef_equipe_id']): new_data = {} new_data['equipe_team_id'] = str(inserted_id) new_data['rh_id'] = str(diction['chef_equipe_id']) now = str(datetime.now()) mytoday = datetime.today() new_data['date_ajout_equipe'] = str(mytoday) new_data['date_update'] = now new_data['valide'] = "1" new_data['locked'] = "0" new_data['partner_owner_recid'] = str(my_partner['recid']) new_data['update_by'] = str(my_partner['_id']) new_data['leader'] = "1" data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['equipe_team_id'] = str(diction['_id']) data_cle['rh_id'] = str(diction['chef_equipe_id']) data_cle['valide'] = "1" data_cle['locked'] = "0" result = MYSY_GV.dbname['equipe_team_membre'].find_one_and_update( data_cle, {"$set": new_data}, upsert=True, return_document=ReturnDocument.AFTER ) if (result is None or "_id" not in result.keys()): mycommon.myprint( " WARNING : Impossible d'ajouter le chef d'équipe") """ ## Add to log history """ now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) # Pour la collection inscription history_event_dict = {} history_event_dict['token'] = mytoken history_event_dict['related_collection'] = "equipe_team" history_event_dict['related_collection_recid'] = str(inserted_id) history_event_dict['action_date'] = str(now) history_event_dict['action_description'] = "Creation Equipe " local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict) if (local_status is False): mycommon.myprint( " WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict)) return True, " L'équipe a été correctement créée" 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'équipe " """ Ajouter une liste de membres à une équipe """ def Add_Update_Equipe_Team_Membres(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', '_id', 'tab_ressource_humaine_ids', 'role'] 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 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 new_data = diction # Verifier que l'équipe existe et est valide qry = {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])} is_existe_groupe = MYSY_GV.dbname['equipe_team'].count_documents( {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) if (is_existe_groupe != 1): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant de l'équipe est invalide ") return False, " L'identifiant de l'équipe est invalide " tab_ressource_humaine_ids = "" if ("tab_ressource_humaine_ids" in diction.keys()): if diction['tab_ressource_humaine_ids']: tab_ressource_humaine_ids = diction['tab_ressource_humaine_ids'] tab_ressource_humaine_ids_splited = str(tab_ressource_humaine_ids).split(",") # Controle de validité des ressource_humaine for my_ressource_humaine in tab_ressource_humaine_ids_splited: # Verifier que l'inscription est valide my_ressource_humaine_is_valide = MYSY_GV.dbname['ressource_humaine'].count_documents( {'_id': ObjectId(str(my_ressource_humaine)), 'valide': '1', 'locked':'0', 'partner_recid': str(my_partner['recid'])}) if (my_ressource_humaine_is_valide != 1): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant de la ressource humaine '" + my_ressource_humaine + "' est invalide ") return False, " L'identifiant de la ressource humaine '" + my_ressource_humaine + "' est invalide " cpt = 0 for my_ressource_humaine in tab_ressource_humaine_ids_splited: new_data = {} new_data['equipe_team_id'] = str(diction['_id']) new_data['rh_id'] = str(my_ressource_humaine) if( "role" in diction.keys()): new_data['role'] = str(diction['role'])[0:255] else: new_data['role'] = "" now = str(datetime.now()) mytoday = datetime.today() new_data['date_ajout_equipe'] = str(mytoday) new_data['date_update'] = now new_data['valide'] = "1" new_data['locked'] = "0" new_data['partner_owner_recid'] = str(my_partner['recid']) new_data['update_by'] = str(my_partner['_id']) data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['equipe_team_id'] = str(diction['_id']) data_cle['rh_id'] = str(my_ressource_humaine) data_cle['valide'] = "1" data_cle['locked'] = "0" result = MYSY_GV.dbname['equipe_team_membre'].find_one_and_update( data_cle, {"$set": new_data}, upsert=True, return_document=ReturnDocument.AFTER ) if (result is None or "_id" not in result.keys()): mycommon.myprint( " Impossible d'inscrire les personnes à l'équipe (2) ") return False, " Impossible d'inscrire les personnes à l'équipe (2) " cpt = cpt + 1 """ ## Add to log history """ now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) # Pour la collection inscription history_event_dict = {} history_event_dict['token'] = str(diction['token']) history_event_dict['related_collection'] = "equipe_team" history_event_dict['related_collection_recid'] = str(diction['_id']) history_event_dict['action_date'] = str(now) history_event_dict['action_description'] = "Mise à jour des membres " local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict) if (local_status is False): mycommon.myprint( " WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict)) return True, str(cpt)+ " Membre(s) inscrit(s)" 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 les personnes à l'équipe " """ Cette fonction permet de supprimer des membres d'une équipe """ def Delete_Equipe_Team_Membre(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'equipe_id', 'tab_ids'] 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', 'equipe_id', 'tab_ids' ] 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 new_data = diction # Verifier que l'equipe existe et est valide is_existe_groupe = MYSY_GV.dbname['equipe_team'].count_documents( {'_id': ObjectId(str(diction['equipe_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) if (is_existe_groupe != 1): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant de l'équipe est invalide ") return False, " L'identifiant de l'équipe est invalide " is_existe_groupe = MYSY_GV.dbname['equipe_team'].find_one( {'_id': ObjectId(str(diction['equipe_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) update_team_leader = "0" equipe_team_chef_equipe_id = "" if(is_existe_groupe and "chef_equipe_id" in is_existe_groupe ): equipe_team_chef_equipe_id = is_existe_groupe['chef_equipe_id'] tab_equipe_team_membre_ids = "" if ("tab_ids" in diction.keys()): if diction['tab_ids']: tab_equipe_team_membre_ids = diction['tab_ids'] tab_equipe_team_membre_ids_splited = str(tab_equipe_team_membre_ids).split(",") tab_equipe_team_membre_ids_splited_ObjectID = [] for tmp in tab_equipe_team_membre_ids_splited : tab_equipe_team_membre_ids_splited_ObjectID.append(ObjectId(str(tmp))) qery_delete = {'_id': {'$in': tab_equipe_team_membre_ids_splited_ObjectID}, 'equipe_team_id':str(diction['equipe_id']), 'partner_owner_recid': str(my_partner['recid']), 'locked': '0'} """ Pour les log d'historique, recuperation des information sur les membres supprimés - rh_id, - email et Si la personne supprimée est le responsable, il faut alors mettre à vide le champ responsable sur la formation """ tab_delete_rh = [] for val in MYSY_GV.dbname['equipe_team_membre'].find(qery_delete): if( val and "rh_id" in val.keys() ): rh_id_data = MYSY_GV.dbname['ressource_humaine'].find_one({'_id':ObjectId(val['rh_id']), 'valide':'1', 'locked':'0', 'partner_recid':str(my_partner['recid']),}) node_delete_rh_id_data = {} if( rh_id_data and '_id' in rh_id_data.keys() and 'email' in rh_id_data.keys()): node_delete_rh_id_data['rh_id'] = rh_id_data['_id'] node_delete_rh_id_data['email'] = rh_id_data['email'] if (rh_id_data and 'nom' in rh_id_data.keys() ): node_delete_rh_id_data['nom'] = rh_id_data['nom'] if (rh_id_data and 'prenom' in rh_id_data.keys() ): node_delete_rh_id_data['prenom'] = rh_id_data['prenom'] tab_delete_rh.append(node_delete_rh_id_data) """ Si la personne supprimée est le responsable, il faut alors mettre à vide le champ responsable sur la formation """ if (str(rh_id_data['_id']) == equipe_team_chef_equipe_id): result = MYSY_GV.dbname['equipe_team'].find_one_and_update( {'_id': ObjectId(str(diction['equipe_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}, {"$set": {"chef_equipe_id":""}}, upsert=False, return_document=ReturnDocument.AFTER ) history_massage_light = "" for val in tab_delete_rh : if("email" in val ): history_massage_light = str(val['email'])+", "+str(history_massage_light) #print(" ### qery_delete = ", qery_delete) delete = MYSY_GV.dbname['equipe_team_membre'].delete_many(qery_delete ) """ ## Add to log history """ now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) # Pour la collection inscription history_event_dict = {} history_event_dict['token'] = diction['token'] history_event_dict['related_collection'] = "equipe_team" history_event_dict['related_collection_recid'] = str(diction['equipe_id']) history_event_dict['action_date'] = str(now) history_event_dict['technical_comment'] = "Suppression membre(s) " + str(tab_delete_rh) history_event_dict['action_description'] = "Suppression membre(s) " + str(history_massage_light) local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict) if (local_status is False): mycommon.myprint( " WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict)) return True, str(delete.deleted_count) + " Membre(s) désinscrit(s)" 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ésinscrire les personnes de l'équipe " """ Mise à jour d'une équipe """ def Update_Equipe_Team(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'code', 'description', 'email_equipe', 'chef_equipe_id', '_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 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 new_data = diction # Verifier que l'équipe existe is_existe_cdtion_paiement = MYSY_GV.dbname['equipe_team'].count_documents( {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) if (is_existe_cdtion_paiement != 1): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant de l'équipe est invalide ") return False, " L'identifiant l'équipe est invalide " # Verifier que ce code n'existe pas déjà is_existe_equipe_team = MYSY_GV.dbname['equipe_team'].count_documents({'code': str(diction['code']), 'valide': '1', 'partner_owner_recid': str( my_partner['recid']), "_id": {"$ne": ObjectId(str(diction['_id']))} }) if (is_existe_equipe_team > 0): mycommon.myprint( str(inspect.stack()[0][3]) + " Une équipe avec le code '" + str(diction['code']) + "' existe déjà ") return False, " Une équipe avec le code '" + str(diction['code']) + "' existe déjà " # Verifier si le chef d'equipe existe if ("chef_equipe_id" in diction.keys() and diction['chef_equipe_id']): # Verifier que l'id du chef d'equipe est valide is_chef_equipe_id_count = MYSY_GV.dbname['ressource_humaine'].count_documents( {'_id': ObjectId(str(diction['chef_equipe_id'])), 'valide': '1', 'locked': '0', 'partner_recid': str(my_partner['recid'])}) if (is_chef_equipe_id_count !=1 ): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant du chef d'équipe est invalide ") return False, " L'identifiant du chef d'équipe est invalide " if ("email_equipe" in diction.keys() and diction['email_equipe']): if (mycommon.isEmailValide(str(diction['email_equipe'])) is False): mycommon.myprint( str(inspect.stack()[0][3]) + " L'adresse email de l'équipe est invalide ") return False, " L'adresse email de l'équipe est invalide " local_id = str(diction['_id']) mytoken = diction['token'] del diction['token'] del diction['_id'] new_data['date_update'] = str(datetime.now()) new_data['update_by'] = str(my_partner['_id']) data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['_id'] = ObjectId(local_id) data_cle['valide'] = "1" data_cle['locked'] = "0" result = MYSY_GV.dbname['equipe_team'].find_one_and_update( data_cle, {"$set": new_data}, upsert=False, return_document=ReturnDocument.AFTER ) if ("_id" not in result.keys()): mycommon.myprint( " Impossible de mettre à jour l'équipe (2) ") return False, " Impossible de mettre à jour l'équipe (2) " """ Après la création de l'equipe, on ajoute le chef d'équipe dans liste de membres """ if ("chef_equipe_id" in diction.keys() and diction['chef_equipe_id']): new_data = {} new_data['equipe_team_id'] = str(local_id) new_data['rh_id'] = str(diction['chef_equipe_id']) now = str(datetime.now()) mytoday = datetime.today() new_data['date_ajout_equipe'] = str(mytoday) new_data['date_update'] = now new_data['valide'] = "1" new_data['locked'] = "0" new_data['partner_owner_recid'] = str(my_partner['recid']) new_data['update_by'] = str(my_partner['_id']) new_data['leader'] = "1" data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['equipe_team_id'] = str(local_id) data_cle['rh_id'] = str(diction['chef_equipe_id']) data_cle['valide'] = "1" data_cle['locked'] = "0" """ D'abord enlever le statut de leader si qq'un d'autre l'avait """ update = MYSY_GV.dbname['equipe_team_membre'].update_many( { "partner_owner_recid": str(my_partner['recid']), "equipe_team_id": str(local_id) }, {"$unset": {'leader':''}}, ) """ A présent mise à jour du leader, avec potentiel ajout s'il n'existe pas """ print(" ### mise à jour du leader , data_cle = ", data_cle) result = MYSY_GV.dbname['equipe_team_membre'].find_one_and_update( data_cle, {"$set": new_data}, upsert=True, return_document=ReturnDocument.AFTER ) if (result is None or "_id" not in result.keys()): mycommon.myprint( " WARNING : Impossible d'ajouter le chef d'équipe") """ ## Add to log history """ now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) # Pour la collection inscription history_event_dict = {} history_event_dict['token'] = mytoken history_event_dict['related_collection'] = "equipe_team" history_event_dict['related_collection_recid'] = str(local_id) history_event_dict['action_date'] = str(now) history_event_dict['action_description'] = "Mise à jour " local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict) if (local_status is False): mycommon.myprint( " WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict)) return True, " L'équipe 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'équipe " """ Suppression d'une équipe regles : Si la condition (_id) n'est pas utiliser dans les collections - xxxxxx (à préciser) - pas de suppression si groupe à des membre. L'utilisateur devra supprimer les memebre d'abord """ def Delete_Equipe_Team(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 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 is_existe_equipe_team = MYSY_GV.dbname['equipe_team'].count_documents( {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) if (is_existe_equipe_team != 1): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant de l'équipe est invalide ") return False, " L'identifiant de l'équipe est invalide " nb_membres = MYSY_GV.dbname['equipe_team_membre'].count_documents({'equipe_team_id': str(diction['_id']), 'partner_owner_recid': str( my_partner['recid']), }, ) if( nb_membres > 0 ): mycommon.myprint( str(inspect.stack()[0][3]) + " Ce groupe à "+str(nb_membres)+" membre(s) actif(s). Vous devez supprimer les membres avant de supprimer l'équipe ") return False, " Ce groupe à "+str(nb_membres)+" membre(s) actif(s). Vous devez supprimer les membres avant de supprimer l'équipe " is_existe_equipe_team_date = MYSY_GV.dbname['equipe_team'].find_one( {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) """ Verifier que le domaine de formation n'est pas utilisé (collection : myclass) "" " is_domaine_in_myclass = MYSY_GV.dbname['myclass'].count_documents({'partner_owner_recid':my_partner['recid'], 'valide':'1', 'class_domaine_id':str(diction['_id'])}) if( is_domaine_in_myclass > 0 ): mycommon.myprint( str(inspect.stack()[0][3]) + " Cette catégorie de formation est utilisée dans "+str(is_domaine_in_myclass)+" formations ") return False, " Cette catégorie de formation est utilisée dans "+str(is_domaine_in_myclass)+" formations " """ # Suppression des membres delete_membres = MYSY_GV.dbname['equipe_team_membre'].delete_one({'equipe_team_id': str(diction['_id']), 'partner_owner_recid': str(my_partner['recid']), }, ) delete = MYSY_GV.dbname['equipe_team'].delete_one({'_id': ObjectId(str(diction['_id'])), 'partner_owner_recid': str(my_partner['recid']), }, ) """ ## Add to log history """ now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S")) # Pour la collection inscription history_event_dict = {} history_event_dict['token'] = diction['token'] history_event_dict['related_collection'] = "equipe_team" history_event_dict['related_collection_recid'] = str(diction['_id']) history_event_dict['action_date'] = str(now) history_event_dict['action_description'] = "Suppression de l'equipe : _id = " + str(is_existe_equipe_team_date['_id'])+", code = "+ str(is_existe_equipe_team_date['code']) local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict) if (local_status is False): mycommon.myprint( " WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict)) return True, " L'équipe a été correctement supprimée" 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 supprimer l'équipe " """ Recuperer la liste des équipes d'un partenaire """ def Get_List_Equipe_Team(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'existe pas") 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 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 """ Clés de mise à jour """ data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['valide'] = "1" data_cle['locked'] = "0" RetObject = [] val_tmp = 0 for retval in MYSY_GV.dbname['equipe_team'].find(data_cle).sort([("_id", pymongo.DESCENDING), ]): user = retval user['id'] = str(val_tmp) val_tmp = val_tmp + 1 # Recuperer le nombre de membre nb_membre = MYSY_GV.dbname['equipe_team_membre'].count_documents({'partner_owner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0', 'equipe_team_id':str(retval['_id'])}) user['nb_membre'] = str(nb_membre) #Recuperation des nom et prenom du responsable (chef d'equipe) chef_equipe_nom_prenom = "" if( "chef_equipe_id" in retval.keys() and retval['chef_equipe_id']): chef_equipe_id_data = MYSY_GV.dbname['ressource_humaine'].find_one({'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0', '_id':ObjectId(str(retval['chef_equipe_id']))}) if( chef_equipe_id_data and 'nom' in chef_equipe_id_data.keys()): chef_equipe_nom_prenom = chef_equipe_id_data['nom'] if (chef_equipe_id_data and 'prenom' in chef_equipe_id_data.keys()): chef_equipe_nom_prenom = chef_equipe_nom_prenom+ " "+chef_equipe_id_data['prenom'] user['chef_equipe_nom_prenom'] = str(chef_equipe_nom_prenom) 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 équipes " """ Recuperer les données d'une équipe donnée, y compris les membres """ def Get_Given_Equipe_Team_With_Members(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'existe pas") 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 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 """ Clés de mise à jour """ data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['recid']) data_cle['valide'] = "1" data_cle['locked'] = "0" data_cle['_id'] = ObjectId(str(diction['_id'])) RetObject = [] val_tmp = 0 for retval in MYSY_GV.dbname['equipe_team'].find(data_cle): user = retval user['id'] = str(val_tmp) # Aller chercher les membres list_membre = [] for team_membre in MYSY_GV.dbname['equipe_team_membre'].find({"partner_owner_recid": str(my_partner['recid']), 'valide':'1', 'locked':'0', 'equipe_team_id':str(diction['_id'])}): membre_rh_data = MYSY_GV.dbname['ressource_humaine'].find_one({'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0', '_id':ObjectId(str(team_membre['rh_id']))}) if(membre_rh_data and '_id' in membre_rh_data.keys() ): node_membre = {} node_membre['_id'] = str(team_membre['_id']) node_membre['equipe_team_id'] = str(retval['_id']) node_membre['rh_id'] = str(membre_rh_data['_id']) if( "nom" in membre_rh_data.keys()): node_membre['nom'] = str(membre_rh_data['nom']) else: node_membre['nom'] = "" if ("prenom" in membre_rh_data.keys()): node_membre['prenom'] = str(membre_rh_data['prenom']) else: node_membre['prenom'] = "" if ("civilite" in membre_rh_data.keys()): node_membre['civilite'] = str(membre_rh_data['civilite']) else: node_membre['civilite'] = "" if ("email" in membre_rh_data.keys()): node_membre['email'] = str(membre_rh_data['email']) else: node_membre['email'] = "" if ("telephone" in membre_rh_data.keys()): node_membre['telephone'] = str(membre_rh_data['telephone']) else: node_membre['telephone'] = "" if ("comment" in membre_rh_data.keys()): node_membre['comment'] = str(membre_rh_data['comment']) else: node_membre['comment'] = "" if ("role" in team_membre.keys()): node_membre['role'] = str(team_membre['role']) else: node_membre['role'] = "" if ("leader" in team_membre.keys()): node_membre['leader'] = str(team_membre['leader']) else: node_membre['leader'] = "" list_membre.append(node_membre) user['list_membre'] = list_membre val_tmp = val_tmp + 1 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'équipe " """ Recuperer la liste des équipes d'un partenaire avec des filtres sur : - code - description - membre (nom ou prenom ) equipe """ def Get_List_Equipe_Team_With_Filter(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'code', 'description', 'membre', ] 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', ] 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 filt_description = {} if ("description" in diction.keys()): filt_description = {'description': {'$regex': str(diction['description']), "$options": "i"}} filt_code = {} if ("code" in diction.keys()): filt_code = { 'code': {'$regex': str(diction['code']), "$options": "i"}} filt_membre_ressource_humaine_id = {} list_membre_ressource_humaine_id = [] if ("membre" in diction.keys()): filt_membre_qry = { '$or': [ { 'nom': {'$regex': str(diction['membre']), "$options": "i"}, 'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0' }, { 'prenom': {'$regex': str(diction['membre']), "$options": "i"}, 'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0' } ] } for tmp_val in MYSY_GV.dbname['ressource_humaine'].find(filt_membre_qry): list_membre_ressource_humaine_id.append(str(tmp_val['_id'])) print(" ### liste des Id des ressources humaine sont = ", list_membre_ressource_humaine_id) filt_membre_ressource_humaine_id = {'rh_id': {'$in': list_membre_ressource_humaine_id, }} #### RetObject = [] val_tmp = 0 find_qry = { '$and': [{'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'}, filt_description, filt_code, ]} local_new_myquery_find_team = "" if ("membre" in diction.keys()): local_new_myquery_find_team = [{'$match': find_qry}, {'$sort': {'_id': -1}}, {"$addFields": {"equipe_team_header_id": {"$toString": "$_id"}}}, {'$lookup': { 'from': 'equipe_team_membre', 'localField': "equipe_team_header_id", 'foreignField': 'equipe_team_id', 'pipeline': [ {'$match': {'$and': [ filt_membre_ressource_humaine_id, {'partner_owner_recid': str(my_partner['recid']),'valide': '1', 'locked':'0' }, ] } }, ], 'as': 'equipe_team_membre_collection' } }, { "$unwind": "$equipe_team_membre_collection" } ] else: local_new_myquery_find_team = [{'$match': find_qry}, {'$sort': {'_id': -1}}, {"$addFields": {"equipe_team_header_id": {"$toString": "$_id"}}}, {'$lookup': { 'from': 'equipe_team_membre', 'localField': "equipe_team_header_id", 'foreignField': 'equipe_team_id', 'pipeline': [ {'$match': {'$and': [ filt_membre_ressource_humaine_id, {'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'}, ] } }, ], 'as': 'equipe_team_membre_collection' } }, ] print(" ### local_new_myquery_find_team = ", local_new_myquery_find_team) ### for local_New_retVal in MYSY_GV.dbname['equipe_team'].aggregate(local_new_myquery_find_team): user = {} user['id'] = str(val_tmp) user['_id'] = str(local_New_retVal['_id']) if( "code" in local_New_retVal.keys()): user['code'] = local_New_retVal['code'] else: user['code'] = "" if ("description" in local_New_retVal.keys()): user['description'] = local_New_retVal['description'] else: user['description'] = "" if ("email_equipe" in local_New_retVal.keys()): user['email_equipe'] = local_New_retVal['email_equipe'] else: user['email_equipe'] = "" if ("chef_equipe_id" in local_New_retVal.keys()): user['chef_equipe_id'] = local_New_retVal['chef_equipe_id'] else: user['chef_equipe_id'] = "" nb_membre = "0" if( "equipe_team_membre_collection" in local_New_retVal.keys() ): nb_membre = len(local_New_retVal['equipe_team_membre_collection']) user['nb_membre'] = str(nb_membre) # Recuperation des nom et prenom du responsable (chef d'equipe) chef_equipe_nom_prenom = "" if ("chef_equipe_id" in local_New_retVal.keys() and local_New_retVal['chef_equipe_id']): chef_equipe_id_data = MYSY_GV.dbname['ressource_humaine'].find_one( {'partner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0', '_id': ObjectId(str(local_New_retVal['chef_equipe_id']))}) if (chef_equipe_id_data and 'nom' in chef_equipe_id_data.keys()): chef_equipe_nom_prenom = chef_equipe_id_data['nom'] if (chef_equipe_id_data and 'prenom' in chef_equipe_id_data.keys()): chef_equipe_nom_prenom = chef_equipe_nom_prenom + " " + chef_equipe_id_data['prenom'] user['chef_equipe_nom_prenom'] = str(chef_equipe_nom_prenom) val_tmp = val_tmp + 1 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 équipes "