""" Ce fichier permet de gerer les messages à afficher sur l'ENT une fois un apprenant connecté. Ce sont par exemples les messages d'alerte, d'info, etc. Un message est definit par : - code - priorité (Urgent (maximum), Important, Info) - message (1000 caractères max) - debut affichage - fin affichage """ 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 """ Ajout d'un nouveau message Un message est definit par : - code - priorité (Urgent (maximum), Important, Info) - message (1000 caractères max) - debut affichage - fin affichage """ def Add_Ent_Alert_Message(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'code', 'priorite', 'message', 'date_debut', 'date_fin'] 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', 'priorite', 'message', 'date_debut', 'date_fin'] 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_alert_message = MYSY_GV.dbname['ent_alert_message'].count_documents({'code':str(diction['code']), 'valide':'1', 'partner_owner_recid':str(my_partner['recid'])}) if( is_existe_alert_message > 0 ): mycommon.myprint( str(inspect.stack()[0][3]) + " Un message avec le code '" + str(diction['code']) + "' existe déjà ") return False, " Un message avec le code '" + str(diction['code']) + "' existe déjà " local_status = mycommon.CheckisDate(diction['date_debut']) if (local_status is False): mycommon.myprint( str(inspect.stack()[0][ 3]) + "La date debut n'est pas au format jj/mm/aaaa ") return False, " La date debut n'est pas au format jj/mm/aaaa " local_status = mycommon.CheckisDate(diction['date_fin']) if (local_status is False): mycommon.myprint( str(inspect.stack()[0][ 3]) + "La date fin n'est pas au format jj/mm/aaaa ") return False, " La date fin n'est pas au format jj/mm/aaaa " if( diction['priorite'] not in MYSY_GV.ENT_MESSAGE_PRIORITE ): mycommon.myprint( str(inspect.stack()[0][ 3]) + " Le niveau de priorité est invalide ") return False, " Le niveau de priorité est invalide " new_data = diction del diction['token'] new_data['valide'] = "1" new_data['locked'] = "0" new_data['publish_ent'] = "0" new_data['creation_date'] = 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['ent_alert_message'].insert_one(new_data).inserted_id if (not inserted_id): mycommon.myprint( " Impossible de créer le message d'alerte (2) ") return False, " Impossible de créer le message d'alerte (2) " return True, " Le message d'alerte 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 le message d'alerte " """ Mise à jour d'un message d'alerte ENT """ def Update_Ent_Alert_Message(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', 'code', 'priorite', 'message', 'date_debut', 'date_fin', '_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 la domaine is_existe_cdtion_paiement = MYSY_GV.dbname['ent_alert_message'].count_documents( {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'partner_owner_recid': str(my_partner['recid'])}) if (is_existe_cdtion_paiement < 0): mycommon.myprint( str(inspect.stack()[0][3]) + " L'identifiant du message est invalide ") return False, " L'identifiant du message est invalide " # Verifier que code n'est pas pris par un autre domaine is_existe_class_domaine = MYSY_GV.dbname['ent_alert_message'].count_documents({'code': str(diction['code']), 'valide': '1', 'partner_owner_recid': str( my_partner['recid']), "_id": {"$ne": ObjectId( str(diction['_id']))} }) if (is_existe_class_domaine > 0): mycommon.myprint( str(inspect.stack()[0][3]) + " Un message avec le code '" + str(diction['code']) + "' existe déjà ") return False, " Un message avec le code '" + str(diction['code']) + "' existe déjà " if( "date_debut" in diction.keys() ): local_status = mycommon.CheckisDate(diction['date_debut']) if (local_status is False): mycommon.myprint( str(inspect.stack()[0][ 3]) + "La date debut n'est pas au format jj/mm/aaaa ") return False, " La date debut n'est pas au format jj/mm/aaaa " if ("date_fin" in diction.keys()): local_status = mycommon.CheckisDate(diction['date_fin']) if (local_status is False): mycommon.myprint( str(inspect.stack()[0][ 3]) + "La date fin n'est pas au format jj/mm/aaaa ") return False, " La date fin n'est pas au format jj/mm/aaaa " if ("priorite" in diction.keys()): if (diction['priorite'] not in MYSY_GV.ENT_MESSAGE_PRIORITE): mycommon.myprint( str(inspect.stack()[0][ 3]) + " Le niveau de priorité est invalide ") return False, " Le niveau de priorité est invalide " local_id = str(diction['_id']) 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['ent_alert_message'].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 le message (2) ") return False, " Impossible de mettre à jour le message (2) " return True, " Le message a été correctement mis à 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 le message " """ Recuperer la liste des message d'alerte ENT d'un partenaire """ def Get_List_Ent_Alert_Message(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 print(" ## my_partner = ",my_partner) """ Clés de mise à jour """ data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['partner_owner_recid']) data_cle['valide'] = "1" data_cle['locked'] = "0" RetObject = [] val_tmp = 0 for retval in MYSY_GV.dbname['ent_alert_message'].find(data_cle).sort([ ("priorite", pymongo.ASCENDING), ("_id", pymongo.DESCENDING), ]): user = retval user['id'] = str(val_tmp) 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 des messages " """ Recuperer la liste des message d'alerte PUBLIE sur ENT d'un partenaire """ def Get_List_Published_Ent_Alert_Message(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 print(" ## my_partner = ",my_partner) """ Clés de mise à jour """ data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['partner_owner_recid']) data_cle['valide'] = "1" data_cle['locked'] = "0" data_cle['publish_ent'] = "1" RetObject = [] val_tmp = 0 for retval in MYSY_GV.dbname['ent_alert_message'].find(data_cle).sort([ ("priorite", pymongo.ASCENDING), ("_id", pymongo.DESCENDING), ]): user = retval user['id'] = str(val_tmp) 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 des messages " """ Cette fonction permet de supprimer une liste de message """ def Delete_Ent_Alert_Message(diction): try: diction = mycommon.strip_dictionary(diction) """ Verification des input acceptés """ field_list = ['token', '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', '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 tab_message_ids = "" if ("tab_ids" in diction.keys()): if diction['tab_ids']: tab_message_ids = diction['tab_ids'] tab_message_Object_ids = [] tab_my_message_ids = str(tab_message_ids).split(",") for message_id in tab_my_message_ids: if( message_id ): tab_message_Object_ids.append(ObjectId(str(message_id))) """ Suppression des données dans la collection """ delete_row = MYSY_GV.dbname['ent_alert_message'].delete_many( {'_id': {'$in': tab_message_Object_ids}, 'partner_owner_recid': my_partner['recid']}) return True, " (" + str(delete_row.deleted_count) + " message(s) supprimé(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 de supprimer les messages " """ Recuperer les données d'un message """ def Get_Given_Ent_Alert_Message(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 print(" ## my_partner = ",my_partner) """ Clés de mise à jour """ data_cle = {} data_cle['partner_owner_recid'] = str(my_partner['partner_owner_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['ent_alert_message'].find(data_cle): user = retval user['id'] = str(val_tmp) 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 du message " """ Cette fonction permet publier le message vers l'ENT """ def Publish_to_ENT_Alert_Message(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 local_id = str(diction['_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['ent_alert_message'].find_one_and_update( data_cle, {"$set": {'publish_ent':'1'}}, upsert=False, return_document=ReturnDocument.AFTER ) if ("_id" not in result.keys()): mycommon.myprint( " Impossible de publier le message vers l'ENT (2) ") return False, " Impossible de publier le message vers l'ENT (2) " return True, " Le message a été correctement publié vers l'ENT" 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 publier le message vers l'ENT" """ Cette fonction permet dépublier le message de l'ENT """ def Unpublish_to_ENT_Alert_Message(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 local_id = str(diction['_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['ent_alert_message'].find_one_and_update( data_cle, {"$set": {'publish_ent': '0'}}, upsert=False, return_document=ReturnDocument.AFTER ) if ("_id" not in result.keys()): mycommon.myprint( " Impossible de dépublier le message vers l'ENT (2) ") return False, " Impossible de dépublier le message vers l'ENT (2) " return True, " Le message a été correctement dépublier de l'ENT" 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 dépublier le message vers l'ENT"