from flask import Flask, redirect, url_for, render_template, session, request, flash
import flask
from datetime import datetime, timedelta
from database.User import User
from database.databaseEmotion import SQL_Server
app = Flask(__name__)
app.secret_key = 'mykey'
app.permanent_session_lifetime = timedelta(minutes=5)
query = 'SELECT * FROM dbo.Emotion'
sql = SQL_Server()
db = sql.select(query)
''' lớp emotion '''
class Emotion:
_id: int
image :str
LoaiCamXuc :int
ThietBi: int
Ngay :datetime
Kip:int
def __init__(self, _id, image,LoaiCamXuc ,ThietBi,Ngay,Kip) -> None:
self._id = _id
self.image = image
self.LoaiCamXuc = LoaiCamXuc
self.ThietBi = ThietBi
self.Ngay = Ngay
self.Kip = Kip
@app.route('/table', methods = ['GET'])
def emotion():
if request.method == 'GET':
try:
global db
emotion = ["_id", "image","LoaiCamXuc" ,"ThietBi","Ngay","Kip"]
#fruit_dictionary = dict(zip(fruits, prices))
respond = {"status" : "Success", "person" : [dict(zip(emotion, i)) for i in db]}
return flask.jsonify(respond)
except Exception as e:
return flask.jsonify({"status" : "Failed", "msg" : str(e)})
# else:
# try:
# data = request.get_json()
# person = Person(data['_id'], data['name'], data['old'], data['phone'])
# db.append(person)
# return flask.jsonify({"status" : "Success"})
# except Exception as e:
# return flask.jsonify({"status" : "Failed", "msg" : str(e)})
@app.route('/statistic', methods = ['GET'])
def statistics():
if request.method == 'GET':
try:
global db
# {
# "thietbi" : 1,
# "kip": 2
# }
data = request.get_json()
thietbi=data['thietbi']
kip=data['kip']
f={
1:0 ,2:0 ,3:0 ,4:0 ,5:0 ,6:0
}
for entity in db:
if entity[3]==thietbi and entity[5]==kip :
f[entity[2]] += 1
respond = {"status" : "Success", "statistic" : f}
return flask.jsonify(respond)
except Exception as e:
return flask.jsonify({"status" : "Failed", "msg" : str(e)})
@app.route('/showimage', methods = ['GET'])
def list_image():
try:
global db
data = request.get_json()
thietbi=data['thietbi']
kip=data['kip']
emotion=data['emotion']
f = []
#for i in range(7): f.append([])
for entity in db:
if entity[3]==thietbi and entity[5]==kip and entity[2]==emotion :
#f[entity[2]].append( entity[1])
f.append( entity[1])
respond = {"status" : "Success", "statistic" : f}
return flask.jsonify(respond)
except Exception as e:
return flask.jsonify({"status" : "Failed", "msg" : str(e)})
@app.route('/', methods=['GET', 'POST'])
@app.route('/login', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
session.permanent = True
user = request.form['nm']
password = request.form['pass']
if User.find_user(user, password):
session['user'] = user
session['password'] = password
return redirect(url_for('home'))
else:
flash('Không tìm thấy tài khoản hoặc mật khẩu trùng')
return render_template('login.html')
else:
if 'user' in session and 'password' in session:
return redirect(url_for('home'))
return render_template('login.html')
@app.route('/home')
def home():
if 'user' in session and 'password' in session:
return render_template('home.html')
else:
return redirect(url_for('index'))
@app.route('/logout')
def logout():
session.pop('user', None)
session.pop('password', None)
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port='9999', debug=True)