From e58000d9685ce3775336c1bee4f824398e3f4cee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ch=C3=A9rifBALDE?= Date: Mon, 29 Aug 2022 16:00:33 +0200 Subject: [PATCH] 29/08/22 -16h --- GlobalVariable.py | 30 +++-- articles_avis.py | 5 - class_mgt.py | 262 +++++++++++++++++++++++++++++++++++++++++--- ela_user_account.py | 6 - main.py | 40 ++++++- partners.py | 4 - user_session.py | 3 - 7 files changed, 304 insertions(+), 46 deletions(-) diff --git a/GlobalVariable.py b/GlobalVariable.py index 79ee4bc..b3edc8b 100644 --- a/GlobalVariable.py +++ b/GlobalVariable.py @@ -10,9 +10,21 @@ from autocorrect import Speller from nltk.corpus import stopwords import ela_spacy_common as lsc -CONNECTION_STRING = "mongodb://localhost:27017/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] +""" +Cette variable definit l'environnement de travail +""" +#MYSY_ENV = "PROD" +MYSY_ENV = "DEV" + +if (MYSY_ENV == "PROD"): + CONNECTION_STRING = "mongodb://localhost:27017/cherifdb" + client = MongoClient(CONNECTION_STRING) + dbname = client['cherifdb'] + +elif (MYSY_ENV == "DEV"): + CONNECTION_STRING = "mongodb://localhost:27017/cherifdb_dev" + client = MongoClient(CONNECTION_STRING) + dbname = client['cherifdb_dev'] @@ -21,12 +33,14 @@ YTUBES_CONNECTION_STRING = "mongodb://localhost:27017/" YTUBES_client = MongoClient(YTUBES_CONNECTION_STRING) YTUBES_dbname = YTUBES_client['mysyvideodb'] -""" -Cette variable definit l'environnement de travail -""" -#MYSY_ENV = "PROD" -MYSY_ENV = "DEV" +""" +Cette Variable definit les port de connexion +en dev ou en production +""" +MYSY_PORT_PROD = 5000 +MYSY_PORT_DEV= 5001 + ''' diff --git a/articles_avis.py b/articles_avis.py index b64b5b1..a062126 100644 --- a/articles_avis.py +++ b/articles_avis.py @@ -32,11 +32,6 @@ class JSONEncoder(json.JSONEncoder): print(o) return json.JSONEncoder.default(self, o) -CONNECTION_STRING = "mongodb://localhost/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] - - ''' Recuperation des tous avis et commentaires diff --git a/class_mgt.py b/class_mgt.py index 94dc68d..3d91c5a 100644 --- a/class_mgt.py +++ b/class_mgt.py @@ -27,9 +27,7 @@ class JSONEncoder(json.JSONEncoder): return str(o) return json.JSONEncoder.default(self, o) -CONNECTION_STRING = "mongodb://localhost/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] + MAX_KEYWORD = 3 @@ -49,7 +47,7 @@ def add_class(diction): ''' 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', 'metier', 'date_lieu'] + 'internal_url', 'zone_diffusion', 'metier', 'date_lieu', 'published'] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: @@ -155,6 +153,10 @@ def add_class(diction): if diction['trainer']: mydata['trainer'] = diction['trainer'] + if ("published" in diction.keys()): + if diction['published']: + mydata['published'] = diction['published'] + if ("plus_produit" in diction.keys()): if diction['plus_produit']: mydata['plus_produit'] = diction['plus_produit'] @@ -387,7 +389,8 @@ def update_class(diction): ''' 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', 'metier', 'date_lieu'] + 'domaine', 'internal_code', 'internal_url','zone_diffusion', 'metier', + 'date_lieu', 'published'] incom_keys = diction.keys() for val in incom_keys: @@ -461,6 +464,10 @@ def update_class(diction): if ("metier" in diction.keys()): mydata['metier'] = diction['metier'] + if ("published" in diction.keys()): + mydata['published'] = diction['published'] + + if ("institut_formation" in diction.keys()): mydata['institut_formation'] = diction['institut_formation'] @@ -946,8 +953,6 @@ Verrouillage d'une formation lock a training set mydata['locked'] = '1' ''' - - def lock_class(diction): try: @@ -1019,6 +1024,10 @@ def lock_class(diction): coll_name = MYSY_GV.dbname['myclass'] # seules les formation avec locked = 1 et valide=1 sont 'unlockable' + print( "str(my_internal_url) = "+str(my_internal_url)+" --- partner_recid = " + +partner_recid+" mydata = "+str(mydata)) + + ret_val = coll_name.find_one_and_update( {'internal_url': str(my_internal_url), 'partner_owner_recid': partner_recid, 'locked': '0', 'valide': '1'}, @@ -1041,6 +1050,207 @@ def lock_class(diction): return False, " Impossible de mettre à jour la formation" +''' +Cette fonction publie une formation +elle met la valeur "published" à 1 +''' +def pusblish_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['published'] = '1' + + coll_name = MYSY_GV.dbname['myclass'] + + # seules les formation avec locked = 1 et valide=1 sont 'publiable' + print( "str(my_internal_url) = "+str(my_internal_url)+" --- partner_recid = " + +partner_recid+" mydata = "+str(mydata)) + + + 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 publiée =" + str(nb_doc)) + return True, " La formation " + str(my_internal_url) + "a été publiée" + + else: + mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de publier la formation : " + str(my_internal_url)) + return False, " Impossible de publier la formation : " + str(my_internal_url) + + except Exception as e: + mycommon.myprint(str(inspect.stack()[0][3]) + " - " + str(e)) + return False, " Impossible de publier la formation" + + +''' +Cette fonction Depublie une formation +1 - Elle met "published" à 0 +''' +def unpublish_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])+" - 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 depublier 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['published'] = '0' + + coll_name = MYSY_GV.dbname['myclass'] + + # seules les formation avec locked = 1 et valide=1 sont 'depupliable' + print( " str(my_internal_url) = "+str(my_internal_url)+" partner_recid = "+partner_recid+ + " mydata = "+str(mydata) ) + + 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 depubliée =" + str(nb_doc)) + return True, " La formation " + str(my_internal_url) + "a été depubliée" + + else: + mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de depublier la formation : " + str(my_internal_url)) + return False, " Impossible de depublier la formation : " + str(my_internal_url) + + + + except Exception as e: + mycommon.myprint(str(inspect.stack()[0][3])+" - " +str(e)) + return False, " Impossible de depublier la formation" + + ''' cette fonction recherche et retour une formation. la clé est : l'external code. @@ -1337,11 +1547,11 @@ def get_partner_class(diction): # 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'] + field_list = ['internal_url', 'token', 'title', 'valide', 'locked', 'external_code'] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: - mycommon.myprint( str(inspect.stack()[0][3])+ " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée") + mycommon.myprint( str(inspect.stack()[0][3])+ " - get_partner_class : Le champ '" + val + "' n'existe pas, Creation formation annulée") return False, " Impossible de recuperer la formation" ''' @@ -1359,11 +1569,16 @@ def get_partner_class(diction): mydata = {} my_external_code = "" my_token = "" - + my_internal_url = "" if ("internal_url" in diction.keys()): if diction['internal_url']: - my_internal_code = diction['internal_url'] + my_internal_url = diction['internal_url'] + + + if ("external_code" in diction.keys()): + if diction['external_code']: + my_external_code = diction['external_code'] if ("token" in diction.keys()): if diction['token']: @@ -1373,10 +1588,15 @@ def get_partner_class(diction): Gestion des filters. ''' - external_code_crit = {} + internal_url_crit = {} if ("internal_url" in diction.keys()): if diction['internal_url']: - external_code_crit['internal_url'] = diction['internal_url'] + internal_url_crit['internal_url'] = diction['internal_url'] + + external_code_crit = {} + if ("external_code" in diction.keys()): + if diction['external_code']: + external_code_crit['external_code'] = diction['external_code'] title_crit = {} if ("title" in diction.keys()): @@ -1419,23 +1639,30 @@ def get_partner_class(diction): RetObject = [] filt_external_code= {} - if ("internal_url" in diction.keys()): - filt_external_code = {'internal_url':str(diction['internal_url'])} + if ("external_code" in diction.keys()): + filt_external_code = {'external_code':str(diction['external_code'])} + #print(" GRRRRRRRRRRRRR "+str(filt_external_code)) filt_title = {} if ("title" in diction.keys()): filt_title = {'title': {'$regex': str(diction['title'])}} + filt_internal_url = {} + if ("internal_url" in diction.keys()): + filt_internal_url = {'internal_url': {'$regex': str(diction['internal_url'])}} + print(" filt_internal_url GRRRRRRRRRRRRRRRRRRRRrr "+str(filt_internal_url)) + 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_internal_url = " + str(filt_internal_url) + " 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]}, + filt_external_code, filt_title, filt_internal_url]}, ): #mycommon.myprint(str(retVal)) user = retVal @@ -1567,7 +1794,7 @@ def add_class_mass(file=None, Folder=None, diction=None): ''' field_list = ['external_code', 'title', 'description', 'trainer', 'institut_formation', 'distantiel', 'presentiel', 'price', 'domaine', 'url','duration', 'plus_produit', - 'mots_cle', 'zone_diffusion', 'metier', 'date_lieu'] + 'mots_cle', 'zone_diffusion', 'metier', 'date_lieu', 'published'] total_rows = len(df) @@ -1627,6 +1854,7 @@ def add_class_mass(file=None, Folder=None, diction=None): mydata['distantiel'] = str(df['distantiel'].values[n]) mydata['price'] = str(df['price'].values[n]) mydata['metier'] = str(df['metier'].values[n]) + mydata['published'] = str(df['published'].values[n]) ''' Verification du nombre de mots clée : limite MAX_KEYWORD (3) diff --git a/ela_user_account.py b/ela_user_account.py index 48c0d20..ae77cf7 100644 --- a/ela_user_account.py +++ b/ela_user_account.py @@ -34,12 +34,6 @@ class JSONEncoder(json.JSONEncoder): return json.JSONEncoder.default(self, o) - -CONNECTION_STRING = "mongodb://localhost/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] - - ''' Recuperation des objectifs d'un utilisateur ''' diff --git a/main.py b/main.py index 08797a7..b561ff3 100644 --- a/main.py +++ b/main.py @@ -317,7 +317,6 @@ def unlock_class(): ''' Verrouillage d'une formation - lock training - ''' @app.route('/myclass/api/lock_class/', methods=['POST']) @crossdomain(origin='*') @@ -329,6 +328,31 @@ def lock_class(): return jsonify(status=status, message=retval) +''' +API de Depublication d'une formation +''' +@app.route('/myclass/api/unpublish_class/', methods=['POST']) +@crossdomain(origin='*') +def unpublish_class(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### payload = ",payload) + status, retval = cm.unpublish_class(payload) + return jsonify(status=status, message=retval) + + +''' +API de Publication d'une formation +''' +@app.route('/myclass/api/pusblish_class/', methods=['POST']) +@crossdomain(origin='*') +def pusblish_class(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### payload = ",payload) + status, retval = cm.pusblish_class(payload) + return jsonify(status=status, message=retval) + ''' @@ -346,7 +370,6 @@ def disable_class(): ''' Reactivation d'une formation - enable training - ''' @app.route('/myclass/api/enable_class/', methods=['POST']) @crossdomain(origin='*') @@ -1295,5 +1318,16 @@ def Get_Suggested_Word(): if __name__ == '__main__': print(" debut api") context = SSL.Context(SSL.SSLv23_METHOD) - app.run(host='localhost', port=5000, debug=True, threaded=True) + if (MYSY_GV.MYSY_ENV == "PROD"): + mycommon.myprint(" ++++ ENVIRONNEMENT PRODUCTION ++++ ") + mycommon.myprint("++ DATABASE "+MYSY_GV.CONNECTION_STRING+" ++") + mycommon.myprint("++ DBNAME " + str(MYSY_GV.dbname) + " ++") + app.run(host='localhost', port=MYSY_GV.MYSY_PORT_PROD, debug=True, threaded=True) + elif (MYSY_GV.MYSY_ENV == "DEV"): + mycommon.myprint(" ++++ ENVIRONNEMENT DEVELOPPEMENT ++++") + mycommon.myprint("++ DATABASE " + MYSY_GV.CONNECTION_STRING + " ++") + mycommon.myprint("++ DBNAME " + str(MYSY_GV.dbname) + " ++") + app.run(host='localhost', port=MYSY_GV.MYSY_PORT_DEV, debug=True, threaded=True) + + diff --git a/partners.py b/partners.py index f6e001d..9526e9f 100644 --- a/partners.py +++ b/partners.py @@ -39,10 +39,6 @@ class JSONEncoder(json.JSONEncoder): return json.JSONEncoder.default(self, o) -CONNECTION_STRING = "mongodb://localhost/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] - ''' Ajout d'un partenaire ''' diff --git a/user_session.py b/user_session.py index cd325ef..9de81b0 100644 --- a/user_session.py +++ b/user_session.py @@ -17,9 +17,6 @@ import sys, os import GlobalVariable as MYSY_GV TOKEN_SIZE = 25 -CONNECTION_STRING = "mongodb://localhost/cherifdb" -client = MongoClient(CONNECTION_STRING) -dbname = client['cherifdb'] class JSONEncoder(json.JSONEncoder): def default(self, o):