-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
418 lines (346 loc) · 14 KB
/
app.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
from flask import Flask, redirect, render_template, request, url_for, session, flash
import psycopg2
from psycopg2 import pool
app = Flask(__name__)
app.secret_key = 'your secret key2'
#Database connection
minconn = 3
maxconn = 10
conn_pool = psycopg2.pool.SimpleConnectionPool(minconn, maxconn,
user = "postgres",
password = "postgres",
host = "20.203.212.232",
port = "5435",
database = "internprj")
#Function for login page
@app.route('/', methods=['GET', 'POST'])
def index():
conn = conn_pool.getconn() # connect to database
cur = conn.cursor() # prepare query
cur.execute('SELECT * FROM engineer') #get engineer list
engineers_li = cur.fetchall()
if request.method == 'POST':
engineer_id= request.form.get('engineerID') # Get engineer_id
conn = conn_pool.getconn()
try:
cur = conn.cursor()
cur.execute('SELECT * FROM engineer WHERE engineer_id = %s', (engineer_id,)) #get engineer with given id
result = cur.fetchone()
if result:
session['engineer'] = result # load given engineer
return redirect(url_for('dashboard')) # open dashboard.html
else:
return redirect(url_for('index')) # redirect to index.html
finally:
conn_pool.putconn(conn) # end connection to database
return render_template('login.html', list_engineers=engineers_li)
#function to save new engineer
@app.route('/save', methods=['POST'])
def save():
conn = conn_pool.getconn()
cur = conn.cursor()
if request.method == 'POST':
engineer_name_new = request.form.get('engineerNameNew') # take inputted engineer name
cur.execute("INSERT INTO public.engineer(engineer_id, engineer_name) VALUES (DEFAULT, %s)", (engineer_name_new,))
#inserts engineer with given values taken from params engineer_name_new
conn.commit()
flash('Added Successfully')
return redirect(url_for('index'))
#function to load dashboard with list of engineers
@app.route('/engineers', methods=['GET'])
def engineers():
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM engineer')
engineers_li = cur.fetchall()
conn_pool.putconn(conn)
return render_template('dashboard.html', list_engineer=engineers_li)
#functino for dashbaord page
@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
engineer = session.get('engineer') #get taken engineer
if engineer is None:
return redirect(url_for('index'))
conn = conn_pool.getconn()
try:
cur = conn.cursor() # prepare query
cur.execute('SELECT * FROM mip')
mips = cur.fetchall() # take all MIPs
#take the engineer, steps completed, steps completion
cur.execute("""
SELECT E.ENGINEER_NAME, LP.NAME, LPS.LP_STEP_NAME,T.ISCOMPLETE FROM ENGLPSTEPCOMPLETION T
LEFT JOIN ENGINEER E ON E.ENGINEER_ID = T.ENGINEER_ID
LEFT JOIN LEARNINGPATHSTEPS LPS ON LPS.LP_STEP_ID = T.LP_STEP_ID
LEFT JOIN LEARNINGPATH LP ON LP.LEARNINGPATH_ID = LPS.LP_ID
WHERE E.ENGINEER_ID=%s
ORDER BY 2 ASC
""", (engineer[0],) )
par = cur.fetchall()
#take the learningpath, number of steps completed, learningpath completion
cur.execute("""
WITH KOD AS (
SELECT E.ENGINEER_NAME AS NAME,
LP.NAME AS LPNAME,
LPS.LP_STEP_NAME AS LPSNAME,
T.ISCOMPLETE AS RESULT
FROM ENGLPSTEPCOMPLETION T
LEFT JOIN ENGINEER E ON E.ENGINEER_ID = T.ENGINEER_ID
LEFT JOIN LEARNINGPATHSTEPS LPS ON LPS.LP_STEP_ID = T.LP_STEP_ID
LEFT JOIN LEARNINGPATH LP ON LP.LEARNINGPATH_ID = LPS.LP_ID
WHERE E.ENGINEER_ID = %s
ORDER BY 2 ASC
)
SELECT KOD.LPNAME,
COUNT(CASE WHEN KOD.RESULT = 'False' THEN 1 END) AS FalseR,
COUNT(CASE WHEN KOD.RESULT = 'True' THEN 1 END) AS TrueR,
CASE WHEN COUNT(CASE WHEN KOD.RESULT = 'False' THEN 1 END) = 0 THEN 'LP Completed'
ELSE 'LP Not Completed'
END AS LP_STATUS
FROM KOD
GROUP BY KOD.LPNAME
ORDER BY KOD.LPNAME;
""", (engineer[0],))
parComplete = cur.fetchall()
finally:
conn_pool.putconn(conn) # database bağlantısını kes
return render_template('dashboard.html', engineer=engineer, mips=mips, list_par=par, list_complete=parComplete) # dashboard.htmli yükle ve ona engineer(mevcut oturum) ve databaseden elde ettiğimiz mips listesini yükle.
#Function for opening dashboard from login page list
@app.route('/dashboardd/<engineer_id>', methods=['GET'])
def engdash(engineer_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM engineer WHERE engineer_id = %s', (engineer_id,))
result = cur.fetchone()
if result is None:
return redirect(url_for('index'))
conn = conn_pool.getconn()
try:
cur = conn.cursor()
cur.execute('SELECT * FROM mip')
mips = cur.fetchall()
cur.execute("""
SELECT E.ENGINEER_NAME, LP.NAME, LPS.LP_STEP_NAME,T.ISCOMPLETE FROM ENGLPSTEPCOMPLETION T
LEFT JOIN ENGINEER E ON E.ENGINEER_ID = T.ENGINEER_ID
LEFT JOIN LEARNINGPATHSTEPS LPS ON LPS.LP_STEP_ID = T.LP_STEP_ID
LEFT JOIN LEARNINGPATH LP ON LP.LEARNINGPATH_ID = LPS.LP_ID
WHERE E.ENGINEER_ID=%s
ORDER BY 2 ASC
""", (engineer_id,) )
par = cur.fetchall()
cur.execute("""
WITH KOD AS (
SELECT E.ENGINEER_NAME AS NAME,
LP.NAME AS LPNAME,
LPS.LP_STEP_NAME AS LPSNAME,
T.ISCOMPLETE AS RESULT
FROM ENGLPSTEPCOMPLETION T
LEFT JOIN ENGINEER E ON E.ENGINEER_ID = T.ENGINEER_ID
LEFT JOIN LEARNINGPATHSTEPS LPS ON LPS.LP_STEP_ID = T.LP_STEP_ID
LEFT JOIN LEARNINGPATH LP ON LP.LEARNINGPATH_ID = LPS.LP_ID
WHERE E.ENGINEER_ID = %s
ORDER BY 2 ASC
)
SELECT KOD.LPNAME,
COUNT(CASE WHEN KOD.RESULT = 'False' THEN 1 END) AS FalseR,
COUNT(CASE WHEN KOD.RESULT = 'True' THEN 1 END) AS TrueR,
CASE WHEN COUNT(CASE WHEN KOD.RESULT = 'False' THEN 1 END) = 0 THEN 'LP Completed'
ELSE 'LP Not Completed'
END AS LP_STATUS
FROM KOD
GROUP BY KOD.LPNAME
ORDER BY KOD.LPNAME;
""", (engineer_id,))
parComplete = cur.fetchall()
finally:
conn_pool.putconn(conn)
return render_template('dashboard.html', engineer=result, mips=mips, list_par=par, list_complete=parComplete)
#Function to open mips.html
@app.route('/mips', methods=['GET'])
def mip_list_link():
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM mip')
mips = cur.fetchall()
return render_template('mips.html', list_mips=mips)
#Function to open paths.html
@app.route('/paths/<mip_id>', methods=['GET'])
def paths(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM learningpath WHERE mip_id = %s', (mip_id,))
result = cur.fetchall()
print(result)
conn_pool.putconn(conn)
return render_template('paths.html', list_mips=result)
#function for pathsteps.html
@app.route('/pathsteps/<learningpath_id>', methods=['GET'])
def pathsteps(learningpath_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM learningpathsteps WHERE lp_id = %s', (learningpath_id,))
result = cur.fetchall()
print(result)
conn_pool.putconn(conn)
return render_template('pathsteps.html', list_steps=result)
#function to update an engineer
@app.route('/update/<engineer_id>', methods=['POST'])
def update_engineer(engineer_id):
if request.method == 'POST':
engineer_name = request.form['engineer_name']
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute("""
UPDATE engineer
SET engineer_name = %s
WHERE engineer_id = %s
""", (engineer_name, engineer_id))
flash('Engineer Updated Successfully')
conn.commit()
return redirect(url_for('index'))
#function to edit an engineer
@app.route('/edit/<engineer_id>', methods = ['POST', 'GET'])
def edit(engineer_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM engineer WHERE engineer_id = {0}'.format(engineer_id))
data = cur.fetchall()
cur.close()
print(data[0])
return render_template('edit.html', engineer = data[0])
#function to edit a MIP
@app.route('/edit_mip/<mip_id>', methods = ['POST', 'GET'])
def edit_mip(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM mip WHERE mip_id = {0}'.format(mip_id))
data = cur.fetchall()
cur.execute('SELECT * FROM learningpath WHERE mip_id = {0}'.format(mip_id))
path = cur.fetchall()
cur.close()
print(data[0])
return render_template('editMIP.html', mip = data[0], learningpath=path)
#function to add a MIP
@app.route('/add_mip/<mip_id>', methods = ['POST', 'GET'])
def add_mip(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM mip WHERE mip_id = {0}'.format(mip_id))
data = cur.fetchall()
cur.close()
print(data[0])
return render_template('mipADD.html', mip = data[0])
#Function to update MIP
@app.route('/update_mip/<mip_id>', methods=['POST'])
def update_mip(mip_id):
if request.method == 'POST':
mip_name = request.form['mip_name']
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute("""
UPDATE mip
SET mip_name = %s
WHERE mip_id = %s
""", (mip_name, mip_id))
flash('MIP Updated Successfully')
conn.commit()
return redirect(url_for('dashboard'))
#Function to save MIP
@app.route('/save_mip', methods=['POST'])
def save_mip():
conn = conn_pool.getconn()
cur = conn.cursor()
if request.method == 'POST':
engineer_mip_new = request.form.get('mipNameNew') #take inputted MIP name
cur.execute("INSERT INTO public.mip(mip_id, mip_name) VALUES (DEFAULT, %s)", (engineer_mip_new,))
#inserts mip with given values taken from params new mip ...
conn.commit()
flash('Added Successfully')
return redirect(url_for('dashboard'))
#Function to save a mip
@app.route('/save_mip_list', methods=['POST'])
def save_mip_list():
conn = conn_pool.getconn()
cur = conn.cursor()
if request.method == 'POST':
engineer_mip_new = request.form.get('mipNameNew')
cur.execute("INSERT INTO public.mip(mip_id, mip_name) VALUES (DEFAULT, %s)", (engineer_mip_new,))
conn.commit()
flash('Added Succesfully')
return redirect(url_for('mip_list_link'))
#Function to delete engineer
@app.route('/delete/<engineer_id>', methods = ['POST','GET'])
def delete(engineer_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('DELETE FROM engineer WHERE engineer_id = {0}'.format(engineer_id))
conn.commit()
flash('Engineer Removed Successfully')
return redirect(url_for('index'))
#Function to delete a mip
@app.route('/delete_mip/<mip_id>', methods = ['POST','GET'])
def delete_mip(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('DELETE FROM mip WHERE mip_id = {0}'.format(mip_id))
conn.commit()
return redirect(url_for('dashboard'))
#Funtion to delete a mip
@app.route('/delete_mip_list/<mip_id>', methods = ['POST','GET'])
def delete_mip_list(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('DELETE FROM mip WHERE mip_id = {0}'.format(mip_id))
conn.commit()
return redirect(url_for('mip_list_link'))
#function to edit a LP
@app.route('/edit_lp/<learningpath_id>', methods = ['POST', 'GET'])
def edit_lp(learningpath_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM learningpath WHERE learningpath_id = {0}'.format(learningpath_id))
data = cur.fetchall()
cur.close()
return render_template('editlp.html', learningpath = data[0])
#function to update a Learningpath
@app.route('/update_lp/<learningpath_id>', methods=['POST'])
def update_lp(learningpath_id):
if request.method == 'POST':
lp_name = request.form['name']
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute("""
UPDATE learningpath
SET name = %s
WHERE learningpath_id = %s
""", (lp_name, learningpath_id))
flash('LP Updated Successfully')
conn.commit()
return redirect(url_for('dashboard'))
#function to save new learningpath
@app.route('/save_lp/<mip_id>', methods=['POST'])
def save_lp(mip_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('SELECT * FROM learningpath WHERE mip_id = {0}'.format(mip_id))
data = cur.fetchall()
cur.execute('SELECT * FROM mip WHERE mip_id = {0}'.format(mip_id))
mip = cur.fetchall()
if request.method == 'POST':
lp_name_new = request.form.get('name') # take inputted engineer name
cur.execute("INSERT INTO public.learningpath(learningpath_id, mip_id, name) VALUES (DEFAULT, %s, %s)", (mip_id, lp_name_new,))
#inserts engineer with given values taken from params engineer_name_new
conn.commit()
flash('Added Successfully')
return redirect(url_for('dashboard'))
#Function to delete a learningpath
@app.route('/delete_lp/<learningpath_id>', methods = ['POST','GET'])
def delete_lp(learningpath_id):
conn = conn_pool.getconn()
cur = conn.cursor()
cur.execute('DELETE FROM learningpath WHERE learningpath_id = {0}'.format(learningpath_id))
conn.commit()
return render_template('paths.html')
# MAIN FONKSİYONUMUZ (ÖNEMSİZ) OLDUĞU GİBİ BIRAKABİLİRSİNİZ
if __name__ == '__main__':
app.run()