update 01/03/2022 - 14h30

master
cbalde 2022-03-01 14:40:41 +01:00 committed by cherif
parent d79a649c50
commit 488cb7d7ef
2 changed files with 1833 additions and 24 deletions

File diff suppressed because it is too large Load Diff

View File

@ -15,7 +15,7 @@ import prj_common as mycommon
import secrets
from pymongo import ReturnDocument
import inspect
import os
import sys, os
import csv
import pandas as pd
@ -198,7 +198,9 @@ def recherche_text_simple(diction):
# Ici on doit mettre tous les champs possible (obligatoire ou non) de la BDD dans la liste
# field_list.
'''
field_list = ['search_text', 'token', 'certif', 'support', 'type', 'lang','price']
field_list = ['token', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
'user_postal', 'user_latitude', 'user_longitude', 'user_state', 'search_text', 'certif',
'support', 'type', 'lang', 'price']
incom_keys = diction.keys()
for val in incom_keys:
if val not in field_list:
@ -286,6 +288,8 @@ def recherche_text_simple(diction):
'''
user_recid = "None"
coll_search_result = dbname['user_recherche_result']
# Verification de la validité du token/mail dans le cas des user en mode connecté
if ( len(str(token)) > 0 ):
retval = mycommon.check_token_validity("", token)
@ -304,7 +308,7 @@ def recherche_text_simple(diction):
mycommon.myprint(" On recehrche la phrase +'" + search_text + "'")
# Enregistrerment de la recherche
retval, message = store_recherche(diction, user_recid)
retval, message, store_recherche_Id = store_recherche(diction, user_recid)
if (retval is False):
return retval, message
@ -345,11 +349,11 @@ def recherche_text_simple(diction):
final_message2 = []
for t in final_message:
for y in t:
print( " unique = "+str(y))
final_message2.append(y)
if( t is not False):
for y in t:
print( " unique = "+str(y))
final_message2.append(y)
print(" YY = "+str(final_message2))
coll_name = dbname['myclass']
@ -357,15 +361,79 @@ def recherche_text_simple(diction):
print(" Dans la recherch TIPS , les critère = " + str(certif_crit) + " la langue = " + str(
lang_crit) + " price = " + str(price_crit)+" support = "+str(support_crit))
'''
A present mise à jour de la table "user_recherche" avec les resultats trouvés
'''
#store_recherche_Id
find_result = {'find_result':str(final_message2)}
tab_user_recherche = dbname['user_recherche']
# seules les formation avec locked = 0 et valide=1 sont modifiables
print(str(inspect.stack()[0][3]) + " ENREG DES RESULT :"+str(store_recherche_Id)+" --- "+str(final_message2))
ret_val_user_rech = coll_name.find_one_and_update(
{'_id': ObjectId(store_recherche_Id)},
{"$set": find_result},
return_document=ReturnDocument.AFTER
)
if (ret_val_user_rech is False ):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
" d'une recherche de formation. Voir _id = "+ str( store_recherche_Id))
return False, "La recheche est impossible "
'''
la valeur nb_result permettra de savoir si la requete a donnée un resultat.
si ce n'est pas le cas, il faudra à la fin enregistrer la requete avec un result a vide'''
nb_result = 0
for x in coll_name.find(({"$and": [{"external_code": {"$in": final_message2}},
certif_crit, lang_crit, type_crit, price_crit, support_crit]})):
mycommon.myprint(x)
nb_result = nb_result +1
my_recid = {}
my_recid['user_rec_id'] = str(user_recid)
mydict_combined = {**diction, **x, **my_recid}
mydict_combined['date_update'] = str(datetime.now())
if ("_id" in mydict_combined.keys()):
mydict_combined['class_id'] = mydict_combined.pop('_id')
mycommon.myprint("COMBINED = " + str(mydict_combined))
'''
Statistique : Insertion du recherche - resultat '''
ret_val_tmp = coll_search_result.insert_one(mydict_combined)
if (ret_val_tmp is False):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
" d'une recherche de formation. Voir _id = ")
return False, "La recheche est impossible "
user = x
insertObject.append(JSONEncoder().encode(user))
# mycommon.myprint(" insertObject = ", insertObject)
''' en cas de resultat vide, enregsitrement de la requete de recherche avec les filtres associé'''
if( nb_result == 0):
my_recid = {}
my_recid['user_rec_id'] = str(user_recid)
mydict_combined = {**diction, **my_recid}
mydict_combined['date_update'] = str(datetime.now())
'''
Statistique : Insertion du recherche - resultat '''
ret_val_tmp = coll_search_result.insert_one(mydict_combined)
if (ret_val_tmp is False):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
" d'une recherche de formation. Voir _id = ")
return False, "La recheche est impossible "
return True, insertObject
# Fin de la recherche par tips.
tab_training = []
@ -378,14 +446,52 @@ def recherche_text_simple(diction):
print(" Dans la recherch, les critère = "+str(certif_crit)+" la langue = "+str(lang_crit)+" price = "+str(price_crit))
#ici ajouter tous les critère comme ci-dessous avec le support et le certif
'''
la valeur nb_result permettra de savoir si la requete a donnée un resultat.
si ce n'est pas le cas, il faudra à la fin enregistrer la requete avec un result a vide'''
nb_result = 0
insertObject = []
for x in coll_name.find(({"$and": [{"external_code": {"$in": tab_training}},
certif_crit,lang_crit, type_crit,price_crit, support_crit ]})):
mycommon.myprint(x)
nb_result = nb_result + 1
my_recid = {}
my_recid['user_rec_id'] = str(user_recid)
mydict_combined = {**diction, **x, **my_recid}
mydict_combined['date_update'] = str(datetime.now())
if ("_id" in mydict_combined.keys()):
mydict_combined['class_id'] = mydict_combined.pop('_id')
mycommon.myprint("COMBINED = " + str(mydict_combined))
'''
Statistique : Insertion du recherche - resultat '''
ret_val_tmp = coll_search_result.insert_one(mydict_combined)
if (ret_val_tmp is False):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
" d'une recherche de formation. Voir _id = ")
return False, "La recheche est impossible "
user = x
insertObject.append(JSONEncoder().encode(user))
# mycommon.myprint(" insertObject = ", insertObject)
''' en cas de resultat vide, enregsitrement de la requete de recherche avec les filtres associé'''
if (nb_result == 0):
my_recid = {}
my_recid['user_rec_id'] = str(user_recid)
mydict_combined = {**diction, **my_recid}
mydict_combined['date_update'] = str(datetime.now())
'''
Statistique : Insertion du recherche - resultat '''
ret_val_tmp = coll_search_result.insert_one(mydict_combined)
if (ret_val_tmp is False):
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de mettre à jour la table des resultat"
" d'une recherche de formation. Voir _id = ")
return False, "La recheche est impossible "
return True, insertObject
except Exception as e:
@ -434,14 +540,19 @@ def store_recherche(diction, user_recid=""):
if ( len(str(user_recid)) <= 0 ):
user_recid = 'None' # by default
'''
field_list = ['search_text', 'token','id','type', 'lang', 'level', 'dist',
'price', 'certif', 'source', 'name','valide','certif', 'support','type']
'''
field_list = ['token', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
'user_postal', 'user_latitude', 'user_longitude', 'user_state', 'search_text', 'certif',
'support', 'type', 'lang', 'price']
incom_keys = diction.keys()
for val in incom_keys:
if val not in field_list:
mycommon.myprint(" Le champ '" + val + "' n'existe pas, recherche annulée")
return False, " Le champ '" + val + "' n'existe pas, recherche annulée"
mycommon.myprint(str(inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas, recherche annulée")
return False, " Le champ '" + val + "' n'existe pas, recherche annulée", None
'''
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
@ -452,7 +563,7 @@ def store_recherche(diction, user_recid=""):
for val in field_list_obligatoire:
if val not in diction:
mycommon.myprint(" : La valeur '" + val + "' n'est pas presente dans liste ")
return False, " : La valeur '" + val + "' n'est pas presente dans liste "
return False, " : La valeur '" + val + "' n'est pas presente dans liste ", None
# recuperation des paramettre
@ -529,7 +640,7 @@ def store_recherche(diction, user_recid=""):
coll_name = dbname['user_recherche']
# Si le champ "id" est renseigné, il s'agit d'une mis jour
mycommon.myprint("on va stocker "+str(mydata)+" id = "+str(mydata_id))
mycommon.myprint(str(inspect.stack()[0][3]) + " on va stocker "+str(mydata)+" id = "+str(mydata_id))
if( len(str(mydata_id)) > 0 ):
ret_val = coll_name.find_one_and_update({'_id': ObjectId(str(mydata_id)), 'valide': '1'},
@ -539,26 +650,29 @@ def store_recherche(diction, user_recid=""):
if (ret_val and ret_val['_id']):
nb_doc = str(ret_val['_id'])
mycommon.myprint("La recherche a bien été mise à jour . Le Doc Id = "+str(nb_doc))
return True, "La recherche a bien été mise à jour"
mycommon.myprint(str(inspect.stack()[0][3]) + " La recherche a bien été mise à jour . Le Doc Id = "+str(nb_doc))
return True, "La recherche a bien été mise à jour", ret_val['_id']
else:
return False, "Impossible de mettre à jour a la recherche"
return False, "Impossible de mettre à jour a la recherche", None
else:
ret_val = coll_name.insert_one(mydata)
if ret_val and ret_val.inserted_id:
nb_doc = ret_val.inserted_id
mycommon.myprint("La recherche a été bien enregistrée. Id = '" + str(nb_doc)+"' ")
return True, "La recherche a bien été mise à jour"
mycommon.myprint(str(inspect.stack()[0][3]) + " La recherche a été bien enregistrée. Id = '" + str(nb_doc)+"' ")
return True, "La recherche a bien été mise à jour", nb_doc
else:
return False, "Impossible d'enregistrer la recherche"
mycommon.myprint(
str(inspect.stack()[0][3]) + "Impossible d'enregistrer la recherche")
return False, "Impossible d'enregistrer la recherche ", None
except Exception as e:
mycommon.myprint(e)
return False, "Impossible d'enregistrer la recherche"
exc_type, exc_obj, exc_tb = sys.exc_info()
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
return False, "Impossible d'enregistrer la recherche", None
@ -618,7 +732,7 @@ def recherche_tips_ret_ref(diction):
print(" ####### recherche tips, voici mon new diction"+str(diction))
# Enregistrerment de la recherche
retval, message = store_recherche(diction, user_recid)
retval, message, store_recherche_Id = store_recherche(diction, user_recid)
if( retval is False):
return retval, message
@ -750,7 +864,7 @@ def recherche_tips(diction):
print(" ####### recherche tips, voici mon new diction"+str(diction))
# Enregistrerment de la recherche
retval, message = store_recherche(diction, user_recid)
retval, message, store_recherche_Id = store_recherche(diction, user_recid)
if( retval is False):
return retval, message