-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
191 lines (153 loc) · 5.47 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from flask import *
from os import getpid, system
from CoreProxy import *
from time import sleep
toDelete = ["mode","surname","name","group","subjects","freetime","="]
srv = Flask("Serveur local CAPS")
def shutdown_server():
system("taskkill /f /PID "+str(getpid()))
@srv.errorhandler(404)
def pageNotFound(error):
return render_template("404.html",error=error)
@srv.errorhandler(500)
def serverError(error):
return render_template("500.html", error=error)
@srv.get('/shutdown')
def shutdown():
shutdown_server()
return 'Server shutting down...'
@srv.route('/results', methods = ['GET','POST'])
def show_results():
if len(request.cookies) == 0:
return redirect("/")
if request.method == 'GET':
filename = request.cookies
datas = CoreLibs.utils.getResults(filename['results'])
if filename["tutoreid"] != None:
tutore = CoreLibs.tutore.getTutoreNameByID(int(filename["tutoreid"]))[0]
else:
tutore = ("NaN","NaN")
return render_template("results.html",tutoreID = filename["tutoreid"],tutoreName = tutore[0],tutoreSurname = tutore[1],creneaux = datas["creneaux"], creneauxTXT = datas["creneauxTXT"], matieres = datas["subjects"], data = datas["results"])
else:
if cfgSRV["enableRel"]:
datas = request.get_data()
datas = datas.decode("utf-8")
datas = datas.replace("tuteurid=","").replace("tutoreid=","").replace("subject=","").replace("time=","").replace("lessons=","")
datas = datas.split("&")
try:
CoreLibs.relations.addRelationship(int(datas[1]),int(datas[0]),datas[2],datas[3],int(datas[4]))
return "OK !",200
except Exception as e:
print("[STDERR] > "+str(e))
return "Internal server error",500
@srv.get("/")
def index():
return render_template('index.html')
@srv.post("/")
def index_post():
try:
datas = request.get_data().decode('utf-8')
for i in toDelete:
datas = datas.replace(i,"")
datas = datas.split("&")
print(datas)
results,id = CoreLibs.backendEntry.modeSplit(datas)
print(results)
redirectURL = "/results"
response = make_response(redirect(redirectURL))
response.set_cookie("results",str(results))
response.set_cookie("tutoreid",str(id[0][0]))
return response, 301
except Exception as e:
print(datas)
print(f"[STDERR] > {e}")
with open("exception.txt", mode='w+') as exceptionFile:
exceptionFile.write(str(e))
exceptionFile.close()
return "Internal server error", 500
@srv.get("/refreshDB")
def refresh():
CoreLibs.utils.refreshDB()
return "Done", 200
@srv.get("/editor/<table>")
def editor(table):
MainDB = CoreLibs.utils.MainDB
cursor = MainDB.cursor()
sqlrequest = "SELECT * FROM '"+table+"'"
cursor.execute(sqlrequest)
data = cursor.fetchall()
file = table+"Editor.html"
return render_template(file,data=data)
@srv.get("/settings")
def show_settings():
return render_template("settings.html",Rel = cfgSRV["enableRel"],Feedback = cfgSRV["enableFeedback"])
@srv.get("/help")
def help_page():
return render_template("help.html",labels = CoreLibs.issueHandler.getLabels())
@srv.route("/redirect/<link>")
def timedRedirect(link):
sleep(1)
return redirect("/"+link)
@srv.post("/cfg/<op>")
def cfgOp(op):
global cfgSRV
if op == "refresh":
cfgSRV = CoreLibs.utils.refreshConfig()
return "OK !"
elif op == "reset":
cfgSRV = CoreLibs.cfgHandler.resetCfg()
return "OK !"
elif op == "write":
data = request.get_data().decode('utf-8')
data = data.replace("enableRel","")
data = data.replace("enableFeedback","")
data = data.replace("=","")
data = data.split("&")
if cfgSRV["enableRel"] != data[0] and data[0] == "true":
CoreLibs.DBCreator.createRelTable()
if cfgSRV["enableFeedback"] != data[1] and data[1] == "true":
CoreLibs.DBCreator.createFeedbackTable()
cfgSRV["enableRel"] = data[0] == "true"
cfgSRV["enableFeedback"] = data[1] == "true"
CoreLibs.cfgHandler.writeCfg(cfgSRV)
return "OK !"
else:
return "Error: opcode "+op+" does not exist for url /cfg/",404
@srv.post("/modifyDB")
def modifyDB():
rq = request.get_data()
updaterq = CoreLibs.utils.createModifyRequest(rq)
MainDB = CoreLibs.utils.MainDB
print(updaterq)
cursor = MainDB.cursor()
cursor.execute(updaterq)
MainDB.commit()
return "Received"
@srv.post("/DB/reset")
def reset():
CoreLibs.utils.resetDB()
return "OK !"
@srv.post("/ticket")
def open_ticket():
data = request.get_data().decode("utf-8")
data = data.replace("title=","")
data = data.replace("body=","")
data = data.replace("labels=","")
data = data.split("&")
gitLabels = CoreLibs.issueHandler.getLabels()
labels = []
for i,e in enumerate(data[2].split(",")):
if e.lower()=="true":
try:
labels.append(CoreLibs.issueHandler.getLabel(gitLabels[i]))
except Exception as e:
print(e)
labels.append(gitLabels[i])
if CoreLibs.issueHandler.issueTemplate(data[0],data[1],labels):
return "OK !",200
return "Erruer",500
def init(conf,db):
global cfgSRV, DBSRV
cfgSRV = conf
DBSRV = db
return