769 lines
30 KiB
Python
769 lines
30 KiB
Python
'''
|
|
|
|
Cette class gere les avis et articles sur une formation.
|
|
|
|
'''
|
|
from pymongo import MongoClient
|
|
import pymongo
|
|
import hashlib
|
|
import json
|
|
from bson import ObjectId
|
|
from unidecode import unidecode
|
|
|
|
import ela_index_bdd_classes as ela_index
|
|
import logging
|
|
from datetime import datetime
|
|
import prj_common as mycommon
|
|
import secrets
|
|
from pymongo import ReturnDocument
|
|
import inspect
|
|
import sys, os
|
|
import GlobalVariable as MYSY_GV
|
|
import user_message_mgt as umm
|
|
import Ela_Spacy as ES
|
|
import GlobalVariable as MYSY_GV
|
|
import re
|
|
|
|
class JSONEncoder(json.JSONEncoder):
|
|
def default(self, o):
|
|
if isinstance(o, ObjectId):
|
|
return str(o)
|
|
print(self)
|
|
print(o)
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
|
|
'''
|
|
Recuperation des tous avis et commentaires
|
|
'''
|
|
def get_all_articles_avis(diction):
|
|
try:
|
|
|
|
# Dictionnaire des champs utilisables
|
|
field_list = ['token', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
|
|
'user_postal', 'user_latitude', 'user_longitude', 'user_state', 'search_text']
|
|
|
|
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 autorisé")
|
|
return False, " Impossible de recuperer les articles et avis"
|
|
|
|
coll_name = MYSY_GV.dbname['articles_avis']
|
|
|
|
val_tmp = 1
|
|
insertObject = []
|
|
for x in coll_name.find({'valide': '1', 'locked': '0'},
|
|
{ "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, }).sort(
|
|
[ ("date_avis",pymongo.DESCENDING), ("title_formation",pymongo.ASCENDING),]):
|
|
# mycommon.myprint("AVANT ==> "+str(x['description']))
|
|
val = x['qualite']
|
|
if (len(x['qualite']) > MYSY_GV.MAX_CARACT):
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT]
|
|
|
|
val = x['avis']
|
|
CLEANR = re.compile('<.*?>')
|
|
|
|
clean_avis = re.sub(CLEANR, '', str(val))
|
|
clean_avis = str(clean_avis).replace(" ", " ");
|
|
|
|
#print(" ### CLEANRCLEANR = "+str(clean_avis))
|
|
|
|
if (len(clean_avis) > MYSY_GV.MAX_CARACT):
|
|
x['avis'] = clean_avis[:MYSY_GV.MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['avis'] = str(clean_avis)
|
|
|
|
# mycommon.myprint("APRES ==> " + str(x['description']))
|
|
|
|
user = x
|
|
user['id'] = str(val_tmp)
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
val_tmp = val_tmp + 1
|
|
|
|
#print(" getObject = ", str(insertObject))
|
|
return True, insertObject
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recuperer les articles et avis"
|
|
|
|
|
|
'''
|
|
Cette API recupere et retourne un articles / avis
|
|
'''
|
|
def get_articles_avis(diction):
|
|
try:
|
|
|
|
# Dictionnaire des champs utilisables
|
|
field_list = ['internal_url', 'token', 'user_ip', 'user_country_code', 'user_country_name',
|
|
'user_city','user_postal', 'user_latitude', 'user_longitude', 'user_state']
|
|
|
|
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 autorisé")
|
|
return False, " Impossible de recuperer l'articles/avis"
|
|
|
|
'''
|
|
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 recuperer l'articles/avis"
|
|
|
|
|
|
# Recuperation des parametres
|
|
mydata = {}
|
|
if ("user_ip" in diction.keys()):
|
|
if diction['user_ip']:
|
|
mydata['user_ip'] = diction['user_ip']
|
|
|
|
if ("user_country_code" in diction.keys()):
|
|
if diction['user_country_code']:
|
|
mydata['user_country_code'] = diction['user_country_code']
|
|
|
|
if ("user_country_name" in diction.keys()):
|
|
if diction['user_country_name']:
|
|
mydata['user_country_name'] = diction['user_country_name']
|
|
|
|
if ("user_city" in diction.keys()):
|
|
if diction['user_city']:
|
|
mydata['user_city'] = diction['user_city']
|
|
|
|
if ("user_postal" in diction.keys()):
|
|
if diction['user_postal']:
|
|
mydata['user_postal'] = diction['user_postal']
|
|
|
|
if ("user_latitude" in diction.keys()):
|
|
if diction['user_latitude']:
|
|
mydata['user_latitude'] = diction['user_latitude']
|
|
|
|
if ("user_longitude" in diction.keys()):
|
|
if diction['user_longitude']:
|
|
mydata['user_longitude'] = diction['user_longitude']
|
|
|
|
if ("user_state" in diction.keys()):
|
|
if diction['user_state']:
|
|
mydata['user_state'] = diction['user_state']
|
|
|
|
my_title_formation = ''
|
|
if ("title_formation" in diction.keys()):
|
|
if diction['title_formation']:
|
|
mydata['title_formation'] = diction['title_formation']
|
|
my_title_formation = diction['title_formation']
|
|
|
|
my_internal_id = ''
|
|
if ("internal_url" in diction.keys()):
|
|
if diction['internal_url']:
|
|
my_internal_id = diction['internal_url']
|
|
|
|
|
|
#print(" My Data =" + str(mydata))
|
|
|
|
coll_name = MYSY_GV.dbname['articles_avis']
|
|
insertObject = []
|
|
|
|
for x in coll_name.find({'valide': '1', 'locked': '0', 'internal_url':my_internal_id},
|
|
{ "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, }).sort([("title_formation",pymongo.ASCENDING),
|
|
("date_avis",pymongo.ASCENDING),
|
|
("note", pymongo.DESCENDING),
|
|
]) :
|
|
|
|
user = x
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
|
|
|
|
#print(" getObject = ", str(insertObject))
|
|
return True, insertObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recuperer l'article/avis"
|
|
|
|
|
|
'''
|
|
Cette fonction crée un articles avis, avec les element de traàabilité :
|
|
- user_recid,
|
|
- IP,
|
|
- etc
|
|
|
|
Une fois que l'avis est bien ajouté, une notification est envoyer dans la collection
|
|
'user_message' à destination de l'adresse de contact du site.
|
|
'''
|
|
|
|
def add_articles_avis(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 = ['token', 'title_formation', 'user_ip', 'user_country_code', 'user_country_name',
|
|
'user_city', 'user_postal', 'user_latitude', 'user_longitude', 'user_state',
|
|
'type_formation', 'qualite', 'avis', 'note', 'qualityrating','avisrating',
|
|
'comment', 'postedby', 'url_formation', 'user_recid']
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Creation formation : Le champ '" + val + "' n'est pas autorisé, Creation formation annulée")
|
|
return False, " Verifier votre API"
|
|
|
|
'''
|
|
Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['title_formation', 'url_formation', 'token', 'avis', 'avis', 'user_ip',
|
|
'qualite', 'note']
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " Verifier les parametres de votre API"
|
|
|
|
'''
|
|
Verification si le token et l'email sont valident
|
|
'''
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
|
|
my_user_recid = 'None'
|
|
my_token = 'None'
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
# Verification de la validité du token
|
|
'''
|
|
Si un token est fourni, c'est que la personne est user connecté. on doit donc verifier la validé du token
|
|
si le token est vide, alors c'est un user en mode non connecté
|
|
|
|
'''
|
|
retval = mycommon.check_token_validity("", str(diction['token']))
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
|
|
return False, "L'email ou le token ne sont pas valident"
|
|
|
|
# Recuperation du recid du partenaire
|
|
user_recid = mycommon.get_user_recid_from_token(my_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"
|
|
|
|
my_user_recid = user_recid
|
|
|
|
|
|
mydata['user_recid'] = my_user_recid
|
|
mydata['token'] = my_token
|
|
|
|
if ("title_formation" in diction.keys()):
|
|
if diction['title_formation']:
|
|
mydata['title_formation'] = diction['title_formation']
|
|
'''
|
|
Gestion de l'url interne. Pr un meilleur SEO, le champ 'internal_url' = titre avec des tiret
|
|
'''
|
|
my_internal_url = str(diction['title_formation']).replace("-","").replace(",", "").replace(" ", "").strip()
|
|
|
|
local_status, my_internal_url = ES.Ela_Remove_Ponct_Special_Caractere(my_internal_url)
|
|
my_internal_url = unidecode(my_internal_url.lower())
|
|
if (my_internal_url.startswith('-')):
|
|
my_internal_url = my_internal_url[1:]
|
|
|
|
if (my_internal_url.endswith('-')):
|
|
my_internal_url = my_internal_url[:-1]
|
|
|
|
my_internal_url = my_internal_url.replace(" ","-")
|
|
suffix = hashlib.md5(my_internal_url.encode()).hexdigest()
|
|
my_internal_url = str(my_internal_url)+"-"+str(suffix[-3:])
|
|
my_internal_url.replace("---", "-").replace("--", "-")
|
|
|
|
mydata['internal_url'] = my_internal_url
|
|
|
|
|
|
if ("user_ip" in diction.keys()):
|
|
if diction['user_ip']:
|
|
mydata['user_ip'] = diction['user_ip']
|
|
|
|
if ("user_country_code" in diction.keys()):
|
|
if diction['user_country_code']:
|
|
mydata['user_country_code'] = diction['user_country_code']
|
|
|
|
if ("user_country_name" in diction.keys()):
|
|
if diction['user_country_name']:
|
|
mydata['user_country_name'] = diction['user_country_name']
|
|
|
|
if ("user_city" in diction.keys()):
|
|
if diction['user_city']:
|
|
mydata['user_city'] = diction['user_city']
|
|
|
|
if ("user_postal" in diction.keys()):
|
|
if diction['user_postal']:
|
|
mydata['user_postal'] = diction['user_postal']
|
|
|
|
if ("user_latitude" in diction.keys()):
|
|
if diction['user_latitude']:
|
|
mydata['user_latitude'] = diction['user_latitude']
|
|
|
|
if ("user_longitude" in diction.keys()):
|
|
if diction['user_longitude']:
|
|
mydata['user_longitude'] = diction['user_longitude']
|
|
|
|
if ("user_state" in diction.keys()):
|
|
if diction['user_state']:
|
|
mydata['user_state'] = diction['user_state']
|
|
|
|
if ("type_formation" in diction.keys()):
|
|
if diction['type_formation']:
|
|
mydata['type_formation'] = diction['type_formation']
|
|
|
|
if ("qualite" in diction.keys()):
|
|
if diction['qualite']:
|
|
mydata['qualite'] = diction['qualite']
|
|
|
|
if ("avis" in diction.keys()):
|
|
if diction['avis']:
|
|
mydata['avis'] = diction['avis']
|
|
|
|
if ("note" in diction.keys()):
|
|
if diction['note']:
|
|
mydata['note'] = diction['note']
|
|
|
|
if ("comment" in diction.keys()):
|
|
if diction['comment']:
|
|
mydata['comment'] = diction['comment']
|
|
|
|
if ("postedby" in diction.keys()):
|
|
if diction['postedby']:
|
|
mydata['postedby'] = diction['postedby']
|
|
|
|
if ("url_formation" in diction.keys()):
|
|
if diction['url_formation']:
|
|
mydata['url_formation'] = diction['url_formation']
|
|
|
|
if ("avisrating" in diction.keys()):
|
|
if diction['avisrating']:
|
|
mydata['avisrating'] = diction['avisrating']
|
|
|
|
if ("qualityrating" in diction.keys()):
|
|
if diction['qualityrating']:
|
|
mydata['qualityrating'] = diction['qualityrating']
|
|
|
|
mydata['valide'] = '0'
|
|
mydata['locked'] = '1'
|
|
mydata['indexed'] = '0'
|
|
mydata['indexed'] = '0'
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['date_avis'] = str(datetime.now())
|
|
mydata['isalaune'] = "0"
|
|
|
|
#print(mydata)
|
|
|
|
coll_name = MYSY_GV.dbname['articles_avis']
|
|
ret_val = coll_name.insert_one(mydata)
|
|
|
|
if (ret_val and ret_val.inserted_id):
|
|
nb_doc = str(ret_val.inserted_id)
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - L'article/avis a été bien ajouté =" + str(nb_doc))
|
|
|
|
'''
|
|
Vu que l'ajout a bien ete fait, on declenche l'envoie de l'email (enregistrement
|
|
dans la collection "user_message". c'est un cron qui enverra l'email plus tard.
|
|
'''
|
|
notif_dict = {}
|
|
|
|
notif_dict['type'] = 'email'
|
|
notif_dict['sender_mail']= MYSY_GV.INTERNAL_MAIL_SENDER
|
|
notif_dict['sender_tel'] = MYSY_GV.INTERNAL_TEL_SENDER
|
|
notif_dict['recever_mail'] = MYSY_GV.INTERNAL_MAIL_RECEVER
|
|
notif_dict['recever_tel'] = MYSY_GV.INTERNAL_TEL_RECEVER
|
|
notif_dict['object'] = 'Nouvel article par utilisateur : '+str(ret_val.inserted_id)
|
|
notif_dict['message'] = 'Merci de verifier et valider article _id='+str(ret_val.inserted_id)
|
|
notif_dict['token'] = ''
|
|
notif_dict['user_ip'] = ''
|
|
notif_dict['user_country_code'] = ''
|
|
notif_dict['user_country_name'] = ''
|
|
notif_dict['user_city'] = ''
|
|
notif_dict['user_postal'] = ''
|
|
notif_dict['user_latitude'] = ''
|
|
notif_dict['user_longitude'] = ''
|
|
notif_dict['user_state'] = ''
|
|
notif_dict['sender_name'] = 'systeme automatique'
|
|
|
|
|
|
umm.add_user_message(notif_dict)
|
|
|
|
return True, " L'article/avis a été bien ajouté"
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible d'ajouter L'article/avis. "+str(mydata) )
|
|
return False, "Impossible d'ajouter L'article/avis "
|
|
|
|
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 l'article/avis"
|
|
|
|
|
|
'''
|
|
Cette fonction recherche un aricles ou avis
|
|
'''
|
|
def recherche_articles_avis(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 = ['_id', 'token', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
|
|
'user_postal', 'user_latitude', 'user_longitude', 'user_state', 'search_text']
|
|
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, recherche annuléee")
|
|
return False, " Le champ '" + val + "' n'existe pas, recherche annulée"
|
|
|
|
'''
|
|
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 = ['search_text', '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
|
|
search_text = ""
|
|
user_recid = ""
|
|
token = ""
|
|
if ("search_text" in diction.keys()):
|
|
if diction['search_text']:
|
|
search_text = diction['search_text']
|
|
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
my_internal_id = ''
|
|
if ("_id" in diction.keys()):
|
|
if diction['_id']:
|
|
my_internal_id = diction['_id']
|
|
|
|
# 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)
|
|
|
|
if retval is False:
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide")
|
|
return False, "Le token 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 recuperer le token de l'utilisateur")
|
|
return False, " Impossible de desactiver l'objectif utilisateur"
|
|
|
|
#mycommon.myprint(" On recehrche la phrase +'" + search_text + "' + user_recid = " + user_recid)
|
|
# Enregistrerment de la recherche
|
|
retval, message, store_recherche_Id = store_recherche_article_avis(diction, user_recid)
|
|
if (retval is False):
|
|
return retval, message
|
|
|
|
tab_training = []
|
|
tab_training = ela_index.ela_recherche_article_avis_tokens(search_text)
|
|
'''
|
|
Pour analyser la recherche, decommenter les 2 lignes ci-dessous
|
|
'''
|
|
#mycommon.myprint(" pour phrase : #" + search_text + "#, voici la liste des formations")
|
|
#mycommon.myprint(tab_training)
|
|
|
|
coll_name = MYSY_GV.dbname['articles_avis']
|
|
|
|
'''
|
|
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 val in tab_training:
|
|
|
|
for x in coll_name.find({"_id": ObjectId(my_internal_id)},
|
|
{ "indexed": 0, "valide": 0, "locked": 0, }):
|
|
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())
|
|
mydict_combined['type_view'] = "summary"
|
|
|
|
#if ("_id" in mydict_combined.keys()):
|
|
# mydict_combined['class_id'] = mydict_combined.pop('_id')
|
|
|
|
user = x
|
|
|
|
val = x['qualite']
|
|
if (len(x['qualite']) > MYSY_GV.MAX_CARACT):
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT]
|
|
|
|
val = x['avis']
|
|
CLEANR = re.compile('<.*?>')
|
|
clean_avis = re.sub(CLEANR, '', str(val))
|
|
|
|
#print(" ### CLEANRCLEANR = "+str(CLEANR))
|
|
if (len(clean_avis) > MYSY_GV.MAX_CARACT):
|
|
x['avis'] = clean_avis[:MYSY_GV.MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['avis'] = str(clean_avis)
|
|
|
|
print(user)
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
|
|
return True, insertObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recherche un article/avis"
|
|
|
|
'''
|
|
Stockage des recherches sur les avis
|
|
'''
|
|
def store_recherche_article_avis(diction, user_recid=""):
|
|
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.
|
|
'''
|
|
|
|
if ( len(str(user_recid)) <= 0 ):
|
|
user_recid = 'None' # by default
|
|
|
|
|
|
|
|
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', 'distance', 'duration']
|
|
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, 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,
|
|
On controle que les champs obligatoires sont presents dans la liste
|
|
'''
|
|
field_list_obligatoire = ['search_text', 'token']
|
|
|
|
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 ", None
|
|
|
|
|
|
# recuperation des paramettre
|
|
mydata = {}
|
|
mydata_id = ""
|
|
my_token = ""
|
|
|
|
if ("search_text" in diction.keys()):
|
|
if diction['search_text']:
|
|
mydata['search_text'] = diction['search_text']
|
|
|
|
|
|
if ("user_mail" in diction.keys()):
|
|
if diction['user_recid']:
|
|
mydata['user_recid'] = diction['user_recid']
|
|
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
my_token = diction['token']
|
|
|
|
'''# Recuperation du recid de l'utilisateur
|
|
user_recid = mycommon.get_user_recid_from_token(my_token)
|
|
if user_recid is False:
|
|
mycommon.myprint(" Impossible d'enregistrer la recherche de utilisateur")
|
|
return False, " Impossible d'enregistrer la recherche de utilisateur"
|
|
'''
|
|
mydata['user_recid'] = user_recid
|
|
mydata['document_recherche'] = "articles_avis"
|
|
|
|
if ("source" in diction.keys()):
|
|
if diction['source']:
|
|
mydata['source'] = diction['source']
|
|
|
|
if ("name" in diction.keys()):
|
|
if diction['name']:
|
|
mydata['name'] = diction['name']
|
|
|
|
|
|
if ("id" in diction.keys()):
|
|
if diction['id']:
|
|
mydata_id = diction['id']
|
|
|
|
mydata['date_update'] = str(datetime.now())
|
|
|
|
mydata['valide'] = "1" # By default
|
|
if ("valide" in diction.keys()):
|
|
if diction['valide']:
|
|
mydata['valide'] = diction['valide']
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['user_recherche']
|
|
|
|
# Si le champ "id" est renseigné, il s'agit d'une mis jour
|
|
#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'},
|
|
{"$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 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", 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(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:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + "Impossible d'enregistrer la recherche")
|
|
return False, "Impossible d'enregistrer la recherche ", None
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, "Impossible d'enregistrer la recherche", None
|
|
|
|
|
|
|
|
''''
|
|
Cette fonction retourne la liste
|
|
des articles/avis à la une'''
|
|
def get_article_avis_alaune(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 = ['_id', 'token', 'user_ip', 'user_country_code', 'user_country_name', 'user_city',
|
|
'user_postal', 'user_latitude', 'user_longitude', 'user_state']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas, recherche annulée")
|
|
return False, " Le champ '" + val + "' n'existe pas, recherche annulée"
|
|
|
|
'''
|
|
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(" : La valeur '" + val + "' n'est pas presente dans liste ")
|
|
return False, " : La valeur '" + val + "' n'est pas presente dans liste ",
|
|
|
|
|
|
coll_name = MYSY_GV.dbname['articles_avis']
|
|
insertObject = []
|
|
|
|
for x in coll_name.find({'valide': '1', 'locked': '0', 'isalaune': '1'},
|
|
{"indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "isalaune": 0,
|
|
"valide": 0, "locked": 0, }).sort(
|
|
[ ("date_avis", pymongo.DESCENDING), ("note", pymongo.DESCENDING)]):
|
|
|
|
val = x['qualite']
|
|
if (len(x['qualite']) > MYSY_GV.MAX_CARACT_DEDUIT):
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT_DEDUIT] + " ..."
|
|
|
|
else:
|
|
x['qualite'] = val[:MYSY_GV.MAX_CARACT_DEDUIT]
|
|
|
|
val = x['avis']
|
|
if (len(x['avis']) > MYSY_GV.MAX_CARACT_DEDUIT):
|
|
x['avis'] = val[:MYSY_GV.MAX_CARACT_DEDUIT] + " ..."
|
|
|
|
else:
|
|
x['avis'] = val[:MYSY_GV.MAX_CARACT_DEDUIT]
|
|
|
|
user = x
|
|
insertObject.append(JSONEncoder().encode(user))
|
|
|
|
#print(" getObject = ", str(insertObject))
|
|
return True, insertObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, "Impossible de recuperer les articles à la Une" |