-
Notifications
You must be signed in to change notification settings - Fork 0
/
userHistoryServer.py
63 lines (42 loc) · 1.81 KB
/
userHistoryServer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
##################### Library Imports #####################
import userHistory
from flask import Flask, request, jsonify, abort
import datetime
# -- ADINT Final Project
# -- Made by: Diogo Ferreira and Rafael Cordeiro
# ----------------------------------------
# --------------User History Server-------
# ----------------------------------------
#Flask
app = Flask(__name__)
@app.route("/user/occurrences/history",methods = ['GET'])
def listHistRequest():
User_list = userHistory.getuserHistory()
result1 = [{'id_user_occurence':item.id_user_occurence,'user':item.user,'gate_id':item.gate_id,'Date':item.Date} for item in User_list]
return jsonify({"history": result1})
@app.route("/user/occurrences/<path:istID>/history",methods = ['GET'])
def listUserHistRequest(istID):
User_list = userHistory.GetUserOccurrences(istID)
result1 = [{'id_user_occurence':item.id_user_occurence,'user':item.user,'gate_id':item.gate_id,'Date':item.Date} for item in User_list]
return jsonify({"history": result1})
# Database endpoint for adding new occurrences
@app.route("/user/occurrences/newOccurrence",methods = ['POST'])
def newOccurrenceRequest():
#retrieve data from input JSON body
data = request.json
try:
data["user"]
int(data["gate_id"])
except:
abort(400)
try:
status = userHistory.newOcurrence(data["user"],int(data["gate_id"]),datetime.datetime.now())
except:
return jsonify({'StatusCode':'2', 'Description':'Err'})
if status:
return jsonify({'StatusCode':'1','Description':'OK'})
else:
return jsonify({'StatusCode':'3','Description':'NA'})
#Start server
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8000, debug=True)