376 lines
14 KiB
Python
376 lines
14 KiB
Python
'''
|
|
|
|
Cette class gere les avis et articles sur 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 logging
|
|
from datetime import datetime
|
|
import prj_common as mycommon
|
|
import secrets
|
|
from pymongo import ReturnDocument
|
|
import inspect
|
|
import sys, os
|
|
import csv
|
|
import pandas as pd
|
|
|
|
|
|
|
|
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)
|
|
|
|
CONNECTION_STRING = "mongodb://localhost/cherifdb"
|
|
client = MongoClient(CONNECTION_STRING)
|
|
dbname = client['cherifdb']
|
|
|
|
'''
|
|
La taille maximal d'un champs à retourner.
|
|
Au dela de 300 caractère, le système rame
|
|
'''
|
|
MAX_CARACT = 300
|
|
|
|
|
|
|
|
'''
|
|
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 = dbname['articles_avis']
|
|
|
|
val_tmp = 1
|
|
insertObject = []
|
|
for x in coll_name.find({'valide': '1', 'locked': '0'},
|
|
{"_id": 0, "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, "url_formation": 0, }).sort(
|
|
[("title_formation",pymongo.ASCENDING), ("date_avis",pymongo.ASCENDING)]):
|
|
# mycommon.myprint("AVANT ==> "+str(x['description']))
|
|
val = x['qualite']
|
|
if (len(x['qualite']) > MAX_CARACT):
|
|
x['qualite'] = val[:MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['qualite'] = val[:MAX_CARACT]
|
|
|
|
val = x['avis']
|
|
if (len(x['avis']) > MAX_CARACT):
|
|
x['avis'] = val[:MAX_CARACT] + " ..."
|
|
|
|
else:
|
|
x['avis'] = val[:MAX_CARACT]
|
|
|
|
# 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) + " - 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 = ['token', 'title_formation', '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 = ['token', 'title_formation']
|
|
|
|
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']
|
|
|
|
|
|
print(" My Data =" + str(mydata))
|
|
|
|
coll_name = dbname['articles_avis']
|
|
insertObject = []
|
|
|
|
for x in coll_name.find({'valide': '1', 'locked': '0', 'title_formation':my_title_formation},
|
|
{"_id": 0, "indexed": 0, "indexed_desc": 0, "indexed_obj": 0, "indexed_title": 0,
|
|
"valide": 0, "locked": 0, "url_formation": 0, }).sort([("title_formation",pymongo.ASCENDING), ("date_avis",pymongo.ASCENDING)]) :
|
|
|
|
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) + " - 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
|
|
'''
|
|
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',
|
|
'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
|
|
|
|
print(" ########### 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']
|
|
|
|
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']
|
|
|
|
mydata['valide'] = '0'
|
|
mydata['locked'] = '1'
|
|
mydata['indexed'] = '0'
|
|
mydata['date_update'] = str(datetime.now())
|
|
mydata['date_avis'] = str(datetime.now())
|
|
|
|
print(mydata)
|
|
|
|
coll_name = 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))
|
|
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) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de créer l'article/avis" |