1339 lines
52 KiB
Python
1339 lines
52 KiB
Python
'''
|
|
Ce fichier traite tout ce qui est liée à la gestion des formations
|
|
|
|
'''
|
|
|
|
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
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
def default(self, o):
|
|
if isinstance(o, ObjectId):
|
|
return str(o)
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
CONNECTION_STRING = "mongodb://localhost/cherifdb"
|
|
client = MongoClient(CONNECTION_STRING)
|
|
dbname = client['cherifdb']
|
|
|
|
|
|
MAX_KEYWORD = 3
|
|
|
|
'''
|
|
Cette fonction ajoute une formation
|
|
elle verifie le token de l'entité qui ajoute la formation.
|
|
'''
|
|
|
|
def add_class(diction):
|
|
try:
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'title', 'description', 'trainer', 'institut_formation', 'distantiel', 'presentiel',
|
|
'price', 'url','duree_formation','token', 'plus_produit', 'mots_cle','domaine']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - Creation formation : Le champ '" + val + "' n'est pas autorisé, Creation formation annulée")
|
|
return False, " Verifier votre API"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', 'title', 'description', 'trainer', 'distantiel', 'presentiel', '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, " Verifier votre API"
|
|
|
|
|
|
'''
|
|
Verification si le token et l'email sont valident
|
|
'''
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour créer une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT LES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid du partenaire
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid du partenaire")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
mydata['partner_owner_recid'] = user_recid
|
|
|
|
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
mydata['external_code'] = diction['external_code']
|
|
|
|
if ("title" in diction.keys()):
|
|
if diction['title']:
|
|
mydata['title'] = diction['title']
|
|
|
|
if ("description" in diction.keys()):
|
|
if diction['description']:
|
|
mydata['description'] = diction['description']
|
|
|
|
if ("trainer" in diction.keys()):
|
|
if diction['trainer']:
|
|
mydata['trainer'] = diction['trainer']
|
|
|
|
if ("plus_produit" in diction.keys()):
|
|
if diction['plus_produit']:
|
|
mydata['plus_produit'] = diction['plus_produit']
|
|
|
|
if ("institut_formation" in diction.keys()):
|
|
if diction['institut_formation']:
|
|
mydata['institut_formation'] = diction['institut_formation']
|
|
|
|
if ("distantiel" in diction.keys()):
|
|
if diction['distantiel']:
|
|
mydata['distantiel'] = diction['distantiel']
|
|
|
|
if ("presentiel" in diction.keys()):
|
|
if diction['presentiel']:
|
|
mydata['presentiel'] = diction['presentiel']
|
|
|
|
if ("price" in diction.keys()):
|
|
if diction['price']:
|
|
mydata['price'] = int(str(diction['price']))
|
|
|
|
if ("url" in diction.keys()):
|
|
if diction['url']:
|
|
mydata['url'] = diction['url']
|
|
|
|
if ("duree_formation" in diction.keys()):
|
|
if diction['duree_formation']:
|
|
mydata['duree_formation'] = float(str(diction['duree_formation']))
|
|
|
|
|
|
|
|
if ("plus_produit" in diction.keys()):
|
|
if diction['plus_produit']:
|
|
mydata['plus_produit'] = diction['plus_produit']
|
|
|
|
|
|
if ("mots_cle" in diction.keys()):
|
|
if diction['mots_cle']:
|
|
mydata['mots_cle'] = diction['mots_cle']
|
|
'''
|
|
Verification du nombre de mots clée : limite MAX_KEYWORD (3)
|
|
'''
|
|
nb_keyword = mydata['mots_cle'].split(";")
|
|
if( len(nb_keyword) > MAX_KEYWORD ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " : La formation "+str(mydata['external_code'])+" a plus de "+ str(MAX_KEYWORD)+" mots clés")
|
|
return False, " La formation "+str(mydata['external_code'])+" a plus de "+ str(MAX_KEYWORD)+" mots clés"
|
|
|
|
|
|
|
|
if ("domaine" in diction.keys()):
|
|
if diction['domaine']:
|
|
mydata['domaine'] = diction['domaine']
|
|
|
|
|
|
mydata['valide'] = '1'
|
|
mydata['locked'] = '0'
|
|
mydata['indexed'] = '0'
|
|
mydata['indexed_title'] = '0'
|
|
mydata['indexed_desc'] = '0'
|
|
mydata['indexed_obj'] = '0'
|
|
|
|
# Create internal ref. of class
|
|
mydata['internal_code'] = mycommon.Create_internal_call_ref()
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
'''
|
|
Verification si cette formation exite deja .
|
|
la clé est : external_code
|
|
'''
|
|
#tmp = coll_name.find({'external_code': str(mydata['external_code'])}).count()
|
|
tmp = coll_name.count_documents({'external_code': str(mydata['external_code'])})
|
|
|
|
#mycommon.myprint(" TMP = " + str(tmp))
|
|
if (tmp > 0):
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" -la formation avec l'external code " + str(mydata['external_code']) + "' existe deja, impossible de créer le compte partenaire ")
|
|
return False, "la formation avec l'external code " + str(mydata['external_code']) + "' existe deja. Impossible de créer la formation "
|
|
|
|
|
|
coll_name.insert_one(mydata)
|
|
|
|
return True, "La formation a bien été ajoutée"
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, "Impossible d'ajouter la formation"
|
|
|
|
|
|
'''
|
|
cette fontion met à jour une formation
|
|
la clé est : l'external code.
|
|
|
|
seules les formation "valide" et non "locked" sont modifiable
|
|
'''
|
|
def update_class(diction):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'title', 'description', 'trainer', 'institut_formation', 'distantiel',
|
|
'presentiel','price', 'url', 'duree_formation', 'token','plus_produit', 'mots_cle',
|
|
'domaine', 'internal_code']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3])+" - Creation partner account : Le champ '" + val + "' n'est pas accepté, Creation formation annulée")
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', '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, " Impossible de mettre à jour la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour modifier une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION / MODIFICATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT CELLES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token ne sont pas valident")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid de l'utilisateur")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
partner_recid = user_recid
|
|
my_internal_code = ""
|
|
|
|
if ("internal_code" in diction.keys()):
|
|
if diction['internal_code']:
|
|
my_internal_code = diction['internal_code']
|
|
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
my_external_code = diction['external_code']
|
|
|
|
if ("title" in diction.keys()):
|
|
if diction['title']:
|
|
mydata['title'] = diction['title']
|
|
|
|
if ("description" in diction.keys()):
|
|
if diction['description']:
|
|
mydata['description'] = diction['description']
|
|
|
|
if ("trainer" in diction.keys()):
|
|
if diction['trainer']:
|
|
mydata['trainer'] = diction['trainer']
|
|
|
|
if ("plus_produit" in diction.keys()):
|
|
if diction['plus_produit']:
|
|
mydata['plus_produit'] = diction['plus_produit']
|
|
|
|
if ("institut_formation" in diction.keys()):
|
|
if diction['institut_formation']:
|
|
mydata['institut_formation'] = diction['institut_formation']
|
|
|
|
if ("distantiel" in diction.keys()):
|
|
if diction['distantiel']:
|
|
mydata['distantiel'] = diction['distantiel']
|
|
|
|
if ("presentiel" in diction.keys()):
|
|
if diction['presentiel']:
|
|
mydata['presentiel'] = diction['presentiel']
|
|
|
|
if ("price" in diction.keys()):
|
|
if diction['price']:
|
|
mydata['price'] = int(str(diction['price']))
|
|
|
|
if ("url" in diction.keys()):
|
|
if diction['url']:
|
|
mydata['url'] = diction['url']
|
|
|
|
if ("duree_formation" in diction.keys()):
|
|
if diction['duree_formation']:
|
|
mydata['duree_formation'] = float(str(diction['duree_formation']))
|
|
|
|
|
|
if ("plus_produit" in diction.keys()):
|
|
if diction['plus_produit']:
|
|
mydata['plus_produit'] = diction['plus_produit']
|
|
|
|
|
|
if ("mots_cle" in diction.keys()):
|
|
if diction['mots_cle']:
|
|
mydata['mots_cle'] = diction['mots_cle']
|
|
|
|
if ("domaine" in diction.keys()):
|
|
if diction['domaine']:
|
|
mydata['domaine'] = diction['domaine']
|
|
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['indexed'] = '0'
|
|
mydata['indexed_title'] = '0'
|
|
mydata['indexed_desc'] = '0'
|
|
mydata['indexed_obj'] = '0'
|
|
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update({'external_code': str(my_external_code), 'partner_owner_recid':partner_recid, 'locked': '0', 'valide': '1'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
nb_doc = str(ret_val['_id'])
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La formation a bin ete mise à jour =" + str(nb_doc))
|
|
return True, " La formation a été mise à jour"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la formation : "+str(my_external_code) )
|
|
return False, "Impossible de mettre à jour la formation "+str(my_external_code)
|
|
|
|
|
|
except Exception as e:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - " +str(e))
|
|
return False, " Impossible de mettre à jour la formation "
|
|
|
|
|
|
|
|
'''
|
|
Desactivation d'une formation
|
|
|
|
Disable a training, set mydata['valide'] = '0'
|
|
'''
|
|
def disable_class(diction):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'token']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3])+" - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', '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, " Impossible de mettre à jour la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour modifier une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION / MODIFICATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT CELLES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token ne sont pas valident")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid de l'utilisateur")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
partner_recid = user_recid
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
my_external_code = diction['external_code']
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['valide'] = '0'
|
|
coll_name = dbname['myclass']
|
|
|
|
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'external_code': str(my_external_code), 'partner_owner_recid': partner_recid, 'locked': '0',
|
|
'valide': '1'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
nb_doc = str(ret_val['_id'])
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La formation a bin ete mise à jour =" + str(nb_doc))
|
|
return True, " La formation "+str(my_external_code)+"a été desactivée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de desactivier : " +str(my_external_code) )
|
|
return False, " Impossible de desactivier la formation : "+str(my_external_code)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - " +str(e))
|
|
return False, " Impossible de desactivier la formation "
|
|
|
|
|
|
'''
|
|
reactivation d'une formation
|
|
|
|
Enable a training set mydata['valide'] = '1'
|
|
'''
|
|
def enable_class(diction):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'token']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3])+" - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', '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, " Impossible de mettre à jour la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour modifier une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION / MODIFICATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT CELLES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token ne sont pas valident")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid de l'utilisateur")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
partner_recid = user_recid
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
my_external_code = diction['external_code']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['valide'] = '1'
|
|
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'external_code': str(my_external_code), 'partner_owner_recid': partner_recid, 'locked': '0',
|
|
'valide': '0'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
nb_doc = str(ret_val['_id'])
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La formation a bien ete reactivée =" + str(nb_doc))
|
|
return True, " La formation " + str(my_external_code) + "a été reactivée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de desactivier : " + str(my_external_code))
|
|
return False, " Impossible de reactivée la formation : " + str(my_external_code)
|
|
|
|
except Exception as e:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - " +str(e))
|
|
return False, " Impossible de reactiver la formation"
|
|
|
|
|
|
|
|
'''
|
|
Desactivation d'une formation
|
|
|
|
unlock a training set mydata['locked'] = '0'
|
|
'''
|
|
def unlock_class(diction):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'token']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3])+" - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', '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, " Impossible de mettre à jour la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour modifier une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION / MODIFICATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT CELLES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token ne sont pas valident")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid de l'utilisateur")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
partner_recid = user_recid
|
|
|
|
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
my_external_code = diction['external_code']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['locked'] = '0'
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
# seules les formation avec locked = 1 et valide=1 sont 'unlockable'
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'external_code': str(my_external_code), 'partner_owner_recid': partner_recid, 'locked': '1',
|
|
'valide': '1'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
nb_doc = str(ret_val['_id'])
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La formation a bien ete debloquée =" + str(nb_doc))
|
|
return True, " La formation " + str(my_external_code) + "a été debloquée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de debloquer : " + str(my_external_code))
|
|
return False, " Impossible de debloquer la formation : " + str(my_external_code)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - " +str(e))
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
|
|
'''
|
|
Verrouillage d'une formation
|
|
|
|
lock a training set mydata['locked'] = '1'
|
|
'''
|
|
|
|
|
|
def lock_class(diction):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['external_code', 'token']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['external_code', '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, " Impossible de mettre à jour la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Important : pour modifier une formation, il faut obligatoirement avoir un token.
|
|
PAS DE CREATION / MODIFICATION DE FORMATION EN MODE NON CONNECTE.
|
|
|
|
CONCERNANT CELLES CREEES PAR NOS SYSTEME AUTOMATIQUE, IL FAUDRA LEUR PASSER UNE VALEUR MALGRE TOUT
|
|
|
|
'''
|
|
retval = mycommon.check_partner_token_validity("", str(mydata['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token ne sont pas valident")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(str(mydata['token']))
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid de l'utilisateur")
|
|
return False, " Les informations d'identification sont incorrectes"
|
|
|
|
partner_recid = user_recid
|
|
|
|
if ("external_code" in diction.keys()):
|
|
if diction['external_code']:
|
|
my_external_code = diction['external_code']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['locked'] = '1'
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
# seules les formation avec locked = 1 et valide=1 sont 'unlockable'
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'external_code': str(my_external_code), 'partner_owner_recid': partner_recid, 'locked': '0',
|
|
'valide': '1'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
nb_doc = str(ret_val['_id'])
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La formation a bien ete verrouillée =" + str(nb_doc))
|
|
return True, " La formation " + str(my_external_code) + "a été verrouillée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de verrouiller la formation : " + str(my_external_code))
|
|
return False, " Impossible de verrouiller la formation : " + str(my_external_code)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - " + str(e))
|
|
return False, " Impossible de mettre à jour la formation"
|
|
|
|
|
|
'''
|
|
cette fonction recherche et retour une formation.
|
|
la clé est : l'external code.
|
|
- le token du partenaire
|
|
|
|
Seules les formation "locked = 0 et valide = 1" sont recuperables par l'API
|
|
'''
|
|
def get_class(diction):
|
|
try:
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['internal_url', 'token', 'title', 'valide', 'locked', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
|
|
'user_postal', 'user_latitude', 'user_longitude', 'user_state']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint( str(inspect.stack()[0][3])+ " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
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, " Impossible de recuperer la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_internal_url = ""
|
|
my_token = ""
|
|
|
|
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
|
|
'''
|
|
Gestion des filters.
|
|
'''
|
|
|
|
internal_url_crit = {}
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
internal_url_crit['internal_url'] = diction['internal_url']
|
|
|
|
title_crit = {}
|
|
if ("title" in diction.keys()):
|
|
if diction['title']:
|
|
title_crit['title'] = diction['title']
|
|
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
# verifier que le token et l'email sont ok
|
|
coll_token = dbname['user_token']
|
|
|
|
# Verification de la validité du token dans le cas des user en mode connecté
|
|
'''
|
|
/!\ Important : si le token est vide, alors c'est une recherche faite en mode non-connecté.
|
|
on doit l'accepter.
|
|
|
|
le controle de la validé du token est faite que ce dernier n'est pas vide.
|
|
'''
|
|
|
|
user_recid = "None"
|
|
coll_search_result = dbname['user_recherche_result']
|
|
|
|
# Verification de la validité du token/mail dans le cas des user en mode connecté
|
|
if (len(str(my_token)) > 0):
|
|
retval = mycommon.check_token_validity("", my_token)
|
|
|
|
if retval is False:
|
|
mycommon.myprint( str(inspect.stack()[0][3])+" - Le token n'est pas valide")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_user_recid_from_token(my_token)
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - Impossible de recuperer le token de l'utilisateur")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
RetObject = []
|
|
filt_external_code = {}
|
|
internal_url = ""
|
|
if ("internal_url" in diction.keys()):
|
|
filt_external_code = {'internal_url':str(diction['internal_url'])}
|
|
internal_url = str(diction['internal_url'])
|
|
|
|
#print(' ICICICIC '+str(filt_external_code))
|
|
|
|
filt_title = {}
|
|
if ("title" in diction.keys()):
|
|
filt_title = {'title': {'$regex': str(diction['title'])}}
|
|
|
|
|
|
print(" #### avant requete get partner_owner_recid ="+str(user_recid)+
|
|
" internal_url = "+str(my_internal_url)+
|
|
" filt_title = "+str(filt_title))
|
|
|
|
for retVal in coll_name.find({'valide':'1','locked':'0','internal_url':internal_url},
|
|
{"_id": 0, "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, "partner_owner_recid": 0, }
|
|
):
|
|
|
|
my_recid = {}
|
|
my_recid['user_rec_id'] = str(user_recid)
|
|
mydict_combined = {**diction, **retVal, **my_recid}
|
|
mydict_combined['date_update'] = str(datetime.now())
|
|
mydict_combined['type_view'] = "detail"
|
|
|
|
if ("_id" in mydict_combined.keys()):
|
|
mydict_combined['class_id'] = mydict_combined.pop('_id')
|
|
|
|
#mycommon.myprint("COMBINED = " + str(mydict_combined))
|
|
|
|
'''
|
|
Statistique : Insertion du recherche - resultat '''
|
|
ret_val_tmp = coll_search_result.insert_one(mydict_combined)
|
|
|
|
if (ret_val_tmp is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
|
|
" d'une recherche de formation. Voir _id = ")
|
|
return False, "Impossible de faire un affichage detaillé "
|
|
|
|
if ("description" in retVal.keys()):
|
|
tmp_str = retVal['description']
|
|
if (len(retVal['description']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['description'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
if ("objectif" in retVal.keys()):
|
|
tmp_str = retVal['objectif']
|
|
if (len(retVal['objectif']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['objectif'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
|
|
if ("programme" in retVal.keys()):
|
|
tmp_str = retVal['programme']
|
|
if (len(retVal['programme']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['programme'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
if ("pedagogie" in retVal.keys()):
|
|
tmp_str = retVal['pedagogie']
|
|
if (len(retVal['pedagogie']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['pedagogie'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
#mycommon.myprint(str(retVal))
|
|
user = retVal
|
|
RetObject.append(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 recuperer la formation"
|
|
|
|
|
|
'''
|
|
Cette API retour une formation "coup de coeur".
|
|
elle effectue les controles necessaires et retour la formation
|
|
'''
|
|
def get_class_coup_de_coeur(diction):
|
|
try:
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['internal_url', 'token', 'title', 'valide', 'locked', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
|
|
'user_postal', 'user_latitude', 'user_longitude', 'user_state']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint( str(inspect.stack()[0][3])+ " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
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, " Impossible de recuperer la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_internal_url = ""
|
|
my_token = ""
|
|
|
|
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
|
|
RetObject = []
|
|
filt_external_code = {}
|
|
internal_url = ""
|
|
if ("internal_url" in diction.keys()):
|
|
internal_url = str(diction['internal_url'])
|
|
|
|
print(" laaaa internal_url = "+internal_url)
|
|
for retVal in coll_name.find({'valide':'1','locked':'0','internal_url':internal_url, 'coeur':'1'},
|
|
{"_id": 0, "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, "partner_owner_recid": 0, }
|
|
):
|
|
|
|
print(" retval "+str(retVal))
|
|
|
|
if ("description" in retVal.keys()):
|
|
tmp_str = retVal['description']
|
|
if (len(retVal['description']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['description'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
if ("objectif" in retVal.keys()):
|
|
tmp_str = retVal['objectif']
|
|
if (len(retVal['objectif']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['objectif'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
|
|
if ("programme" in retVal.keys()):
|
|
tmp_str = retVal['programme']
|
|
if (len(retVal['programme']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['programme'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
if ("pedagogie" in retVal.keys()):
|
|
tmp_str = retVal['pedagogie']
|
|
if (len(retVal['pedagogie']) > MYSY_GV.MAX_CARACT_DETAIL):
|
|
retVal['pedagogie'] = tmp_str[:MYSY_GV.MAX_CARACT_DETAIL] + " ..."
|
|
|
|
|
|
mycommon.myprint(str(retVal))
|
|
user = retVal
|
|
RetObject.append(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 recuperer la formation"
|
|
|
|
|
|
|
|
|
|
'''
|
|
cette fonction recherche et retour une formation dont le proprietaire est le partenaire "partenaire_rec_id"
|
|
la clé est : l'external code.
|
|
- le token du partenaire. le token permet d'aller chercher le "rec_id"
|
|
|
|
Seules les formation "locked = 0 et valide = 1" sont recuperables par l'API
|
|
'''
|
|
def get_partner_class(diction):
|
|
try:
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['internal_url', 'token', 'title', 'valide', 'locked']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint( str(inspect.stack()[0][3])+ " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
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, " Impossible de recuperer la formation"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
my_external_code = ""
|
|
my_token = ""
|
|
|
|
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_code = diction['internal_url']
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
'''
|
|
Gestion des filters.
|
|
'''
|
|
|
|
external_code_crit = {}
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
external_code_crit['internal_url'] = diction['internal_url']
|
|
|
|
title_crit = {}
|
|
if ("title" in diction.keys()):
|
|
if diction['title']:
|
|
title_crit['title'] = diction['title']
|
|
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
# verifier que le token et l'email sont ok
|
|
coll_token = dbname['user_token']
|
|
|
|
# Verification de la validité du token dans le cas des user en mode connecté
|
|
'''
|
|
/!\ Important : le token ne doit jamais etre vide car cette fonction a pour objectif
|
|
de retourner les formation edité par un partenaire.
|
|
|
|
Il dont obligatoirement est en mode connecté
|
|
'''
|
|
|
|
user_recid = "None"
|
|
# Verification de la validité du token/mail dans le cas des user en mode connecté
|
|
if (len(str(my_token)) > 0):
|
|
retval = mycommon.check_partner_token_validity("", my_token)
|
|
|
|
if retval is False:
|
|
mycommon.myprint( str(inspect.stack()[0][3])+" - Le token n'est pas valide")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_parnter_recid_from_token(my_token)
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - Impossible de recuperer le token de l'utilisateur")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
if (len(str(my_token)) <= 0):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide")
|
|
return False, " Impossible de recuperer la formation"
|
|
|
|
|
|
RetObject = []
|
|
filt_external_code = {}
|
|
if ("external_code" in diction.keys()):
|
|
filt_external_code = {'internal_url':{'$regex':str(diction['internal_url'])}}
|
|
|
|
filt_title = {}
|
|
if ("title" in diction.keys()):
|
|
filt_title = {'title': {'$regex': str(diction['title'])}}
|
|
|
|
|
|
print(" ATTTTENNTION : GESTION DU CAS OU LA PERSONNE QUI CHERCHE LE COURS EST UN UTILISATEUR : PB avec : partner_owner_recid ")
|
|
print(" #### avant requete get partner_owner_recid ="+str(user_recid)+
|
|
" filt_external_code = "+str(filt_external_code)+
|
|
" filt_title = "+str(filt_title))
|
|
|
|
val_tmp = 1
|
|
for retVal in coll_name.find( {'valide':'1'},{'locked':'0'},
|
|
{'partner_owner_recid':user_recid} ,
|
|
filt_external_code,
|
|
filt_title):
|
|
mycommon.myprint(str(retVal))
|
|
user = retVal
|
|
user['id'] = str(val_tmp)
|
|
RetObject.append(JSONEncoder().encode(user))
|
|
val_tmp = val_tmp + 1
|
|
|
|
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 recuperer la formation"
|
|
|
|
|
|
|
|
def get_class_global_search(search_string):
|
|
try:
|
|
mycommon.myprint(" search_string", search_string)
|
|
client = MongoClient(CONNECTION_STRING)
|
|
|
|
dbname = client['cherifdb']
|
|
coll_name = dbname['myclass']
|
|
|
|
val = re.compile(r".*"+search_string+".*")
|
|
my_regex = "/.*"+search_string+".*/"
|
|
|
|
insertObject = []
|
|
for x in coll_name.find({'myindex': { '$regex': re.compile(r".*"+search_string+".*") }}):
|
|
mycommon.myprint(x)
|
|
user = x
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
|
|
#mycommon.myprint(" insertObject = ", insertObject)
|
|
return insertObject
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False
|
|
|
|
|
|
|
|
def get_all_class_by_attribut(attribut, value):
|
|
try:
|
|
mycommon.myprint(" attribut", attribut, " value = ",value)
|
|
|
|
coll_name = dbname['myclass']
|
|
|
|
insertObject = []
|
|
for x in coll_name.find({attribut: value}, {"_id": 0}):
|
|
mycommon.myprint(x)
|
|
user = x
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
|
|
#mycommon.myprint(" insertObject = ", insertObject)
|
|
return insertObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False
|
|
|
|
|
|
|
|
|
|
'''
|
|
Cette fontion import un fichier excel de formation
|
|
'''
|
|
|
|
def add_class_mass(file=None, Folder=None, diction=None):
|
|
try:
|
|
|
|
'''
|
|
# Verification que les champs reçus dans l'API sont bien dans la liste des champs autorisés
|
|
# Cela evite le cas ou une entité tierce ajouter les valeurs inconnu dans l'API
|
|
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
|
|
# field_list.
|
|
'''
|
|
field_list = ['token']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Creation formation : Le champ '" + val + "' n'existe pas, Creation formation annulée")
|
|
return False, " Verifier votre API"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
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, " Verifier votre API"
|
|
|
|
|
|
status, saved_file = mycommon.Upload_Save_CSV_File(file, Folder)
|
|
if (status == False):
|
|
return False, "Impossible d'inserer les formation en masse "
|
|
|
|
#" Lecture du fichier "
|
|
print(" Lecture du fichier : "+saved_file)
|
|
nb_line = 0
|
|
|
|
df = pd.read_csv(saved_file, encoding='utf8', on_bad_lines='skip', sep=';')
|
|
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# Verification que les noms des colonne sont bien corrects"
|
|
'''
|
|
field_list = ['external_code', 'title', 'description', 'trainer', 'institut_formation', 'distantiel', 'presentiel',
|
|
'price', 'domaine', 'url','duree_formation', 'plus_produit', 'mots_cle']
|
|
|
|
total_rows = len(df)
|
|
|
|
print(df.columns)
|
|
for val in df.columns:
|
|
if str(val).lower() not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3])+" : entete du fichier csv. '" + val + "' n'est pas acceptée")
|
|
return False, " Entete du fichier csv. '" + val + "' n'est pas acceptée"
|
|
|
|
|
|
x = range(0, total_rows)
|
|
|
|
|
|
for n in x:
|
|
mydata = {}
|
|
mydata['external_code'] = str(df['external_code'].values[n])
|
|
mydata['title'] = str(df['domaine'].values[n])
|
|
mydata['description'] = str(df['description'].values[n])
|
|
mydata['trainer'] = str(df['trainer'].values[n])
|
|
mydata['institut_formation'] = str(df['institut_formation'].values[n])
|
|
mydata['distantiel'] = str(df['presentiel'].values[n])
|
|
mydata['url'] = str(df['url'].values[n])
|
|
mydata['duree_formation'] = float(str(df['duree_formation'].values[n]))
|
|
mydata['plus_produit'] = str(df['plus_produit'].values[n])
|
|
mydata['mots_cle'] = str(df['mots_cle'].values[n])
|
|
'''
|
|
Verification du nombre de mots clée : limite MAX_KEYWORD (3)
|
|
'''
|
|
nb_keyword = mydata['mots_cle'].split(";")
|
|
if( len(nb_keyword) > MAX_KEYWORD ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " : La formation "+str(mydata['external_code'])+" a plus de "+ str(MAX_KEYWORD)+" mots clés")
|
|
return False, " La formation "+str(mydata['external_code'])+" a plus de "+ str(MAX_KEYWORD)+" mots clés"
|
|
|
|
|
|
mydata['distantiel'] = str(df['distantiel'].values[n])
|
|
mydata['presentiel'] = str(df['presentiel'].values[n])
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
print( mydata)
|
|
status, retval = add_class(mydata)
|
|
|
|
if( status is False ):
|
|
return status, retval
|
|
|
|
print(str(total_rows)+" formations ont été inserées")
|
|
|
|
return True, str(total_rows)+" formations ont été inserées"
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, "Impossible d'inserer les formation en masse "
|