diff --git a/articles_avis.py b/articles_avis.py index 25e9665..110c7dd 100644 --- a/articles_avis.py +++ b/articles_avis.py @@ -74,7 +74,8 @@ def get_all_articles_avis(diction): 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, }): + "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): @@ -190,17 +191,186 @@ def get_articles_avis(diction): 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, }): + "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)) + #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" \ No newline at end of file + 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" \ No newline at end of file diff --git a/class_mgt.py b/class_mgt.py index 8db42f8..d1917b7 100644 --- a/class_mgt.py +++ b/class_mgt.py @@ -84,13 +84,13 @@ def add_class(diction): 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") + 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_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") + mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le recid du partenaire") return False, " Les informations d'identification sont incorrectes" mydata['partner_owner_recid'] = user_recid diff --git a/main.py b/main.py index 2f31fbf..6052a41 100644 --- a/main.py +++ b/main.py @@ -865,6 +865,17 @@ def get_articles_avis(): status, retval = aa.get_articles_avis(payload) return jsonify(status=status, message=retval) +''' +Cette API ajouter un article/avis +''' +@app.route('/myclass/api/add_articles_avis/', methods=['POST','GET']) +@crossdomain(origin='*') +def add_articles_avis(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### payload = ",payload) + status, retval = aa.add_articles_avis(payload) + return jsonify(status=status, message=retval) if __name__ == '__main__':