544 lines
22 KiB
Python
544 lines
22 KiB
Python
"""
|
|
Ce fichier traite de tous les sujets concenant l'editique
|
|
C'est a dire :
|
|
- Quel document à imprimer
|
|
- Quand il a été imprimé ou téléchargé, et
|
|
"""
|
|
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 fonction retourne la liste de documents editable par une
|
|
- un partner
|
|
"""
|
|
def Get_Editable_Document_By_Partner(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 = 0
|
|
|
|
qry_match = {"partner_owner_recid":str(my_partner['recid']), 'valide':'1', 'locked':'0'}
|
|
|
|
pipe_qry = ([
|
|
{'$match': qry_match},
|
|
{'$sort': {'_id': -1}},
|
|
{'$lookup': {
|
|
'from': 'courrier_template',
|
|
"let": {'courrier_template_id': "$courrier_template_id", 'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$courrier_template_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
{'$project': {'contenu_doc': 0, 'corps_mail': 0}},
|
|
],
|
|
'as': 'collection_courrier_template'
|
|
}
|
|
},
|
|
{
|
|
'$unwind': '$collection_courrier_template'
|
|
}
|
|
])
|
|
|
|
print(" #### Get_Editable_Document_By_Partner pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)
|
|
|
|
for New_retVal in MYSY_GV.dbname['courrier_template_tracking'].aggregate(pipe_qry):
|
|
if( "collection_courrier_template" in New_retVal.keys() ):
|
|
user = New_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 documents éditables "
|
|
|
|
|
|
"""
|
|
Cette fonction retourne la liste de documents editable par une
|
|
- un partner
|
|
- une collection (ex : session_formation).
|
|
|
|
important : ici on recuperer juste la liste des document tracké, pas l'historique de traitement
|
|
"""
|
|
|
|
def Get_Editable_Document_By_Partner_By_Collection(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'related_collection', 'related_collection_recid']
|
|
|
|
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', 'related_collection']
|
|
|
|
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']
|
|
|
|
related_collection = ""
|
|
if ("related_collection" in diction.keys()):
|
|
if diction['related_collection']:
|
|
related_collection = diction['related_collection']
|
|
|
|
if( str(related_collection).strip() == ""):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Le nom de la collection est invalide ")
|
|
return False, " Le nom de la collection est invalide "
|
|
|
|
|
|
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 = 0
|
|
|
|
qry_match = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0', 'related_collection':str(related_collection)}
|
|
|
|
pipe_qry = ([
|
|
{'$match': qry_match},
|
|
{'$sort': {'rang': 1}},
|
|
{'$lookup': {
|
|
'from': 'courrier_template',
|
|
"let": {'courrier_template_id': "$courrier_template_id", 'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$courrier_template_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
{'$project': {'contenu_doc': 0, 'corps_mail': 0}},
|
|
],
|
|
'as': 'collection_courrier_template'
|
|
}
|
|
},
|
|
{
|
|
'$unwind': '$collection_courrier_template'
|
|
}
|
|
])
|
|
|
|
print(" #### Get_Editable_Document_By_Partner_By_Collection pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)
|
|
|
|
for New_retVal in MYSY_GV.dbname['courrier_template_tracking'].aggregate(pipe_qry):
|
|
if ("collection_courrier_template" in New_retVal.keys()):
|
|
user = {}
|
|
user['_id'] = str(New_retVal['_id'])
|
|
user['id'] = str(val_tmp)
|
|
|
|
user['courrier_template_id'] = ""
|
|
if('courrier_template_id' in New_retVal.keys()):
|
|
user['courrier_template_id'] = str(New_retVal['courrier_template_id'])
|
|
|
|
user['rang'] = "99999"
|
|
if ('rang' in New_retVal.keys()):
|
|
user['rang'] = str(New_retVal['rang'])
|
|
|
|
user['server_action_name'] = ""
|
|
if ('server_action_name' in New_retVal.keys()):
|
|
user['server_action_name'] = str(New_retVal['server_action_name'])
|
|
|
|
user['partner_owner_recid'] = ""
|
|
if ('partner_owner_recid' in New_retVal.keys()):
|
|
user['partner_owner_recid'] = str(New_retVal['partner_owner_recid'])
|
|
|
|
user['related_collection'] = ""
|
|
if ('related_collection' in New_retVal.keys()):
|
|
user['related_collection'] = str(New_retVal['related_collection'])
|
|
|
|
user['courrier_template_ref_interne'] = ""
|
|
if ('courrier_template_ref_interne' in New_retVal.keys()):
|
|
user['courrier_template_ref_interne'] = str(New_retVal['courrier_template_ref_interne'])
|
|
|
|
user['courrier_template_ref_interne'] = ""
|
|
if ('ref_interne' in New_retVal['collection_courrier_template'].keys()):
|
|
user['courrier_template_ref_interne'] = str(New_retVal['collection_courrier_template']['ref_interne'])
|
|
|
|
user['courrier_template_nom'] = ""
|
|
if ('nom' in New_retVal['collection_courrier_template'].keys()):
|
|
user['courrier_template_nom'] = str(
|
|
New_retVal['collection_courrier_template']['nom'])
|
|
|
|
user['courrier_template_sujet'] = ""
|
|
if ('sujet' in New_retVal['collection_courrier_template'].keys()):
|
|
user['courrier_template_sujet'] = str(
|
|
New_retVal['collection_courrier_template']['sujet'])
|
|
|
|
user['courrier_template_cible'] = ""
|
|
if ('cible' in New_retVal['collection_courrier_template'].keys()):
|
|
user['courrier_template_cible'] = str(
|
|
New_retVal['collection_courrier_template']['cible'])
|
|
|
|
user['courrier_template_type_doc'] = ""
|
|
if ('type_doc' in New_retVal['collection_courrier_template'].keys()):
|
|
user['courrier_template_type_doc'] = str(
|
|
New_retVal['collection_courrier_template']['type_doc'])
|
|
|
|
has_history_event = "0"
|
|
if( "related_collection_recid" in diction.keys() and diction['related_collection_recid']):
|
|
"""
|
|
Verifier si document a un historic pour le related_collection_recid.
|
|
Cela permet de savoir si pour l'entité qui appelle cette fonction, ce document a déjà été
|
|
traité
|
|
"""
|
|
local_qry = {'partner_owner_recid':str(my_partner['recid']),
|
|
'related_collection_recid':str(diction['related_collection_recid']),
|
|
'courrier_template_tracking_id':str(New_retVal['_id']),
|
|
'valide':'1',
|
|
'locked':'0'}
|
|
|
|
#print(" #### is_document_has_history_event qry = ", local_qry)
|
|
|
|
is_document_has_history_event = MYSY_GV.dbname['courrier_template_tracking_history'].count_documents(local_qry)
|
|
|
|
if( is_document_has_history_event > 0 ):
|
|
has_history_event = "1"
|
|
|
|
#print(" #### is_document_has_history_event RESULT = ", is_document_has_history_event)
|
|
|
|
user['has_history_event'] = has_history_event
|
|
|
|
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 documents éditables "
|
|
|
|
|
|
"""
|
|
Cette fonction retourne l'HISTORIQUE des telechargements et envois de mail
|
|
des documents tracés, pour :
|
|
- un partner (Obligatoire)
|
|
- un related_collection_recid (Obligatoire) (ex : l'_id de la session)
|
|
|
|
"""
|
|
|
|
def Get_Editable_History_Document_By_Partner_By_Collection(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'related_collection_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', 'related_collection_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']
|
|
|
|
related_collection_id = ""
|
|
if ("related_collection_id" in diction.keys()):
|
|
if diction['related_collection_id']:
|
|
related_collection_id = diction['related_collection_id']
|
|
|
|
if( str(related_collection_id).strip() == ""):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'Id de la collection est invalide ")
|
|
return False, " L'Id de la collection est invalide "
|
|
|
|
|
|
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 = 0
|
|
|
|
qry_match = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0', 'related_collection_recid':str(related_collection_id)}
|
|
|
|
pipe_qry = ([
|
|
{'$match': qry_match},
|
|
{'$sort': {'_id': -1}},
|
|
{'$lookup': {
|
|
'from': 'courrier_template_tracking',
|
|
"let": {'courrier_template_tracking_id': "$courrier_template_tracking_id",
|
|
'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$courrier_template_tracking_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'courrier_template_tracking'
|
|
}
|
|
},
|
|
|
|
{
|
|
'$unwind': '$courrier_template_tracking'
|
|
},
|
|
{'$lookup': {
|
|
'from': 'courrier_template',
|
|
"let": {'courrier_template_id': "$courrier_template_tracking.courrier_template_id", 'partner_owner_recid': '$partner_owner_recid'},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$courrier_template_id",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$partner_owner_recid", '$$partner_owner_recid']}
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
{'$project': {'contenu_doc': 0, 'corps_mail': 0}},
|
|
|
|
],
|
|
'as': 'courrier_template'
|
|
}
|
|
},
|
|
{
|
|
'$unwind': '$courrier_template'
|
|
},
|
|
])
|
|
|
|
print(" #### Get_Editable_Document_By_Partner pipe_qry_Get_Editable_Document_By_Partner = ", pipe_qry)
|
|
|
|
for New_retVal in MYSY_GV.dbname['courrier_template_tracking_history'].aggregate(pipe_qry):
|
|
if ("courrier_template_tracking" in New_retVal.keys() and "courrier_template" in New_retVal.keys()):
|
|
|
|
user = {}
|
|
user['_id'] = str(New_retVal['_id'])
|
|
user['id'] = str(val_tmp)
|
|
|
|
user['related_collection_recid'] = ""
|
|
if ('related_collection_recid' in New_retVal.keys()):
|
|
user['related_collection_recid'] = str(New_retVal['related_collection_recid'])
|
|
|
|
user['courrier_template_tracking_id'] = ""
|
|
if ('courrier_template_tracking_id' in New_retVal.keys()):
|
|
user['courrier_template_tracking_id'] = str(New_retVal['courrier_template_tracking_id'])
|
|
|
|
user['date_update'] = ""
|
|
if ('date_update' in New_retVal.keys()):
|
|
user['date_update'] = str(New_retVal['date_update'])
|
|
|
|
user['update_by'] = ""
|
|
if ('update_by' in New_retVal.keys()):
|
|
user['update_by'] = str(New_retVal['update_by'])
|
|
|
|
|
|
# Traitement de courrier_template_tracking
|
|
user['courrier_template_id'] = ""
|
|
if ('courrier_template_id' in New_retVal['courrier_template_tracking'].keys()):
|
|
user['courrier_template_id'] = str(New_retVal['courrier_template_tracking']['courrier_template_id'])
|
|
|
|
user['related_collection'] = ""
|
|
if ('related_collection' in New_retVal['courrier_template_tracking'].keys()):
|
|
user['related_collection'] = str(New_retVal['courrier_template_tracking']['related_collection'])
|
|
|
|
user['server_action_name'] = ""
|
|
if ('server_action_name' in New_retVal['courrier_template_tracking'].keys()):
|
|
user['server_action_name'] = str(New_retVal['courrier_template_tracking']['server_action_name'])
|
|
|
|
user['courrier_template_ref_interne'] = ""
|
|
if ('courrier_template_ref_interne' in New_retVal['courrier_template_tracking'].keys()):
|
|
user['courrier_template_ref_interne'] = str(New_retVal['courrier_template_tracking']['courrier_template_ref_interne'])
|
|
|
|
user['rang'] = ""
|
|
if ('rang' in New_retVal['courrier_template_tracking'].keys()):
|
|
user['rang'] = str(New_retVal['courrier_template_tracking']['rang'])
|
|
|
|
# Traitement de courrier_template
|
|
user['ref_interne'] = ""
|
|
if ('ref_interne' in New_retVal['courrier_template'].keys()):
|
|
user['ref_interne'] = str(New_retVal['courrier_template']['ref_interne'])
|
|
|
|
user['nom'] = ""
|
|
if ('nom' in New_retVal['courrier_template'].keys()):
|
|
user['nom'] = str(New_retVal['courrier_template']['nom'])
|
|
|
|
user['cible'] = ""
|
|
if ('cible' in New_retVal['courrier_template'].keys()):
|
|
user['cible'] = str(New_retVal['courrier_template']['cible'])
|
|
|
|
user['type_doc'] = ""
|
|
if ('type_doc' in New_retVal['courrier_template'].keys()):
|
|
user['cible'] = str(New_retVal['courrier_template']['type_doc'])
|
|
|
|
|
|
|
|
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 documents éditables "
|
|
|