1498 lines
58 KiB
Python
1498 lines
58 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
|
|
from math import isnan
|
|
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','duration', 'duration_unit', 'token', 'plus_produit', 'mots_cle','domaine', 'internal_url', 'zone_diffusion']
|
|
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
|
|
|
|
class_internal_url = ""
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
class_internal_url = diction['internal_url']
|
|
|
|
'''
|
|
Si l'internal_url reste à vide, alors le système va créer une internal url de la formation
|
|
'''
|
|
if( len(class_internal_url) <= 0 ):
|
|
status, class_internal_url = mycommon.CreateInternalUrl(diction['title'])
|
|
|
|
mydata['internal_url'] = class_internal_url
|
|
|
|
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']
|
|
|
|
my_presentiel = "0"
|
|
my_distantiel = "0"
|
|
|
|
if ("distantiel" in diction.keys()):
|
|
if diction['distantiel']:
|
|
my_distantiel = diction['distantiel']
|
|
|
|
if ("presentiel" in diction.keys()):
|
|
if diction['presentiel']:
|
|
my_presentiel = diction['presentiel']
|
|
|
|
mydata['presentiel'] = {'presentiel': my_presentiel, 'distantiel': my_distantiel}
|
|
|
|
if ("price" in diction.keys()):
|
|
if diction['price']:
|
|
mydata['price'] = mycommon.tryFloat(str(diction['price']))
|
|
|
|
if ("url" in diction.keys()):
|
|
if diction['url']:
|
|
mydata['url'] = diction['url']
|
|
|
|
if ("duration" in diction.keys()):
|
|
if diction['duration']:
|
|
mydata['duration'] = float(str(diction['duration']))
|
|
|
|
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']
|
|
|
|
|
|
if ("duration_unit" in diction.keys()):
|
|
if diction['duration_unit']:
|
|
mydata['duration_unit'] = diction['duration_unit']
|
|
|
|
|
|
if ("zone_diffusion" in diction.keys()):
|
|
if diction['zone_diffusion']:
|
|
tmp_str2 = str(diction['zone_diffusion']).lower().replace(",", ";")
|
|
|
|
if (not tmp_str2.endswith(";")):
|
|
tmp_str2 = tmp_str2 + ";"
|
|
|
|
print("tmp_str2 = " + tmp_str2)
|
|
|
|
|
|
if (";" not in tmp_str2):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation : A" + str(
|
|
diction['external_code']) + " est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(diction['external_code']) + " est incorrecte"
|
|
|
|
|
|
tmp_str = tmp_str2.split(";") #==> ce qui va donner un tableau de : Country_Code-City : ['fr-paris', 'fr-marseille', 'bn-cotonou']
|
|
cpt = 0
|
|
tab_country = []
|
|
tab_city = []
|
|
for val in tmp_str:
|
|
print(" val = "+str(val))
|
|
if( len(str(val)) <= 0 ):
|
|
continue
|
|
|
|
if( "-" not in str(val)):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation : B" + str( diction['external_code']) + " est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(diction['external_code']) + " est incorrecte"
|
|
|
|
|
|
tab_val = len(val.split("-"))
|
|
if( len(val.split("-")) != 1 and len(val.split("-")) != 2 ):
|
|
mycommon.myprint( str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation : C" + str(diction['external_code'])+" est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(diction['external_code'])+" est incorrecte"
|
|
|
|
if( val.split("-")[0]):
|
|
tab_country.append(val.split("-")[0])
|
|
else:
|
|
tab_country.append("")
|
|
|
|
if(val.split("-")[1]):
|
|
tab_city.append(val.split("-")[1])
|
|
else:
|
|
tab_city.append("")
|
|
|
|
|
|
mydata['zone_diffusion'] = {'country':tab_country, 'city':tab_city}
|
|
|
|
|
|
|
|
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 = MYSY_GV.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 la formation ")
|
|
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)
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'external_code': str(mydata['external_code']), },
|
|
{"$set": mydata},
|
|
upsert=True,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ret_val and ret_val['_id']:
|
|
return True, "La formation a bien été ajoutée"
|
|
else:
|
|
mycommon.myprint(" Impossible d'ajouter la formation "+str(str(mydata['external_code'])))
|
|
return False, " Impossible d'ajouter la formation "+str(str(mydata['external_code']))
|
|
|
|
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', 'duration', 'token','plus_produit', 'mots_cle',
|
|
'domaine', 'internal_code', 'internal_url','zone_diffusion']
|
|
|
|
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 = ['internal_url', '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()):
|
|
my_internal_code = diction['internal_code']
|
|
|
|
my_internal_url = ""
|
|
if ("internal_url" in diction.keys()):
|
|
my_internal_url = diction['internal_url']
|
|
|
|
|
|
if ("external_code" in diction.keys()):
|
|
my_external_code = diction['external_code']
|
|
|
|
if ("title" in diction.keys()):
|
|
mydata['title'] = diction['title']
|
|
|
|
if ("description" in diction.keys()):
|
|
mydata['description'] = diction['description']
|
|
|
|
if ("trainer" in diction.keys()):
|
|
mydata['trainer'] = diction['trainer']
|
|
|
|
if ("institut_formation" in diction.keys()):
|
|
mydata['institut_formation'] = diction['institut_formation']
|
|
|
|
my_presentiel = "0"
|
|
my_distantiel = "0"
|
|
|
|
if ("distantiel" in diction.keys()):
|
|
my_distantiel = diction['distantiel']
|
|
|
|
if ("presentiel" in diction.keys()):
|
|
my_presentiel = diction['presentiel']
|
|
|
|
mydata['presentiel'] = {'presentiel': my_presentiel, 'distantiel': my_distantiel}
|
|
|
|
if ("price" in diction.keys()):
|
|
if( diction['price']):
|
|
mydata['price'] = mycommon.tryFloat(str(diction['price']))
|
|
|
|
if ("url" in diction.keys()):
|
|
mydata['url'] = diction['url']
|
|
|
|
if ("duration" in diction.keys()):
|
|
mydata['duration'] = float(str(diction['duration']))
|
|
|
|
|
|
if ("plus_produit" in diction.keys()):
|
|
mydata['plus_produit'] = diction['plus_produit']
|
|
|
|
|
|
if ("mots_cle" in diction.keys()):
|
|
mydata['mots_cle'] = diction['mots_cle']
|
|
|
|
if ("domaine" in diction.keys()):
|
|
if diction['domaine']:
|
|
mydata['domaine'] = diction['domaine']
|
|
|
|
if ("zone_diffusion" in diction.keys()):
|
|
tmp_str2 = str(diction['zone_diffusion']).lower().replace(",", ";")
|
|
|
|
|
|
|
|
if( not tmp_str2.endswith(";")):
|
|
tmp_str2 = tmp_str2+";"
|
|
|
|
print("tmp_str2 = " + tmp_str2)
|
|
|
|
if (";" not in tmp_str2):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation 0: " + str(
|
|
my_external_code) + " est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(my_external_code) + " est incorrecte"
|
|
|
|
|
|
tmp_str = tmp_str2.split(";") #==> ce qui va donner un tableau de : Country_Code-City : ['fr-paris', 'fr-marseille', 'bn-cotonou']
|
|
cpt = 0
|
|
tab_country = []
|
|
tab_city = []
|
|
for val in tmp_str:
|
|
print(" val = "+str(val))
|
|
|
|
print(" val = " + str(val))
|
|
if (len(str(val)) <= 0):
|
|
continue
|
|
|
|
|
|
if( "-" not in str(val)):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation 1: " + str( my_external_code) + " est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(my_external_code) + " est incorrecte"
|
|
|
|
|
|
tab_val = len(val.split("-"))
|
|
if( len(val.split("-")) != 1 and len(val.split("-")) != 2 ):
|
|
mycommon.myprint( str(inspect.stack()[0][3]) + " - la zone de diffusion de la formation 2: " + str(my_external_code)+" est incorrecte")
|
|
return False, "La zone de diffusion de la formation " + str(my_external_code)+" est incorrecte"
|
|
|
|
if( val.split("-")[0]):
|
|
tab_country.append(val.split("-")[0])
|
|
else:
|
|
tab_country.append("")
|
|
|
|
if(val.split("-")[1]):
|
|
tab_city.append(val.split("-")[1])
|
|
else:
|
|
tab_city.append("")
|
|
|
|
|
|
mydata['zone_diffusion'] = {'country':tab_country, 'city':tab_city}
|
|
|
|
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['indexed'] = '0'
|
|
mydata['indexed_title'] = '0'
|
|
mydata['indexed_desc'] = '0'
|
|
mydata['indexed_obj'] = '0'
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update({'internal_url': str(my_internal_url), '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:
|
|
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 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 = ['internal_url', '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 = ['internal_url', '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_internal_url = ""
|
|
|
|
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 ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['valide'] = '0'
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
|
|
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'internal_url': str(my_internal_url), '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_internal_url)+"a été desactivée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de desactivier : " +str(my_internal_url) )
|
|
return False, " Impossible de desactivier la formation : "+str(my_internal_url)
|
|
|
|
|
|
|
|
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 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 = ['internal_url', '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 = ['internal_url', '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_internal_url = ""
|
|
|
|
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 ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['valide'] = '1'
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
|
|
# seules les formation avec locked = 0 et valide=1 sont modifiables
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'internal_url': str(my_internal_url), '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_internal_url) + "a été reactivée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de desactivier : " + str(my_internal_url))
|
|
return False, " Impossible de reactivée la formation : " + str(my_internal_url)
|
|
|
|
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 = ['internal_url', '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 = ['internal_url', '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_internal_url = ""
|
|
|
|
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 ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['locked'] = '0'
|
|
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
|
|
# seules les formation avec locked = 1 et valide=1 sont 'unlockable'
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'internal_url': str(my_internal_url), '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_internal_url) + "a été debloquée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de debloquer : " + str(my_internal_url))
|
|
return False, " Impossible de debloquer la formation : " + str(my_internal_url)
|
|
|
|
|
|
|
|
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 = ['internal_url', '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 = ['internal_url', '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_internal_url = ""
|
|
|
|
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 ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_url = diction['internal_url']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['locked'] = '1'
|
|
|
|
coll_name = MYSY_GV.dbname['myclass']
|
|
|
|
# seules les formation avec locked = 1 et valide=1 sont 'unlockable'
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'internal_url': str(my_internal_url), '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_internal_url) + "a été verrouillée"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de verrouiller la formation : " + str(my_internal_url))
|
|
return False, " Impossible de verrouiller la formation : " + str(my_internal_url)
|
|
|
|
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 = MYSY_GV.dbname['myclass']
|
|
|
|
# verifier que le token et l'email sont ok
|
|
coll_token = MYSY_GV.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 = MYSY_GV.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 laa ="+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 = MYSY_GV.dbname['myclass']
|
|
|
|
|
|
RetObject = []
|
|
filt_external_code = {}
|
|
internal_url = ""
|
|
if ("internal_url" in diction.keys()):
|
|
internal_url = str(diction['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 = MYSY_GV.dbname['myclass']
|
|
|
|
# verifier que le token et l'email sont ok
|
|
coll_token = MYSY_GV.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 ("internal_url" in diction.keys()):
|
|
filt_external_code = {'internal_url':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( {"$and":[ {'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)
|
|
|
|
'''
|
|
Pour des facilité d'affichage coté front
|
|
on va reformater le champ "zone_diffusion" de sorte à le renvoyer
|
|
sous la forme "code_pays-ville"
|
|
'''
|
|
i = 0
|
|
tmp_zone_diffusion = ""
|
|
if( "zone_diffusion" in user.keys()):
|
|
if( user['zone_diffusion'] and user['zone_diffusion']["city"]):
|
|
for tmp_val in user['zone_diffusion']["city"]:
|
|
tmp_zone_diffusion = tmp_zone_diffusion + str(user['zone_diffusion']["country"][i])+"-"+str(user['zone_diffusion']["city"][i])+";"
|
|
i = i+1
|
|
|
|
user['zone_diffusion_str'] = str(tmp_zone_diffusion[:-1])
|
|
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 = MYSY_GV.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 = MYSY_GV.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','duration', 'plus_produit', 'mots_cle', 'zone_diffusion']
|
|
|
|
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['title'].values[n])
|
|
mydata['domaine'] = 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['duration'] = float(str(df['duration'].values[n]))
|
|
mydata['plus_produit'] = str(df['plus_produit'].values[n])
|
|
mydata['mots_cle'] = str(df['mots_cle'].values[n])
|
|
mydata['presentiel'] = str(df['presentiel'].values[n])
|
|
mydata['distantiel'] = str(df['distantiel'].values[n])
|
|
mydata['price'] = str(df['price'].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"
|
|
|
|
|
|
'''
|
|
Traitement de la zone de diffusion
|
|
'''
|
|
if ("zone_diffusion" in df.keys()):
|
|
if(str(df['zone_diffusion'].values[n])):
|
|
mydata['zone_diffusion'] =str(df['zone_diffusion'].values[n])
|
|
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mydata['token'] = diction['token']
|
|
|
|
mydata['duration_unit'] = "jour"
|
|
|
|
clean_dict = {k: mydata[k] for k in mydata if ( str(mydata[k]) != "nan") }
|
|
|
|
print( clean_dict)
|
|
status, retval = add_class(clean_dict)
|
|
|
|
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 "
|