1406 lines
56 KiB
Python
1406 lines
56 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
|
|
import GlobalVariable as MYSY_GV
|
|
import strype_payement as Stripe
|
|
import lms_chamilo.mysy_lms as mysy_lms
|
|
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
def default(self, o):
|
|
if isinstance(o, ObjectId):
|
|
return str(o)
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
|
|
'''
|
|
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', 'pack_service']
|
|
|
|
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 autorisé, Creation partenaire annulée")
|
|
return False, "Impossible de créer le partenaire. Toutes les informations fournies ne sont pas valables"
|
|
|
|
'''
|
|
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, Toutes les informations necessaires n'ont pas été fournies"
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
|
|
mydata['invoice_vat_num'] = ""
|
|
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
mydata['nom'] = diction['nom']
|
|
mydata['invoice_nom'] = diction['nom']
|
|
|
|
if ("adr_street" in diction.keys()):
|
|
if diction['adr_street']:
|
|
mydata['adr_street'] = diction['adr_street']
|
|
mydata['invoice_adr_street'] = diction['adr_street']
|
|
|
|
if ("adr_city" in diction.keys()):
|
|
if diction['adr_city']:
|
|
mydata['adr_city'] = diction['adr_city']
|
|
mydata['invoice_adr_city'] = diction['adr_city']
|
|
|
|
if ("adr_zip" in diction.keys()):
|
|
if diction['adr_zip']:
|
|
mydata['adr_zip'] = diction['adr_zip']
|
|
mydata['invoice_adr_zip'] = diction['adr_zip']
|
|
|
|
if ("adr_country" in diction.keys()):
|
|
if diction['adr_country']:
|
|
mydata['adr_country'] = diction['adr_country']
|
|
mydata['invoice_adr_country'] = diction['adr_country']
|
|
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
mydata['email'] = diction['email']
|
|
mydata['invoice_email'] = diction['email']
|
|
|
|
if ("telephone" in diction.keys()):
|
|
if diction['telephone']:
|
|
mydata['telephone'] = diction['telephone']
|
|
mydata['invoice_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']
|
|
|
|
if ("pack_service" in diction.keys()):
|
|
if diction['pack_service']:
|
|
mydata['pack_service'] = diction['pack_service']
|
|
|
|
|
|
# Creation du RecId du user
|
|
mydata['recid'] = mycommon.create_user_recid()
|
|
|
|
# Creation de la clé d'insertion
|
|
mydata['insert_key'] = mycommon.create_user_recid()
|
|
|
|
# Creation des champs pour VIDE pour stripe
|
|
mydata['stripe_account_id'] = ""
|
|
mydata['stripe_paymentmethod_id'] = ""
|
|
|
|
mydata['active'] = '0'
|
|
mydata['locked'] = '0'
|
|
mydata['ispending'] = "0"
|
|
|
|
print(str(datetime.now()) + " webservice : diction = "+str(mydata))
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
coll_name_user_account = MYSY_GV.dbname['user_account']
|
|
|
|
# Verification que cette adresse email n'existe pas. Ceci que soit entant que user ou entant que partner
|
|
#tmp = coll_name.find({'email': str(mydata['email']) }).count()
|
|
tmp = coll_name.count_documents({'email': str(mydata['email']), 'active':'1' })
|
|
|
|
#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'])+"' est deja utilisée, impossible de créer le compte partenaire "
|
|
|
|
tmp = coll_name_user_account.count_documents({'email': str(mydata['email']), 'active':'1'})
|
|
logging.info(" TMP = " + str(tmp))
|
|
if (tmp > 0):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - l'adresse email '" + str(
|
|
mydata['email']) + "' existe deja (utilisé comme compte user), impossible de créer le compte partenaire ")
|
|
return False, "l'adresse email '" + str(
|
|
mydata['email']) + "' est deja utilisée, 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 compte utilisateur a été correctement créé. Vous allez recevoir un email avec les informations de connexion."
|
|
|
|
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 compte utilisateur"
|
|
|
|
|
|
|
|
'''
|
|
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 récupérer 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 = MYSY_GV.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 récupérer 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 = MYSY_GV.dbname['partnair_account']
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'recid': str(partner_recid), 'locked': '0', 'active': '1', 'pwd':mypwd},
|
|
{"$set": mydata},
|
|
upsert= False,
|
|
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 dans Mysy =" + str(ret_val['_id']))
|
|
|
|
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"
|
|
|
|
"""
|
|
pres la modification du mot de passe dans Mysy, il faut voir si le partenaire
|
|
a un compte LMS. Si c'est le cas, alors il faut mettre à jour le mot de passe dans le compte LMS aussi.
|
|
"""
|
|
|
|
# Recuperation des données du partenaire
|
|
local_status, my_partner = mycommon.get_partner_data_from_recid(partner_recid)
|
|
if (local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire")
|
|
return False, str(inspect.stack()[0][3]) + " - impossible de récupérer les données du partenaire"
|
|
|
|
|
|
if ("mysy_lms_user_id" in my_partner.keys()):
|
|
if my_partner['mysy_lms_user_id']:
|
|
new_diction = {}
|
|
new_diction['token'] = my_partner['token']
|
|
new_diction['new_password'] = confnewpwd
|
|
new_diction['lms_user_id'] = my_partner['mysy_lms_user_id']
|
|
|
|
local_status, message, rowid = mysy_lms.Update_Passwd_MySy_LMS_User(new_diction)
|
|
if( local_status is False ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - WARNING : impossible de mettre le mot de passe à jour dans le LMS")
|
|
else:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - La mise à jour du mot de passe dans le LMS a été faite également ")
|
|
|
|
|
|
return True, " Le mot de passe a bien été changé"
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
"""
|
|
Cette fonction permet de mettre à jour la clé d'insertion.
|
|
Cette clé est utilisée pour permettre de rattacher une collaborateur à une entreprise
|
|
"""
|
|
def update_partner_insert_key(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', 'current_insert_key', 'new_insert_key', 'conf_insert_key']
|
|
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', 'current_insert_key', 'new_insert_key', 'conf_insert_key']
|
|
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 récupérer le recid du partenaire")
|
|
return False, " Impossible de mettre à jour la clé d'insertion "
|
|
|
|
mykey=""
|
|
if ("current_insert_key" in diction.keys()):
|
|
if diction['current_insert_key']:
|
|
mykey = diction['current_insert_key']
|
|
|
|
|
|
newkey = ""
|
|
if ("new_insert_key" in diction.keys()):
|
|
if diction['new_insert_key']:
|
|
newkey = diction['new_insert_key']
|
|
mydata['insert_key'] = diction['new_insert_key']
|
|
|
|
|
|
confkey = ""
|
|
if ("conf_insert_key" in diction.keys()):
|
|
if diction['conf_insert_key']:
|
|
confkey = diction['conf_insert_key']
|
|
|
|
|
|
if ( len(newkey) <= 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - la nouvelle clé d'insertion est vide")
|
|
return False, " Impossible de mettre à jour la clé d'insertion "
|
|
|
|
if( str(newkey) != str(confkey)) :
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - La nouvelle clé et la confirmation ne sont pas identiques")
|
|
return False, "La nouvelle clé et la confirmation ne sont pas identiques"
|
|
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
#print(mydata)
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
|
|
ret_val = coll_name.find_one_and_update(
|
|
{'recid': str(partner_recid), 'locked': '0', 'active': '1', 'insert_key':mykey},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
|
|
if (ret_val and ret_val['_id']):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + "La clé d'insertion du partenaire a bien ete mise à jour =" + str(ret_val['_id']))
|
|
return True, "La clé d'insertion a bien été mise à jour"
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la clé d'insertion du partenaire recid= : " +str(partner_recid) )
|
|
return False, " Impossible de mettre à jour la clé d'insertion"
|
|
|
|
|
|
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 clé d'insertion "
|
|
|
|
|
|
|
|
'''
|
|
MAJ d'un partenaire.
|
|
la clé est l'adresse email principale
|
|
'''
|
|
def update_partner_account(file_logo=None, file_cachet=None, Folder=None, diction=None):
|
|
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', 'invoice_vat_num',
|
|
'invoice_nom', 'invoice_adr_street', 'invoice_adr_city', 'invoice_adr_zip',
|
|
'invoice_adr_country', 'invoice_email', 'invoice_telephone', 'invoice_vat_num',
|
|
'siret', 'num_nda', 'iscertitrace', 'isdatadock', 'isqualiopi', 'website',
|
|
'isbureaucertitrace', 'iscertifvoltaire', 'file_logo_recid', 'file_cachet_recid']
|
|
|
|
|
|
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 ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
|
|
# Verifier la validité du token
|
|
retval = mycommon.check_partner_token_validity("", my_token)
|
|
|
|
if retval is False :
|
|
return False, " La session de connexion 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 récupérer le recid du partenaire")
|
|
return False, " Impossible de mettre à jour le partenaire "
|
|
|
|
mydata = {}
|
|
if ("nom" in diction.keys()):
|
|
mydata['nom'] = diction['nom']
|
|
|
|
if ("adr_street" in diction.keys()):
|
|
mydata['adr_street'] = diction['adr_street']
|
|
|
|
if ("adr_city" in diction.keys()):
|
|
mydata['adr_city'] = diction['adr_city']
|
|
|
|
if ("adr_zip" in diction.keys()):
|
|
mydata['adr_zip'] = diction['adr_zip']
|
|
|
|
if ("adr_country" in diction.keys()):
|
|
mydata['adr_country'] = diction['adr_country']
|
|
|
|
if ("siret" in diction.keys()):
|
|
mydata['siret'] = diction['siret']
|
|
|
|
if ("num_nda" in diction.keys()):
|
|
mydata['num_nda'] = diction['num_nda']
|
|
|
|
if ("iscertitrace" in diction.keys()):
|
|
mydata['iscertitrace'] = diction['iscertitrace']
|
|
|
|
if ("isbureaucertitrace" in diction.keys()):
|
|
mydata['isbureaucertitrace'] = diction['isbureaucertitrace']
|
|
|
|
if ("iscertifvoltaire" in diction.keys()):
|
|
mydata['iscertifvoltaire'] = diction['iscertifvoltaire']
|
|
|
|
if ("isdatadock" in diction.keys()):
|
|
mydata['isdatadock'] = diction['isdatadock']
|
|
|
|
if ("isqualiopi" in diction.keys()):
|
|
mydata['isqualiopi'] = diction['isqualiopi']
|
|
|
|
if ("website" in diction.keys()):
|
|
mydata['website'] = diction['website']
|
|
|
|
if ("email" in diction.keys()):
|
|
my_email = diction['email']
|
|
|
|
if ("telephone" in diction.keys()):
|
|
mydata['telephone'] = diction['telephone']
|
|
|
|
if ("pwd" in diction.keys()):
|
|
mydata['pwd'] = diction['pwd']
|
|
|
|
if ("contact_nom" in diction.keys()):
|
|
mydata['contact_nom'] = diction['contact_nom']
|
|
|
|
if ("contact_prenom" in diction.keys()):
|
|
mydata['contact_prenom'] = diction['contact_prenom']
|
|
|
|
if ("contact_tel" in diction.keys()):
|
|
mydata['contact_tel'] = diction['contact_tel']
|
|
|
|
if ("contact_mail" in diction.keys()):
|
|
mydata['contact_mail'] = diction['contact_mail']
|
|
|
|
if ("link_linkedin" in diction.keys()):
|
|
mydata['link_linkedin'] = diction['link_linkedin']
|
|
|
|
if ("link_facebook" in diction.keys()):
|
|
mydata['link_facebook'] = diction['link_facebook']
|
|
|
|
if ("link_twitter" in diction.keys()):
|
|
mydata['link_twitter'] = diction['link_twitter']
|
|
|
|
if ("invoice_vat_num" in diction.keys()):
|
|
mydata['invoice_vat_num'] = diction['invoice_vat_num']
|
|
|
|
if ("invoice_nom" in diction.keys()):
|
|
mydata['invoice_nom'] = diction['invoice_nom']
|
|
|
|
if ("invoice_adr_street" in diction.keys()):
|
|
mydata['invoice_adr_street'] = diction['invoice_adr_street']
|
|
|
|
if ("invoice_adr_city" in diction.keys()):
|
|
mydata['invoice_adr_city'] = diction['invoice_adr_city']
|
|
|
|
if ("invoice_adr_zip" in diction.keys()):
|
|
mydata['invoice_adr_zip'] = diction['invoice_adr_zip']
|
|
|
|
if ("invoice_adr_zip" in diction.keys()):
|
|
mydata['invoice_adr_country'] = diction['invoice_adr_country']
|
|
|
|
if ("invoice_email" in diction.keys()):
|
|
mydata['invoice_email'] = diction['invoice_email']
|
|
|
|
if ("invoice_telephone" in diction.keys()):
|
|
mydata['invoice_telephone'] = diction['invoice_telephone']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
#print(str(datetime.now()) + " webservice : diction = " + str(mydata))
|
|
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
|
|
"""
|
|
Si le compte partenaire n'as pas de compte de payement stripe, alors on profite de cette mise à jour pour le faire
|
|
"""
|
|
tmp = coll_name.find({'recid':str(partner_recid)})
|
|
print('### tmp = '+str(tmp[0]))
|
|
|
|
partnair_stripe_id = ""
|
|
if ("stripe_account_id" in tmp[0].keys()):
|
|
if tmp[0]['stripe_account_id']:
|
|
partnair_stripe_id = tmp[0]['stripe_account_id']
|
|
|
|
if ( partnair_stripe_id is None or len(partnair_stripe_id) < 5):
|
|
print('### le partenaire = ' + str(tmp[0]['nom']) + " n'as pas de compte Stripe. on va le créer")
|
|
"""
|
|
Creation du compte de payement Stripe
|
|
"""
|
|
|
|
my_stripe_data = {}
|
|
my_stripe_data['email'] = str(tmp[0]['email'])
|
|
my_stripe_data['name'] = str(tmp[0]['nom'])
|
|
|
|
my_stripe_data['city'] = ""
|
|
if ("adr_city" in tmp[0].keys()):
|
|
if tmp[0]['adr_city']:
|
|
my_stripe_data['city'] = str(tmp[0]['adr_city'])
|
|
|
|
my_stripe_data['country'] = ""
|
|
if ("adr_country" in tmp[0].keys()):
|
|
if tmp[0]['adr_country']:
|
|
my_stripe_data['country'] = str(tmp[0]['adr_country'])
|
|
|
|
local_status, part_stripe_account_id = Stripe.create_customer(my_stripe_data)
|
|
mydata['stripe_account_id'] = part_stripe_account_id
|
|
mydata['stripe_paymentmethod_id'] = ""
|
|
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - WARNING : Impossible de créer le compte STRIPE du Client " + str(
|
|
tmp[0]['nom']))
|
|
|
|
|
|
|
|
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 is None or '_id' not in ret_val.keys()):
|
|
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"
|
|
|
|
|
|
"""
|
|
A present que le partenaire est mis à jour / créé, on va mettre à jour les image logo et image cachet s'il y en a
|
|
"""
|
|
if( file_logo ):
|
|
recordimage_diction = {}
|
|
recordimage_diction['token'] = diction['token']
|
|
recordimage_diction['related_collection'] = "partnair_account"
|
|
recordimage_diction['type_img'] = "logo"
|
|
recordimage_diction['related_collection_recid'] = str(partner_recid)
|
|
recordimage_diction['image_recid'] = diction['file_logo_recid']
|
|
#print(" ### recordimage_diction 1 = ", recordimage_diction)
|
|
local_status, local_message = mycommon.recordClassImage_v2(file_logo, MYSY_GV.upload_folder, recordimage_diction)
|
|
if( local_status is False):
|
|
return local_status, local_message
|
|
|
|
if( file_cachet ):
|
|
recordimage_diction = {}
|
|
recordimage_diction['token'] = diction['token']
|
|
recordimage_diction['related_collection'] = "partnair_account"
|
|
recordimage_diction['type_img'] = "cachet"
|
|
recordimage_diction['related_collection_recid'] = str(partner_recid)
|
|
recordimage_diction['image_recid'] = diction['file_cachet_recid']
|
|
#print(" ### recordimage_diction 2 = ", recordimage_diction)
|
|
local_status, local_message = mycommon.recordClassImage_v2(file_cachet, MYSY_GV.upload_folder, recordimage_diction)
|
|
if (local_status is False):
|
|
return local_status, local_message
|
|
|
|
|
|
return True, "Le partenaire a bien ete mise à jour"
|
|
|
|
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
|
|
|
|
|
|
/!\ : Juste apres la validation, le compte de payement est créé dans Stripe
|
|
|
|
'''
|
|
def valide_partnair_account(value):
|
|
try:
|
|
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
|
|
tmp_count = coll_name.count_documents({'_id':ObjectId(str(value)), 'active':'1'})
|
|
if (tmp_count > 0):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Ce compte a deja été activé")
|
|
return False, "Ce compte a deja été activé "
|
|
|
|
|
|
tmp_count = coll_name.count_documents({'_id':ObjectId(str(value)), 'active':'0'})
|
|
|
|
if( tmp_count <= 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - il n'y aucun compte à activer")
|
|
return False, "Impossible de valider ce compte "
|
|
|
|
tmp = coll_name.find({'_id': ObjectId(str(value)), 'active': '0'})
|
|
partner_email = tmp[0]['email']
|
|
|
|
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()
|
|
|
|
"""
|
|
Creation du compte de payement Stripe
|
|
"""
|
|
|
|
my_stripe_data = {}
|
|
my_stripe_data['email'] = str(tmp[0]['email'])
|
|
my_stripe_data['name'] = str(tmp[0]['nom'])
|
|
|
|
my_stripe_data['city'] = ""
|
|
if ("adr_city" in tmp[0].keys()):
|
|
if tmp[0]['adr_city']:
|
|
my_stripe_data['city'] = str(tmp[0]['adr_city'])
|
|
|
|
|
|
my_stripe_data['adr_country'] = ""
|
|
if ("adr_country" in tmp[0].keys()):
|
|
if tmp[0]['adr_country']:
|
|
my_stripe_data['country'] = str(tmp[0]['adr_country'])
|
|
|
|
|
|
|
|
local_status, part_stripe_account_id = Stripe.create_customer(my_stripe_data)
|
|
if( local_status is False):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - WARNING : Impossible de créer le compte STRIPE du Client "+str(tmp[0]['nom']))
|
|
|
|
|
|
'''
|
|
Create default / temporary pwd for new account
|
|
'''
|
|
#my_tmp_pwd = mycommon.create_token_urlsafe();
|
|
|
|
now = datetime.now()
|
|
ret_val = coll_name.find_one_and_update({'_id':ObjectId(str(value)), 'locked':'0'},
|
|
{"$set":
|
|
{'active': "1","date_update":str(now),
|
|
'secret_key':str(my_token),
|
|
'stripe_account_id':part_stripe_account_id,
|
|
}
|
|
},
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if( ret_val and ret_val['_id']):
|
|
mycommon.myprint("La modif a bien ete faite Ajout secret_key OK, ="+str(ret_val['_id']))
|
|
# Envoie du mail de notification
|
|
mail.Pro_Account_Token_Pass(partner_email, my_token)
|
|
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 récupérer 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 récupérer les informations"
|
|
|
|
# recuperation des paramettre
|
|
email_value = ""
|
|
|
|
|
|
token_value = ""
|
|
if diction['token']:
|
|
token_value = diction['token']
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
|
|
# Verification de la valididé du token
|
|
coll_token = MYSY_GV.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]) + " - La session de connexion n'est pas valide")
|
|
return False, " Impossible de récupérer 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 = MYSY_GV.dbname['partner_token']
|
|
tmp = coll_token.find({ 'token': str(token_value), 'valide': '1'})
|
|
partnere_recid = tmp[0]['recid']
|
|
|
|
#print(" #### parters RECID = "+str(partnere_recid))
|
|
|
|
is_first_connexion = True
|
|
|
|
RetObject = []
|
|
for retVal in coll_name.find({'recid': str(partnere_recid)}):
|
|
#mycommon.myprint (str(retVal))
|
|
if ("firstconnexion" in retVal.keys()):
|
|
if retVal['firstconnexion'] and str(retVal['firstconnexion'] == "0"):
|
|
is_first_connexion = False
|
|
|
|
user = retVal
|
|
RetObject.append(JSONEncoder().encode(user))
|
|
|
|
|
|
"""
|
|
Si la première connexion, alors on met la Le champ avec la valeur firstconnexion = 0
|
|
"""
|
|
my_new_data = {}
|
|
now = str(datetime.now())
|
|
|
|
if( is_first_connexion is True):
|
|
my_new_data = {'firstconnexion':'0', 'lastconnexion':now}
|
|
else:
|
|
my_new_data = {'lastconnexion': now}
|
|
|
|
ret_val = coll_name.find_one_and_update({'recid': str(partnere_recid)},
|
|
{"$set": my_new_data},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ret_val and ret_val['_id']:
|
|
mycommon.myprint("Connexion du partner recid = "+str(partnere_recid)+" OK. Mise à jour du firstconnexion et/ou lastconnexion : OK")
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) +
|
|
" WARNING : Impossible de mettre à jour du firstconnexion et/ou lastconnexion du partner recid = "+str(partnere_recid)+". ")
|
|
|
|
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 récupérer les informations"
|
|
|
|
|
|
"""
|
|
Recuperation des images d'un partenaire (logo & cachet)
|
|
"""
|
|
|
|
def getRecodedParnterImage_from_front(diction=None):
|
|
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 récupérer 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 récupérer les informations"
|
|
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
qery_images = {'locked': '0', 'valide': '1', 'related_collection': 'partnair_account',
|
|
'related_collection_recid': str(my_partner['recid'])}
|
|
|
|
print(" ### qery_images = ", qery_images)
|
|
|
|
RetObject = []
|
|
partner_images = {}
|
|
# Recuperation des image 'logo' et 'cachet' si le partenaire en a
|
|
for retVal in MYSY_GV.dbname['mysy_images'].find(qery_images):
|
|
if ('type_img' in retVal.keys()):
|
|
if (retVal['type_img'] == "logo"):
|
|
partner_images['logo_img'] = retVal['img'].decode()
|
|
partner_images['logo_img_recid'] = retVal['recid']
|
|
|
|
if (retVal['type_img'] == "cachet"):
|
|
partner_images['cachet_img'] = retVal['img'].decode()
|
|
partner_images['cachet_img_recid'] = retVal['recid']
|
|
|
|
RetObject.append(JSONEncoder().encode(partner_images))
|
|
|
|
print(" ### getRecodedParnterImage_from_front RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recupérer les images/médias du partenaire"
|
|
|
|
|
|
|
|
'''
|
|
|
|
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', ]
|
|
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, str(inspect.stack()[0][3]) + " Toutes les informations obligatoires ne sont pas fournies"
|
|
|
|
'''
|
|
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', ]
|
|
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, str(inspect.stack()[0][3]) + " Toutes les informations obligatoires ne sont pas fournies (2) "
|
|
|
|
|
|
mydata={}
|
|
|
|
# recuperation des paramettre
|
|
email = ""
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
email = diction['email']
|
|
mydata['email'] = diction['email']
|
|
|
|
pwd = ""
|
|
if ("pwd" in diction.keys()):
|
|
if diction['pwd']:
|
|
pwd = diction['pwd']
|
|
|
|
# Verifier la taille des infos saisies. Pour le login et pass, la longeur max autorisée est 100 caractère
|
|
if (len(str(email)) > 100 or len(str(pwd)) > 100):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Les informations de login et/ou mot de passe sont incorrectes")
|
|
return False, " Les informations de login et/ou mot de passe sont incorrectes."
|
|
|
|
# Verifier la taille des infos saisies. Pour le login et pass, la longeur max autorisée est 100 caractère
|
|
if (len(str(email)) <= 0 or len(str(pwd)) <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Les informations de login et/ou mot de passe sont incorrectes")
|
|
return False, " Les informations de login et/ou mot de passe sont incorrectes."
|
|
|
|
|
|
"""
|
|
Verifier si le compte ne serait pas verrouillé
|
|
"""
|
|
|
|
tmp_count_verrou = MYSY_GV.dbname['partnair_account'].count_documents({'email':email, 'pwd':pwd, 'locked':'1', 'active': '1'})
|
|
if (tmp_count_verrou > 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Ce compte " + str(email) + " est actuellement verrouillé")
|
|
return False, " Ce compte " + str(email) + " est actuellement verrouillé."
|
|
|
|
# Verification si ce user existe (controle : login et passwd)
|
|
tmp_count = MYSY_GV.dbname['partnair_account'].count_documents({'email':email, 'pwd':pwd, 'locked':'0', 'active': '1'})
|
|
|
|
if (tmp_count != 1):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Login et/ou mot de passe invalide " + str( email) + " ")
|
|
return False, " Login et/ou mot de passe invalide, merci de verifier votre saisie. "
|
|
|
|
|
|
# Recuperation des données du partenaire qui se connecte
|
|
my_partner = MYSY_GV.dbname['partnair_account'].find_one(
|
|
{'email': email, 'pwd': pwd, 'locked': '0', 'active': '1'})
|
|
|
|
partner_recid = my_partner['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()
|
|
|
|
# 1 - Vu le login et passe sont valident, alors on va aller desactier un eventuel token qui existerait
|
|
local_now = str(datetime.now())
|
|
local_result = MYSY_GV.dbname['partner_token'].update_many(
|
|
{"recid": partner_recid, 'valide': '1'},
|
|
{
|
|
"$set": {"valide": '0', 'end_date': str(datetime.now()), 'date_update': str(datetime.now())}
|
|
})
|
|
|
|
# Enregistrement du nouveau token
|
|
tokenfield = {}
|
|
tokenfield['email'] = email
|
|
tokenfield['recid'] = partner_recid
|
|
tokenfield['date_update'] = str(datetime.now())
|
|
tokenfield['valide'] = '1'
|
|
tokenfield['locked'] = '0'
|
|
tokenfield['start_date'] = str(datetime.now())
|
|
tokenfield['token'] = str(my_token)
|
|
|
|
'''
|
|
Insertion du nouveau token
|
|
'''
|
|
ret_val = MYSY_GV.dbname['partner_token'].insert_one(tokenfield, )
|
|
|
|
if (not ret_val.inserted_id):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Impossible de finaliser la creation du token du partner")
|
|
return False, "Impossible de se connecter, merci de contacter le support technique (1)"
|
|
|
|
|
|
|
|
now = str(datetime.now())
|
|
|
|
"""
|
|
Mise à jour du compte du partenaie
|
|
avec les info de last connexion et de token
|
|
"""
|
|
partner_update_data = {}
|
|
partner_update_data['lastconnexion'] = str(datetime.now())
|
|
partner_update_data['token'] = str(my_token)
|
|
|
|
|
|
mydata['valide'] = "1"
|
|
mydata['date_update'] = now
|
|
|
|
partner_key_secrete_key = ""
|
|
|
|
# Verification que les informations sont juste
|
|
coll_name = MYSY_GV.dbname['partnair_account']
|
|
ret_val = coll_name.find_one_and_update({'email': str(email), 'pwd': str(pwd), 'active': '1', 'locked':'0'},
|
|
{"$set":partner_update_data},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if ("_id" not in ret_val.keys()):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Impossible de récupérer les données de l'utilisateur (2)")
|
|
return False, " Impossible de récupérer les données de l'utilisateur (2) "
|
|
|
|
return_data = {}
|
|
return_data['token'] = str(my_token)
|
|
|
|
print(" ### token partner = ", str(my_token))
|
|
|
|
RetObject = []
|
|
RetObject.append(JSONEncoder().encode(return_data))
|
|
|
|
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 se connecter au système. Merci de contacter votre administrateur "
|
|
|
|
|
|
"""
|
|
Cette fonction upgrade un compte utilisateur vers
|
|
un compte PRO (aussi appelé compte partenaire).
|
|
|
|
Ceci revient à créer un nouveau compte utilisateur. aini la personne en face
|
|
aura
|
|
- un compte utilisateur classique et
|
|
- un compte pro
|
|
|
|
En suite on desactive le compte utilisateur classique et on maintient que le compte pro.
|
|
|
|
Pour migrer un compte utilisateur classique vers un compte pro, voici ce qu'il faut faire :
|
|
|
|
1 - récupérer les infos du contact . ici à partir du token de la personne connectée, on peut aller chercher ses info.
|
|
2 - récupérer les infos de la société
|
|
3 - récupérer les infos sur pack
|
|
|
|
etape 1 : add_partner_account(diction)
|
|
etape 2 : Desactivation du compte utilisateur
|
|
"""
|
|
def UpgradetoPro(diction):
|
|
try:
|
|
field_list = ['token', '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', 'pack_service']
|
|
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, UpgradetoPro 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 = ['token', 'nom', 'adr_street', 'adr_city', 'adr_zip', 'adr_country',
|
|
'email', 'telephone', 'pack_service']
|
|
|
|
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"
|
|
|
|
token = ""
|
|
new_diction = {}
|
|
user_recid = "None"
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
|
|
if ( len(str(token)) > 0 ):
|
|
retval = mycommon.check_token_validity("", token)
|
|
|
|
if retval is False :
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - La session de connexion n'est pas valide")
|
|
return "Err_Connexion", "La session de connexion n'est pas valide"
|
|
|
|
# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_user_recid_from_token(token)
|
|
if user_recid is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3])+" - Impossible de récupérer le token de l'utilisateur")
|
|
return False, " Impossible de migrer vers le compte pro"
|
|
|
|
|
|
# Recuperation des paramettre du compe
|
|
if ("nom" in diction.keys()):
|
|
if diction['nom']:
|
|
new_diction['nom'] = diction['nom']
|
|
|
|
if ("adr_street" in diction.keys()):
|
|
if diction['adr_street']:
|
|
new_diction['adr_street'] = diction['adr_street']
|
|
|
|
if ("adr_city" in diction.keys()):
|
|
if diction['adr_city']:
|
|
new_diction['adr_city'] = diction['adr_city']
|
|
|
|
if ("adr_zip" in diction.keys()):
|
|
if diction['adr_zip']:
|
|
new_diction['adr_zip'] = diction['adr_zip']
|
|
|
|
if ("adr_country" in diction.keys()):
|
|
if diction['adr_country']:
|
|
new_diction['adr_country'] = diction['adr_country']
|
|
|
|
if ("link_linkedin" in diction.keys()):
|
|
if diction['link_linkedin']:
|
|
new_diction['link_linkedin'] = diction['link_linkedin']
|
|
|
|
if ("link_facebook" in diction.keys()):
|
|
if diction['link_facebook']:
|
|
new_diction['link_facebook'] = diction['link_facebook']
|
|
|
|
if ("link_twitter" in diction.keys()):
|
|
if diction['link_twitter']:
|
|
new_diction['link_twitter'] = diction['link_twitter']
|
|
|
|
if ("email" in diction.keys()):
|
|
if diction['email']:
|
|
new_diction['email'] = diction['email']
|
|
|
|
if ("telephone" in diction.keys()):
|
|
if diction['telephone']:
|
|
new_diction['telephone'] = diction['telephone']
|
|
|
|
|
|
if ("pack_service" in diction.keys()):
|
|
if diction['pack_service']:
|
|
new_diction['pack_service'] = diction['pack_service']
|
|
|
|
'''
|
|
Create default pwd for new account
|
|
'''
|
|
new_diction['pwd'] = mycommon.create_token_urlsafe();
|
|
|
|
|
|
'''
|
|
Recuperation des information du contact.
|
|
Pour memo, le compte utilisateur qui fait cette migration devient automatiquement
|
|
le contact principal du compte pro.
|
|
'''
|
|
coll_user_account = MYSY_GV.dbname['user_account']
|
|
for x in coll_user_account.find({"recid": user_recid}):
|
|
if ("email" in x.keys()):
|
|
if x['email']:
|
|
new_diction['contact_mail'] = x['email']
|
|
|
|
if ("last_name" in x.keys()):
|
|
if x['last_name']:
|
|
new_diction['contact_nom'] = x['last_name']
|
|
|
|
if ("surname" in x.keys()):
|
|
if x['surname']:
|
|
new_diction['contact_prenom'] = x['surname']
|
|
|
|
if ("mob_phone" in x.keys()):
|
|
if x['mob_phone']:
|
|
new_diction['contact_tel'] = x['mob_phone']
|
|
|
|
print(" new_diction la = "+str(new_diction))
|
|
|
|
'''
|
|
Verification que cette adresse email n'hesiste pas deja dans
|
|
'''
|
|
check_tmp = coll_user_account.count_documents({'email': str(diction['email'])})
|
|
|
|
if (check_tmp < 0):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Ce compte partenaire existe deja")
|
|
return False, "Ce compte partenaire existe deja"
|
|
|
|
status, retval = add_partner_account(new_diction)
|
|
|
|
if( status ):
|
|
'''
|
|
La migration s'est bien passée.
|
|
Maintenant mettre le compte utilisateur :
|
|
- "migrated : 1"
|
|
- "migrated_mail_account" : le mail du compte pro associé
|
|
- "migration_date" : datetime.now()
|
|
|
|
'''
|
|
mydata = {}
|
|
mydata['migrated'] = "1"
|
|
mydata['ispending'] = "0"
|
|
mydata['migrated_date'] = str(datetime.now())
|
|
mydata['migrated_mail_account'] = new_diction['email']
|
|
ret_val = coll_user_account.find_one_and_update(
|
|
{"recid": user_recid},
|
|
{"$set": mydata},
|
|
return_document=ReturnDocument.AFTER )
|
|
if (ret_val and ret_val['_id']):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " le compte recid "+str(user_recid)+" migré en compte pro "+str(mydata))
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " impossible de migrer le user_account : " + str(user_recid)+" en compte pro")
|
|
return False, " Impossible de finaliser la migration vers le compte PRO : "
|
|
|
|
|
|
else:
|
|
return False, "Impossible de migrer le compte vers le compte pro"
|
|
|
|
return True, "Le compte à été bien migré vers un compte pro"
|
|
|
|
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 migrer vers le compte pro"
|