885 lines
33 KiB
Python
885 lines
33 KiB
Python
"""
|
|
Ce fichier permet de gerer les formulaire d'un utilisateur :
|
|
|
|
par exemple la mis en place d'un formulaire de satisfaction.
|
|
|
|
|
|
Un formulaire est stocké comme suit :
|
|
La clé d'un formulaire est le 'code' en minuscule
|
|
Formaulaire :
|
|
=> Nom from
|
|
=> List_Questions
|
|
|
|
|
=> phrase Question
|
|
=> Type question (texte, boolean, note, Qcm, entier)
|
|
=> Liste_Choix (si type = QCM)
|
|
=> Max_note (si type = note)
|
|
"""
|
|
import ast
|
|
|
|
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
|
|
|
|
def Add_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'code', 'nom', 'description', 'commentaire', 'message_introduction', 'type' ]
|
|
|
|
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', 'nom', 'description', 'type' ]
|
|
|
|
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
|
|
|
|
mydata = {}
|
|
|
|
mydata = diction
|
|
del mydata['token']
|
|
|
|
# Initialisation des champs non envoyés à vide
|
|
for val in field_list:
|
|
if val not in diction.keys():
|
|
mydata[str(val)] = ""
|
|
|
|
|
|
if( str(diction['type']) not in MYSY_GV.FORM_TYPE ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Le type de formulaire n'est pas valide. Les valeurs autorisée sont "+str(MYSY_GV.FORM_TYPE))
|
|
return False, " Le type de formulaire n'est pas valide. Les valeurs autorisée sont "+str(MYSY_GV.FORM_TYPE)
|
|
|
|
|
|
"""
|
|
Verifier que le formulaire n'existe pas (code en minuscule)
|
|
"""
|
|
is_form_exist = MYSY_GV.dbname['formulaire'].count_documents({'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1', 'code':str(diction['code'])})
|
|
|
|
if(is_form_exist > 0 ):
|
|
mycommon.myprint(" Un formulaire avec le même code existe déjà ")
|
|
return False, " Un formulaire avec le même code existe déjà "
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['update_by'] = str(my_partner['_id'])
|
|
mydata['partner_owner_recid'] = str(my_partner['recid'])
|
|
mydata['valide'] = "1"
|
|
mydata['locked'] = "0"
|
|
|
|
inserted_id = MYSY_GV.dbname['formulaire'].insert_one(mydata).inserted_id
|
|
|
|
if (not inserted_id):
|
|
mycommon.myprint(" Impossible de créer le formulaire (2) ")
|
|
return False, " Impossible de créer le formulaire (2) "
|
|
|
|
return True, " Le formulaire a été correctement ajouté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 le formulaire "
|
|
|
|
|
|
"""
|
|
Fonction ajoute ou modifie une question à un formulaire
|
|
"""
|
|
|
|
def Add_Update_Question_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'form_id', 'question_id', 'question', 'type', 'commentaire', 'list_choix', 'max_note', 'list_qcm', 'list_case_cocher' ]
|
|
|
|
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', 'form_id', 'question', 'type' ]
|
|
|
|
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 le formulaire existe et est valide
|
|
"""
|
|
is_form_exist = MYSY_GV.dbname['formulaire'].count_documents({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked':'0',
|
|
'_id': ObjectId(str(diction['form_id']))})
|
|
|
|
if (is_form_exist <= 0):
|
|
mycommon.myprint(" L'identifiant du formulaire est invalide ")
|
|
return False, " L'identifiant du formulaire est invalide "
|
|
|
|
|
|
"""
|
|
Verifier que le type de question est valide
|
|
"""
|
|
|
|
if( str(diction['type']).strip().lower() not in MYSY_GV.FORM_QUESTION_TYPE):
|
|
mycommon.myprint(" Le type des question est invalide. Les valeurs autorisées "+ MYSY_GV.FORM_QUESTION_TYPE)
|
|
return False, " Le type des question est invalide. Les valeurs autorisées "+ MYSY_GV.FORM_QUESTION_TYPE
|
|
|
|
|
|
|
|
"""
|
|
Verifier que la note maximale est un entier
|
|
"""
|
|
if( str(diction['type']) == "note"):
|
|
if( "max_note" not in diction.keys() ):
|
|
mycommon.myprint(" Pour ce type de question, une note maximale doit être fournie ")
|
|
return False, "Pour ce type de question, une note maximale doit être fournie "
|
|
|
|
local_status, local_note_max = mycommon.IsInt(str(diction['max_note']).strip())
|
|
if(local_status is False ):
|
|
mycommon.myprint(" Pour ce type de question, une note maximale doit être un nombre entier ")
|
|
return False, " Pour ce type de question, une note maximale doit être un nombre entier "
|
|
|
|
|
|
|
|
# si 'question_id', verifier qu'il est valide
|
|
if( "question_id" in diction.keys() ):
|
|
is_question_exist = MYSY_GV.dbname['formulaire'].count_documents(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id'])),
|
|
'list_questions._id':str(diction['question_id'])})
|
|
|
|
if (is_question_exist <= 0):
|
|
mycommon.myprint(" L'identifiant de la question est invalide ")
|
|
return False, " L'identifiant de la question est invalide "
|
|
|
|
# On doit faire un update
|
|
|
|
new_data = {}
|
|
new_data['date_update'] = str(datetime.now())
|
|
new_data['valide'] = "1"
|
|
new_data['update_by'] = str(my_partner['_id'])
|
|
new_data['locked'] = "0"
|
|
new_data['question'] = str(diction['question'])
|
|
new_data['type'] = str(diction['type'])
|
|
new_data['_id'] = str(diction['question_id'])
|
|
|
|
if ("commentaire" in diction.keys()):
|
|
new_data['commentaire'] = str(diction['commentaire'])
|
|
else:
|
|
new_data['commentaire'] = ""
|
|
|
|
if ("max_note" in diction.keys()):
|
|
new_data['max_note'] = str(diction['max_note'])
|
|
else:
|
|
new_data['max_note'] = ""
|
|
|
|
|
|
tab_list_choix = []
|
|
if ("list_choix" in diction.keys() and str(diction['type']) == "qcm"):
|
|
|
|
print(" diction[list_choix] = ", diction['list_choix'])
|
|
local_JSON = ast.literal_eval(diction['list_choix'])
|
|
for local_val in local_JSON :
|
|
|
|
print(" local_val ", local_val)
|
|
reponse_qcm = {}
|
|
if( "reponse" in local_val):
|
|
reponse_qcm['reponse_qcm'] = local_val['reponse']
|
|
else:
|
|
reponse_qcm['reponse_qcm'] = ""
|
|
|
|
tab_list_choix.append(reponse_qcm)
|
|
|
|
|
|
if (len(tab_list_choix) > 0 and str(diction['type']) == "qcm"):
|
|
new_data['list_choix'] = tab_list_choix
|
|
|
|
|
|
tab_list_case_cocher = []
|
|
if ("list_case_cocher" in diction.keys() and str(diction['type']) == "checkbox"):
|
|
|
|
print(" diction[list_case_cocher] = ", diction['list_case_cocher'])
|
|
local_JSON = ast.literal_eval(diction['list_case_cocher'])
|
|
for local_val in local_JSON :
|
|
|
|
print(" local_val ", local_val)
|
|
reponse_checkbox = {}
|
|
if( "reponse" in local_val):
|
|
reponse_checkbox['reponse_checkbox'] = local_val['reponse']
|
|
else:
|
|
reponse_checkbox['reponse_checkbox'] = ""
|
|
|
|
|
|
tab_list_case_cocher.append(reponse_checkbox)
|
|
|
|
if(len(tab_list_case_cocher) > 0 and str(diction['type']) == "checkbox" ):
|
|
new_data['list_case_cocher'] = tab_list_case_cocher
|
|
|
|
|
|
update = MYSY_GV.dbname['formulaire'].update_one({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))},
|
|
|
|
{
|
|
'$set': {
|
|
'list_questions.$[xxx]': new_data,
|
|
|
|
}
|
|
},
|
|
upsert=False,
|
|
array_filters=[
|
|
{"xxx._id": str(diction['question_id'])}
|
|
]
|
|
|
|
)
|
|
|
|
if (update.modified_count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Impossible de mettre à jour la question (3) ")
|
|
return False, " Impossible de mettre à jour la question (3) "
|
|
|
|
return True, " La question a été correctement mise à jour"
|
|
|
|
else:
|
|
# Il s'agit de la creation d'une nouvelle question
|
|
# field_list = ['token', 'form_id', 'question_id', 'question', 'type', 'commentaire', 'list_choix', 'max_note' ]
|
|
|
|
new_data = {}
|
|
new_data['date_update'] = str(datetime.now())
|
|
new_data['valide'] = "1"
|
|
new_data['update_by'] = str(my_partner['_id'])
|
|
new_data['locked'] = "0"
|
|
new_data['question'] = str(diction['question'])
|
|
new_data['type'] = str(diction['type'])
|
|
new_question_id = secrets.token_hex(5)
|
|
new_data['_id'] = new_question_id
|
|
|
|
if("commentaire" in diction.keys() ):
|
|
new_data['commentaire'] = str(diction['commentaire'])
|
|
else:
|
|
new_data['commentaire'] = ""
|
|
|
|
if ("max_note" in diction.keys()):
|
|
new_data['max_note'] = str(diction['max_note'])
|
|
else:
|
|
new_data['max_note'] = ""
|
|
|
|
|
|
tab_list_choix = []
|
|
if ("list_choix" in diction.keys() and str(diction['type']) == "qcm"):
|
|
|
|
print(" diction[list_choix] = ", diction['list_choix'])
|
|
local_JSON = ast.literal_eval(diction['list_choix'])
|
|
for local_val in local_JSON :
|
|
|
|
print(" local_val ", local_val)
|
|
reponse_qcm = {}
|
|
if( "reponse" in local_val):
|
|
reponse_qcm['reponse_qcm'] = local_val['reponse']
|
|
else:
|
|
reponse_qcm['reponse_qcm'] = ""
|
|
|
|
tab_list_choix.append(reponse_qcm)
|
|
|
|
|
|
if (len(tab_list_choix) > 0 and str(diction['type']) == "qcm"):
|
|
new_data['list_choix'] = tab_list_choix
|
|
|
|
|
|
tab_list_case_cocher = []
|
|
if ("list_case_cocher" in diction.keys() and str(diction['type']) == "checkbox"):
|
|
|
|
print(" diction[list_case_cocher] = ", diction['list_case_cocher'])
|
|
local_JSON = ast.literal_eval(diction['list_case_cocher'])
|
|
for local_val in local_JSON :
|
|
|
|
print(" local_val ", local_val)
|
|
reponse_checkbox = {}
|
|
if( "reponse" in local_val):
|
|
reponse_checkbox['reponse_checkbox'] = local_val['reponse']
|
|
else:
|
|
reponse_checkbox['reponse_checkbox'] = ""
|
|
|
|
|
|
tab_list_case_cocher.append(reponse_checkbox)
|
|
|
|
if(len(tab_list_case_cocher) > 0 and str(diction['type']) == "checkbox" ):
|
|
new_data['list_case_cocher'] = tab_list_case_cocher
|
|
|
|
update = MYSY_GV.dbname['formulaire'].update_one({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))},
|
|
|
|
{
|
|
'$push': {
|
|
"list_questions": { '$each': [new_data] },
|
|
|
|
}
|
|
},
|
|
|
|
)
|
|
|
|
print(update)
|
|
if (update.modified_count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Impossible d'ajouter la question (2) ")
|
|
return False, " Impossible d'ajouter la question (2) "
|
|
|
|
|
|
return True, " La question a été correctement ajoutée au formulaire"
|
|
|
|
|
|
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'ajouter ou mettre à jour la question "
|
|
|
|
|
|
"""
|
|
Fonction pour supprimer une question d'un formulaire
|
|
"""
|
|
|
|
def Delete_Question_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'form_id', 'question_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', 'form_id', 'question_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 le formulaire existe et est valide
|
|
"""
|
|
is_form_exist = MYSY_GV.dbname['formulaire'].count_documents({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))})
|
|
|
|
if (is_form_exist <= 0):
|
|
mycommon.myprint(" L'identifiant du formulaire est invalide ")
|
|
return False, " L'identifiant du formulaire est invalide "
|
|
|
|
|
|
# si 'question_id', verifier qu'il est valide
|
|
if ("question_id" in diction.keys()):
|
|
is_question_exist = MYSY_GV.dbname['formulaire'].count_documents(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id'])),
|
|
'list_questions._id': str(diction['question_id'])})
|
|
|
|
if (is_question_exist <= 0):
|
|
mycommon.myprint(" L'identifiant de la question est invalide ")
|
|
return False, " L'identifiant de la question est invalide "
|
|
|
|
|
|
update = MYSY_GV.dbname['formulaire'].update_one({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))},
|
|
|
|
{'$pull': {"list_questions": {"_id": str(diction['question_id'])}}}
|
|
|
|
)
|
|
|
|
if (update.modified_count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Impossible de supprimer la question (2) ")
|
|
return False, " Impossible de supprimer la question (2) "
|
|
|
|
return True, " La question 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 la question "
|
|
|
|
|
|
"""
|
|
Fonction pour supprimer un formulaire
|
|
"""
|
|
def Delete_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'form_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', 'form_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 le formulaire existe et est valide
|
|
"""
|
|
is_form_exist = MYSY_GV.dbname['formulaire'].count_documents({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))})
|
|
|
|
if (is_form_exist <= 0):
|
|
mycommon.myprint(" L'identifiant du formulaire est invalide ")
|
|
return False, " L'identifiant du formulaire est invalide "
|
|
|
|
qry = {'partner_owner_recid': str(my_partner['recid']),'valide': '1', 'locked': '0', '_id': ObjectId(str(diction['form_id']))}
|
|
|
|
delete_formulaire = MYSY_GV.dbname['formulaire'].delete_many(qry)
|
|
|
|
return True, " Le formulaire a été correctement supprimé"
|
|
|
|
|
|
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 le questionnaire "
|
|
|
|
|
|
"""
|
|
Recuperer la listes des formulaire d'un partenaire, sans filter
|
|
"""
|
|
def Get_List_Formulaire(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 = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0'}
|
|
for New_retVal in MYSY_GV.dbname['formulaire'].find(qry).sort([("_id", pymongo.DESCENDING), ]):
|
|
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 formulaires "
|
|
|
|
|
|
|
|
"""
|
|
Recuperer la listes des formulaire d'un partenaire, AVEC filter
|
|
"""
|
|
def Get_List_Formulaire_with_filter(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'code', 'nom', 'description', 'type' ]
|
|
|
|
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
|
|
|
|
filt_type = {}
|
|
if ("type" in diction.keys()):
|
|
filt_type = {'type': {'$regex': str(diction['type']), "$options": "i"}}
|
|
|
|
filt_code = {}
|
|
if ("code" in diction.keys()):
|
|
filt_code = {'code': {'$regex': str(diction['code']), "$options": "i"}}
|
|
|
|
filt_nom = {}
|
|
if ("nom" in diction.keys()):
|
|
filt_nom = {'nom': {'$regex': str(diction['nom']), "$options": "i"}}
|
|
|
|
filt_description = {}
|
|
if ("description" in diction.keys()):
|
|
filt_description = {'description': {'$regex': str(diction['description']), "$options": "i"}}
|
|
|
|
|
|
RetObject = []
|
|
val_tmp = 0
|
|
|
|
qry = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0'}
|
|
query_with_filter = {'$and': [qry, filt_code, filt_nom, filt_description, filt_type]}
|
|
|
|
for New_retVal in MYSY_GV.dbname['formulaire'].find(query_with_filter).sort([("_id", pymongo.DESCENDING), ]):
|
|
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 formulaires "
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
Fonction pour recuperer les données d'un formulaire données
|
|
"""
|
|
def Get_Given_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'form_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', 'form_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
|
|
|
|
RetObject = []
|
|
val_tmp = 0
|
|
|
|
qry = {"partner_owner_recid": str(my_partner['recid']), 'valide': '1', 'locked': '0', '_id':ObjectId(str(diction['form_id']))}
|
|
for New_retVal in MYSY_GV.dbname['formulaire'].find(qry).sort([("_id", pymongo.DESCENDING), ]):
|
|
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 formulaires "
|
|
|
|
|
|
"""
|
|
Fonction pour mettre à jour les données
|
|
d'un formulaire
|
|
"""
|
|
|
|
def Update_Formulaire(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'code', 'nom', 'description', 'commentaire', 'form_id', 'message_introduction', 'type']
|
|
|
|
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', 'nom', 'description', 'form_id', 'type' ]
|
|
|
|
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 le formulaire existe et est valide
|
|
"""
|
|
is_form_exist = MYSY_GV.dbname['formulaire'].count_documents({'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0',
|
|
'_id': ObjectId(str(diction['form_id']))})
|
|
|
|
if (is_form_exist <= 0):
|
|
mycommon.myprint(" L'identifiant du formulaire est invalide ")
|
|
return False, " L'identifiant du formulaire est invalide "
|
|
|
|
mydata = {}
|
|
|
|
mydata = diction
|
|
del mydata['token']
|
|
|
|
if (str(diction['type']) not in MYSY_GV.FORM_TYPE):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " Le type de formulaire n'est pas valide. Les valeurs autorisée sont " + str(
|
|
MYSY_GV.FORM_TYPE))
|
|
return False, " Le type de formulaire n'est pas valide. Les valeurs autorisée sont " + str(
|
|
MYSY_GV.FORM_TYPE)
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['update_by'] = str(my_partner['_id'])
|
|
|
|
data_cle = {'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0', '_id': ObjectId(str(diction['form_id']))}
|
|
|
|
inserted_id = ""
|
|
result = MYSY_GV.dbname['formulaire'].find_one_and_update(
|
|
data_cle,
|
|
{"$set": mydata},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
if (result is None or "_id" not in result.keys()):
|
|
mycommon.myprint(
|
|
" Impossible de mettre à jour le formulaire (2) ")
|
|
return False, " Impossible de mettre à jour le formulaire (2) "
|
|
|
|
|
|
return True, " Le formulaire 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 formulaire "
|
|
|