From 67817addb9d8e8210e426d8f9cd1060fd9be97dd Mon Sep 17 00:00:00 2001 From: Lorenzo Balugani Date: Sun, 28 Apr 2019 16:12:39 +0200 Subject: [PATCH] Add flask server for online functionalities --- Flask-Server/dummy.py | 3 +++ Flask-Server/server.py | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 Flask-Server/dummy.py create mode 100644 Flask-Server/server.py diff --git a/Flask-Server/dummy.py b/Flask-Server/dummy.py new file mode 100644 index 0000000..da31816 --- /dev/null +++ b/Flask-Server/dummy.py @@ -0,0 +1,3 @@ +import requests +r = requests.post("http://127.0.0.1:5000/message/2", data={'content':'Piano 2'}) +print(r.text) \ No newline at end of file diff --git a/Flask-Server/server.py b/Flask-Server/server.py new file mode 100644 index 0000000..287fec6 --- /dev/null +++ b/Flask-Server/server.py @@ -0,0 +1,58 @@ +from flask import Flask, session, url_for, redirect, request, render_template, abort, flash, Markup, json +from datetime import datetime +from flask_sqlalchemy import SQLAlchemy +import random +import os +from sqlalchemy.sql.expression import func + +app = Flask(__name__) +app.secret_key = "debug-attivo" +app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' +app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db = SQLAlchemy(app) +app.config.from_object(__name__) + + +# Database Classes go beyond this line + +class Message(db.Model): + mid = db.Column(db.Integer, primary_key=True) + floor = db.Column(db.Integer, nullable=False) + content = db.Column(db.String, nullable=False) + + def toJSON(self): + return {"Message": {'floor': self.floor, + 'content': self.content}} + + def __init__(self, content, floor): + self.content = content + self.floor = floor + + def __repr__(self): + return self.content + + +def give_json_response(floor): + messages = Message.query.filter_by(floor=floor).order_by(func.random()).limit(5).all() + return json.dumps([message.toJSON() for message in messages]) + + +# Website pages and API functions go beyond this line +@app.route("/message/", methods=["GET", "POST"]) +def page_message(floor): + if request.method == "GET": + return give_json_response(floor) + newmessage = Message(request.form['content'], floor) + db.session.add(newmessage) + db.session.commit() + return "Success." + + +if __name__ == "__main__": + # Se non esiste il database viene creato + if not os.path.isfile("db.sqlite"): + newmessage = Message("Welcome to the dungeon, mortal.", 1) + db.create_all() + db.session.add(newmessage) + db.session.commit() + app.run(debug=True)