From 2df7a73f07307cd3437a11e3c47edcd80774a52a Mon Sep 17 00:00:00 2001 From: Eric F Date: Tue, 7 Dec 2021 18:56:15 +0100 Subject: [PATCH] essai sse --- testFlask/curl.cmd2 | 1 + testFlask/mdm.py | 52 +++++++--- testFlask/templates/commandes_2.html | 139 +++++++++++++++++++-------- 3 files changed, 135 insertions(+), 57 deletions(-) create mode 100644 testFlask/curl.cmd2 diff --git a/testFlask/curl.cmd2 b/testFlask/curl.cmd2 new file mode 100644 index 00000000..b74108e4 --- /dev/null +++ b/testFlask/curl.cmd2 @@ -0,0 +1 @@ +curl http://127.0.0.1:8123/hello diff --git a/testFlask/mdm.py b/testFlask/mdm.py index 45948332..ba723848 100755 --- a/testFlask/mdm.py +++ b/testFlask/mdm.py @@ -4,12 +4,25 @@ sys.path.append('/home/eric/FLASK/eficas.eecj.git/testFlask') from connectEficas import accasConnecteur code='Essai' -from flask import Flask, request, render_template, url_for, jsonify, make_response +from flask import Flask, request, render_template, url_for, jsonify, make_response, session, g, Response # from flask import Flask, request, render_template, url_for, json, jsonify import json import pprint -from forms import BasicForm -from collections import OrderedDict +from forms import BasicForm +from collections import OrderedDict +from markupsafe import escape + +from flask_sse import sse + +# For example, you may want to override how request parameters are handled to preserve their order: +# from flask import Flask, Request +# from werkzeug.datastructures import ImmutableOrderedMultiDict +# class MyRequest(Request): +# """Request subclass to override request parameter storage""" +# parameter_storage_class = ImmutableOrderedMultiDict +# class MyFlask(Flask): +# """Flask subclass using the custom request class""" +# request_class = MyReq def createConnecteur(): monConnecteur=accasConnecteur(code, langue='ang') @@ -19,6 +32,16 @@ monConnecteur=createConnecteur() print (monConnecteur.getListeCommandes()) app = Flask(__name__) +#Server Side Event config +app.config["REDIS_URL"] = "redis://localhost" +#app.config["REDIS_URL"] = "redis://:password@localhost" +#TODO: personaliser l'url en fonction de la session utilisateur +app.register_blueprint(sse, url_prefix='/stream') + +@app.route('/update') +def publish_update(): + sse.publish({"message": "Hello!"}, type='update') + return "Message sent!" @app.route('/') def index(): @@ -152,26 +175,25 @@ def updateSimp(): # Print the dictionary print(req) print(req['id']) - id,data = req.values() - data = str(data) #Oups PN - #comments,changeIsAccepted = monConnecteur.changeValeur(id,data); - changeDone = monConnecteur.changeValeur(id,data); - changeDone = False + id=req['id'];value=req['value'] + # id, value = req.values() # Dangereux correspondance implicite + value = str(value) #Oups PN + changeDone = monConnecteur.changeValeur(id,value); + changeDone = True print ("changeDone : ",changeDone) - # Ne pas récupérer le noeud dans le cas du SIMP (le changeDone et l''ancienne valeur ds la WebApp suffit + # Ne pas récupérer et ne pas renvoyer le noeud dans le cas du SIMP + # (le changeDone et l''ancienne valeur ds la WebApp suffit node = monConnecteur.getDicoForFancy(monConnecteur.monEditeur.getNodeById(id)) print("node :",node) - # myTreeDico=monConnecteur.getDicoObjetsCompletsPourTree(req['id']) - # print("myTreeDico :",myTreeDico) # return jsonify([myTreeDico]) - # return make_response(json.dumps([myTreeDico])) - #return make_response(json.dumps([monConnecteur.getDicoObjetsCompletsPourTree(monConnecteur.monEditeur.tree.racine)])) + return make_response(json.dumps( {'source':node, 'changeIsAccepted' : changeDone} )) - # # Return a string along with an HTTP status code - # return "JSON received!", 200 + # Return a string along with an HTTP status code + # return "JSON received!", 200 else: # The request body wasn't JSON so return a 400 HTTP status code return "Request was not JSON", 400 + #return make_response(jsonify({"message": "Request body must be JSON"}), 400) ## SSE from Eficas signals : # - Validite diff --git a/testFlask/templates/commandes_2.html b/testFlask/templates/commandes_2.html index 66688544..c2cab9b5 100644 --- a/testFlask/templates/commandes_2.html +++ b/testFlask/templates/commandes_2.html @@ -132,16 +132,25 @@