734 lines
28 KiB
Python
734 lines
28 KiB
Python
'''
|
|
|
|
Ce fichier definit la gestion des partners - client.
|
|
exemple :
|
|
- creation et modification des compte client
|
|
- connexion pour l'obtention d'un token
|
|
- creation d'une api d'ajout/mise d'une formation
|
|
- creation d'une api de desactivation d'une formation
|
|
|
|
'''
|
|
|
|
from pymongo import MongoClient
|
|
import pymongo
|
|
import json
|
|
from flask import Flask, request, jsonify
|
|
#from flask_mongoengine import MongoEngine
|
|
import json
|
|
from bson import ObjectId
|
|
import re
|
|
import numpy as np
|
|
import ela_index_bdd_classes as ela_index
|
|
import email_mgt as mail
|
|
from datetime import datetime
|
|
import logging
|
|
import prj_common as mycommon
|
|
import secrets
|
|
import inspect
|
|
import sys, os
|
|
from pymongo import ReturnDocument
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
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']
|
|
|
|
'''
|
|
Ajout d'un partenaire
|
|
'''
|
|
def add_partner_account(diction):
|
|
try:
|
|
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# 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 = ['nom', 'adr_street', 'adr_city', 'adr_zip', 'adr_country','link_linkedin','link_facebook','link_twitter',
|
|
'email','pwd', 'telephone','contact_nom','contact_prenom','contact_tel','contact_mail']
|
|
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 partenaire annulée")
|
|
return False, "Impossible de créer le partenaire"
|
|
|
|
|
|
'''
|
|
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 = ['nom', 'email','pwd', 'telephone','contact_nom','contact_prenom','contact_tel','contact_mail']
|
|
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 créer le partenaire"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
mydata['nom'] = diction['nom']
|
|
|
|
if ("adr_street" in diction.keys()):
|
|
if diction['adr_street']:
|
|
mydata['adr_street'] = diction['adr_street']
|
|
|
|
if ("adr_city" in diction.keys()):
|
|
if diction['adr_city']:
|
|
mydata['adr_city'] = diction['adr_city']
|
|
|
|
if ("adr_zip" in diction.keys()):
|
|
if diction['adr_zip']:
|
|
mydata['adr_zip'] = diction['adr_zip']
|
|
|
|
if ("adr_country" in diction.keys()):
|
|
if diction['adr_country']:
|
|
mydata['adr_country'] = diction['adr_country']
|
|
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
mydata['email'] = diction['email']
|
|
|
|
if ("telephone" in diction.keys()):
|
|
if diction['telephone']:
|
|
mydata['telephone'] = diction['telephone']
|
|
|
|
if ("pwd" in diction.keys()):
|
|
if diction['pwd']:
|
|
mydata['pwd'] = diction['pwd']
|
|
|
|
if ("contact_nom" in diction.keys()):
|
|
if diction['contact_nom']:
|
|
mydata['contact_nom'] = diction['contact_nom']
|
|
|
|
if ("contact_prenom" in diction.keys()):
|
|
if diction['contact_prenom']:
|
|
mydata['contact_prenom'] = diction['contact_prenom']
|
|
|
|
if ("contact_tel" in diction.keys()):
|
|
if diction['contact_tel']:
|
|
mydata['contact_tel'] = diction['contact_tel']
|
|
|
|
if ("contact_mail" in diction.keys()):
|
|
if diction['contact_mail']:
|
|
mydata['contact_mail'] = diction['contact_mail']
|
|
|
|
if ("link_linkedin" in diction.keys()):
|
|
if diction['link_linkedin']:
|
|
mydata['link_linkedin'] = diction['link_linkedin']
|
|
|
|
if ("link_facebook" in diction.keys()):
|
|
if diction['link_facebook']:
|
|
mydata['link_facebook'] = diction['link_facebook']
|
|
|
|
if ("link_twitter" in diction.keys()):
|
|
if diction['link_twitter']:
|
|
mydata['link_twitter'] = diction['link_twitter']
|
|
|
|
# Creation du RecId du user
|
|
mydata['recid'] = mycommon.create_user_recid()
|
|
|
|
mydata['active'] = '0'
|
|
mydata['locked'] = '0'
|
|
|
|
print(str(datetime.now()) + " webservice : diction = "+str(mydata))
|
|
|
|
|
|
coll_name = dbname['partnair_account']
|
|
|
|
|
|
# Verification que cette adresse email n'existe pas
|
|
#tmp = coll_name.find({'email': str(mydata['email']) }).count()
|
|
tmp = coll_name.count_documents({'email': str(mydata['email']) })
|
|
|
|
logging.info(" TMP = "+str(tmp))
|
|
if( tmp > 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - l'adresse email '"+str(mydata['email'])+"' existe deja, impossible de créer le compte partenaire ")
|
|
return False, "l'adresse email '"+str(mydata['email'])+"' existe deja, impossible de créer le compte partenaire "
|
|
|
|
ret_val = coll_name.insert_one(mydata)
|
|
mail.send_partner_account_mail(ret_val.inserted_id, str(mydata['email']))
|
|
return True, " Le partenaire a été créé"
|
|
|
|
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 créer le partenaire"
|
|
|
|
|
|
|
|
'''
|
|
partner Securité :
|
|
Cette fonction met à jour l'adresse email du partenaire
|
|
'''
|
|
def update_partner_main_mail(diction):
|
|
try:
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# 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', 'mail', 'new_mail', 'conf_new_mail']
|
|
incom_keys = diction.keys()
|
|
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " : Le champ '" + val + "' n'est pas autorisé ")
|
|
return False, " Creation partner account : Le champ '" + val + "' n'est pas accepté "
|
|
|
|
'''
|
|
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', 'mail', 'new_mail', 'conf_new_mail']
|
|
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, " La valeur '" + val + "' n'est pas presente dans liste "
|
|
|
|
# Recuperation du recid du partner
|
|
mydata = {}
|
|
mytoken = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mytoken = diction['token']
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(mytoken)
|
|
if( partner_recid is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid du partenaire")
|
|
return False, " Impossible de mettre à jour le partenaire "
|
|
|
|
mymail=""
|
|
if ("mail" in diction.keys()):
|
|
if diction['mail']:
|
|
mymail = diction['mail']
|
|
|
|
|
|
newmail = ""
|
|
if ("new_mail" in diction.keys()):
|
|
if diction['new_mail']:
|
|
newmail = diction['new_mail']
|
|
mydata['email'] = diction['new_mail']
|
|
|
|
|
|
confnewmail = ""
|
|
if ("conf_new_mail" in diction.keys()):
|
|
if diction['conf_new_mail']:
|
|
confnewmail = diction['conf_new_mail']
|
|
|
|
|
|
if ( len(newmail) <= 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - le nouvel email est vide")
|
|
return False, " Impossible de mettre à jour l'adresse email du partenaire "
|
|
|
|
if( str(newmail) != str(confnewmail)) :
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Les emails ne sont pas identiques")
|
|
return False, " Les emails ne sont pas identiques"
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
|
|
coll_name = dbname['partnair_account']
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'recid': str(partner_recid), 'locked': '0', 'active': '1', 'email':mymail},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + "L'adresse mail du partenaire a bien ete mis à jour =" + str(ret_val['_id']))
|
|
return True, "L'adresse mail du partenaire a bien ete mis à jour"
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour l'adresse mail du partenaire recid= : " +str(partner_recid) )
|
|
return False, " Impossible de mettre à jour l'adresse mail du partenaire"
|
|
|
|
|
|
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 l'adresse mail du partenaire"
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
partner Securité :
|
|
Cette fonction met à jour le mot de passe du partenaire
|
|
'''
|
|
def update_partner_pwd(diction):
|
|
try:
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# 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', 'pwd', 'new_pwd', 'conf_new_pwd']
|
|
incom_keys = diction.keys()
|
|
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " : Le champ '" + val + "' n'est pas autorisé ")
|
|
return False, " Le champ '" + val + "' n'est pas accepté "
|
|
|
|
'''
|
|
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', 'pwd', 'new_pwd', 'conf_new_pwd']
|
|
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, " La valeur '" + val + "' n'est pas presente dans liste "
|
|
|
|
# Recuperation du recid du partner
|
|
mydata = {}
|
|
mytoken = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mytoken = diction['token']
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(mytoken)
|
|
if( partner_recid is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid du partenaire")
|
|
return False, " Impossible de mettre à jour le partenaire "
|
|
|
|
mypwd=""
|
|
if ("pwd" in diction.keys()):
|
|
if diction['pwd']:
|
|
mypwd = diction['pwd']
|
|
|
|
|
|
newpwd = ""
|
|
if ("new_pwd" in diction.keys()):
|
|
if diction['new_pwd']:
|
|
newpwd = diction['new_pwd']
|
|
mydata['pwd'] = diction['new_pwd']
|
|
|
|
|
|
confnewpwd = ""
|
|
if ("conf_new_pwd" in diction.keys()):
|
|
if diction['conf_new_pwd']:
|
|
confnewpwd = diction['conf_new_pwd']
|
|
|
|
|
|
if ( len(newpwd) <= 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - le nouveau mot de passe est vide")
|
|
return False, " Impossible de mettre à jour le mot de passe "
|
|
|
|
if( str(newpwd) != str(confnewpwd)) :
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Les mots de passe ne sont pas identiques")
|
|
return False, " Les mots de passe ne sont pas identiques "
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
#print(mydata)
|
|
|
|
|
|
coll_name = dbname['partnair_account']
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'recid': str(partner_recid), 'locked': '0', 'active': '1', 'pwd':mypwd},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + "Le mot de passe du partenaire a bien ete mis à jour =" + str(ret_val['_id']))
|
|
return True, "Le mot de passe du partenaire a bien ete mis à jour"
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour le mot de passe du partenaire recid= : " +str(partner_recid) )
|
|
return False, " Impossible de mettre à jour le mot de passe du partenaire"
|
|
|
|
|
|
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 le mot de passe du partenaire"
|
|
|
|
|
|
'''
|
|
MAJ d'un partenaire.
|
|
la clé est l'adresse email principale
|
|
'''
|
|
def update_partner_account(diction):
|
|
try:
|
|
|
|
# Dictionnaire des champs utilisables
|
|
'''
|
|
# 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 = ['nom', 'adr_street', 'adr_city', 'adr_zip', 'adr_country', 'link_linkedin',
|
|
'email', 'pwd', 'telephone', 'contact_nom', 'contact_prenom', 'contact_tel',
|
|
'contact_mail', 'token', 'link_facebook', 'link_twitter']
|
|
incom_keys = diction.keys()
|
|
|
|
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " : Le champ '"+val + "' n'est pas autorisé ")
|
|
return False, " Le champ '" + val + "' n'est pas accepté "
|
|
|
|
'''
|
|
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, " La valeur '" + val + "' n'est pas presente dans liste "
|
|
|
|
# recuperation des paramettre
|
|
my_email = ""
|
|
my_token = ""
|
|
|
|
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
my_token = diction['token']
|
|
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", my_token)
|
|
|
|
if retval is False :
|
|
return False, " Le token n'est pas valide"
|
|
# Recuperation du recid de l'utilisateur
|
|
|
|
partner_recid = mycommon.get_parnter_recid_from_token(str(my_token))
|
|
if partner_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid du partenaire")
|
|
return False, " Impossible de mettre à jour le partenaire "
|
|
|
|
mydata = {}
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
mydata['nom'] = diction['nom']
|
|
|
|
if ("adr_street" in diction.keys()):
|
|
if diction['adr_street']:
|
|
mydata['adr_street'] = diction['adr_street']
|
|
|
|
if ("adr_city" in diction.keys()):
|
|
if diction['adr_city']:
|
|
mydata['adr_city'] = diction['adr_city']
|
|
|
|
if ("adr_zip" in diction.keys()):
|
|
if diction['adr_zip']:
|
|
mydata['adr_zip'] = diction['adr_zip']
|
|
|
|
if ("adr_country" in diction.keys()):
|
|
if diction['adr_country']:
|
|
mydata['adr_country'] = diction['adr_country']
|
|
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
my_email = diction['email']
|
|
|
|
if ("telephone" in diction.keys()):
|
|
if diction['telephone']:
|
|
mydata['telephone'] = diction['telephone']
|
|
|
|
if ("pwd" in diction.keys()):
|
|
if diction['pwd']:
|
|
mydata['pwd'] = diction['pwd']
|
|
|
|
if ("contact_nom" in diction.keys()):
|
|
if diction['contact_nom']:
|
|
mydata['contact_nom'] = diction['contact_nom']
|
|
|
|
if ("contact_prenom" in diction.keys()):
|
|
if diction['contact_prenom']:
|
|
mydata['contact_prenom'] = diction['contact_prenom']
|
|
|
|
if ("contact_tel" in diction.keys()):
|
|
if diction['contact_tel']:
|
|
mydata['contact_tel'] = diction['contact_tel']
|
|
|
|
if ("contact_mail" in diction.keys()):
|
|
if diction['contact_mail']:
|
|
mydata['contact_mail'] = diction['contact_mail']
|
|
|
|
if ("link_linkedin" in diction.keys()):
|
|
if diction['link_linkedin']:
|
|
mydata['link_linkedin'] = diction['link_linkedin']
|
|
|
|
if ("link_facebook" in diction.keys()):
|
|
if diction['link_facebook']:
|
|
mydata['link_facebook'] = diction['link_facebook']
|
|
|
|
if ("link_twitter" in diction.keys()):
|
|
if diction['link_twitter']:
|
|
mydata['link_twitter'] = diction['link_twitter']
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
#print(str(datetime.now()) + " webservice : diction = " + str(mydata))
|
|
|
|
coll_name = dbname['partnair_account']
|
|
|
|
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'recid': str(partner_recid), 'locked': '0', 'active': '1'},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + "Le partenaire a bien ete mise à jour =" + str(ret_val['_id']))
|
|
return True, "Le partenaire a bien ete mise à jour"
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour le partenaire recid= : " +str(partner_recid) )
|
|
return False, " Impossible de mettre à jour le partenaire"
|
|
|
|
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 le partenaire"
|
|
|
|
|
|
'''
|
|
cette fonction valide un compte partenaire
|
|
La modification ne s'effectue que si le compte n'est pas verrouillé.
|
|
'locked'=0
|
|
|
|
'''
|
|
def valide_partnair_account(value):
|
|
try:
|
|
client = MongoClient(CONNECTION_STRING)
|
|
dbname = client['cherifdb']
|
|
coll_name = dbname['partnair_account']
|
|
|
|
tmp = coll_name.find({'_id':ObjectId(str(value))})
|
|
|
|
print(" ########## Email = "+str(tmp[0]['email']))
|
|
print(" ########## recid = " + str(tmp[0]['recid']))
|
|
|
|
# A ce niveau le login et passe sont ok.
|
|
# il faut donc créer le token et renvoyer le token.
|
|
my_token = mycommon.create_token_urlsafe()
|
|
|
|
now = datetime.now()
|
|
ret_val = coll_name.find_and_modify(query={'_id':ObjectId(str(value)), 'locked':'0'},
|
|
update={"$set":
|
|
{'active': "1","date_update":str(now), 'token':str(my_token)}
|
|
}
|
|
)
|
|
|
|
if( ret_val and ret_val['_id']):
|
|
mycommon.myprint("La modif a bien ete faite Ajout Token OK, ="+str(ret_val['_id']))
|
|
return 'True', my_token
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Aucune modif n'a été faite")
|
|
return False, "Impossible de valider le compte"
|
|
|
|
|
|
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 valider le compte"
|
|
|
|
|
|
|
|
|
|
'''
|
|
Recuperation d'un partenaire
|
|
'''
|
|
def get_partner_account(diction):
|
|
try:
|
|
# Dictionnaire des champs utilisables
|
|
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]) + " - Le champ '" + val + "' n'existe pas, requete annulée")
|
|
return False, " Impossible de recuperer les informations"
|
|
|
|
'''
|
|
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 les informations"
|
|
|
|
# recuperation des paramettre
|
|
email_value = ""
|
|
|
|
|
|
token_value = ""
|
|
if diction['token']:
|
|
token_value = diction['token']
|
|
|
|
|
|
coll_name = dbname['partnair_account']
|
|
|
|
# Verification de la valididé du token
|
|
coll_token = dbname['user_token']
|
|
|
|
# Verification de la validité du token/mail dans le cas des user en mode connecté
|
|
retval = mycommon.check_partner_token_validity(email_value, token_value )
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
|
|
return False, " Impossible de recuperer les informations"
|
|
|
|
|
|
'''tmp_count = coll_token.find({'email':email_value, 'token': token_value, 'valide': '1'}).count()
|
|
|
|
if (tmp_count <= 0):
|
|
mycommon.myprint("L'email ou le token ne sont pas valident")
|
|
return False
|
|
'''
|
|
|
|
coll_token = dbname['partner_token']
|
|
tmp = coll_token.find({ 'token': str(token_value), 'valide': '1'})
|
|
partnere_recid = tmp[0]['recid']
|
|
|
|
print(" parters RECID = "+str(partnere_recid))
|
|
|
|
RetObject = []
|
|
for retVal in coll_name.find({'recid': str(partnere_recid)}):
|
|
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) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recuperer les informations"
|
|
|
|
|
|
|
|
'''
|
|
|
|
Login du partenaire avec
|
|
- mail
|
|
- passwd
|
|
- cle secrete qui est son token fixe'''
|
|
def partner_login(diction):
|
|
try:
|
|
|
|
# Dictionnaire des champs utilisables
|
|
field_list = ['email', 'pwd', 'secret']
|
|
incom_keys = diction.keys()
|
|
|
|
'''
|
|
# 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.
|
|
'''
|
|
for val in incom_keys:
|
|
if str(val).lower() not in str(field_list).lower():
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas accepté dans cette API")
|
|
return False, " Impossible de se connecter"
|
|
|
|
'''
|
|
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 = ['email', 'pwd', 'secret']
|
|
for val in field_list_obligatoire:
|
|
if str(val).lower() not in diction:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " La valeur '" + val + "' n'est pas presente dans liste des champs")
|
|
return False, "Impossible de se connecter"
|
|
|
|
|
|
mydata={}
|
|
|
|
# recuperation des paramettre
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
email = diction['email']
|
|
mydata['email'] = diction['email']
|
|
|
|
|
|
if ("pwd" in diction.keys()):
|
|
if diction['pwd']:
|
|
pwd = diction['pwd']
|
|
|
|
if ("secret" in diction.keys()):
|
|
if diction['secret']:
|
|
token = diction['secret']
|
|
mydata['token'] = diction['secret']
|
|
|
|
now = str(datetime.now())
|
|
|
|
mydata['valide'] = "1"
|
|
mydata['date_update'] = now
|
|
|
|
|
|
# Verification que les informations sont juste
|
|
coll_name = dbname['partnair_account']
|
|
ret_val = coll_name.find_one_and_update({'email': str(email), 'pwd': str(pwd), 'token': str(token), 'active': '1', 'locked':'0'},
|
|
{"$set": {'last_connexion':str(now)}},
|
|
upsert=True,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ret_val and ret_val['_id']:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Connexion partener "+email+" : OK, Date : "+ str(now)+" , _id = " + str(ret_val['_id']))
|
|
# Vu que les credentials sont ok, alors enregistrement de la connexion dans la table "partner token"
|
|
|
|
#print(" GRRRRRRRRRRRRRRRRRRR "+str(ret_val))
|
|
mydata['recid'] = str(ret_val['recid'])
|
|
token_collection = dbname['partner_token']
|
|
|
|
ret_val2 = token_collection.find_one_and_update({'recid': str(mydata['recid']), 'valide': '1', 'locked':'0'},
|
|
{"$set": mydata},
|
|
upsert=True,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ret_val2 and ret_val2['_id']:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Connexion partener_token : OK , _id = " + str(ret_val2['_id']))
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Impossible d'enregistrer le token du partenare dans partener_token ")
|
|
return False, " Impossible de se connecter"
|
|
|
|
|
|
return True, " Partner Connexion : OK"
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Impossible de se connecter - Partner ")
|
|
return False, " Impossible de se connecter"
|
|
|
|
|
|
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 recuperer la formation"
|