-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
528 lines (395 loc) · 15 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
from flask import render_template
import sqlite3
# import requests
from flask import Flask
from flask import request,redirect,url_for,session,flash
from flask_wtf import Form
from wtforms import TextField
app = Flask(__name__)
app.secret_key = "super secret key"
@app.route('/')
def hel():
conn = sqlite3.connect('database.db')
print("Opened database successfully")
conn.execute('CREATE TABLE IF NOT EXISTS users (name TEXT, addr TEXT, city TEXT, pin TEXT, bg TEXT,email TEXT UNIQUE, pass TEXT)')
print( "Table created successfully")
conn.close()
if session.get('username')==True:
messages = session['username']
else:
messages = ""
user = {'username': messages}
return redirect(url_for('index',user=user))
@app.route('/reg')
def add():
return render_template('register.html')
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
msg = ""
#con = None
if request.method == 'POST':
try:
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
bg = request.form['bg']
email = request.form['email']
passs = request.form['pass']
with sqlite3.connect("database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO users (name,addr,city,pin,bg,email,pass) VALUES (?,?,?,?,?,?,?)",(nm,addr,city,pin,bg,email,passs) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
flash('done')
return redirect(url_for('index'))
con.close()
@app.route('/index',methods = ['POST','GET'])
def index():
if request.method == 'POST':
if session.get('username') is not None:
messages = session['username']
else:
messages = ""
user = {'username': messages}
print(messages)
val = request.form['search']
print(val)
type = request.form['type']
print(type)
if type=='blood':
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from users where bg=?",(val,))
search = cur.fetchall();
cur.execute("select * from users ")
rows = cur.fetchall();
return render_template('index.html', title='Home', user=user,rows=rows,search=search)
if type=='donorname':
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from users where name=?",(val,))
search = cur.fetchall();
cur.execute("select * from users ")
rows = cur.fetchall();
return render_template('index.html', title='Home', user=user,rows=rows,search=search)
if session.get('username') is not None:
messages = session['username']
else:
messages = ""
user = {'username': messages}
print(messages)
if request.method=='GET':
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from users ")
rows = cur.fetchall();
return render_template('index.html', title='Home', user=user, rows=rows)
#messages = request.args['user']
@app.route('/list')
def list():
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from users")
rows = cur.fetchall();
print(rows)
return render_template("list.html",rows = rows)
@app.route('/drop')
def dr():
con = sqlite3.connect('database.db')
con.execute("DROP TABLE request")
return "dropped successfully"
@app.route('/login',methods = ['POST', 'GET'])
def login():
if request.method == 'GET':
return render_template('/login.html')
if request.method == 'POST':
email = request.form['email']
password = request.form['pass']
if email == '[email protected]' and password == 'admin':
a = 'yes'
session['username'] = email
#session['logged_in'] = True
session['admin'] = True
return redirect(url_for('index'))
#print((password,email))
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select email,pass from users where email=?",(email,))
rows = cur.fetchall();
for row in rows:
print(row['email'],row['pass'])
a = row['email']
session['username'] = a
session['logged_in'] = True
print(a)
u = {'username': a}
p = row['pass']
print(p)
if email == a and password == p:
return redirect(url_for('index'))
else:
return render_template('/login.html')
return render_template('/login.html')
else:
return render_template('/')
@app.route('/logout')
def logout():
# remove the username from the session if it is there
session.pop('username', None)
session.pop('logged_in',None)
try:
session.pop('admin',None)
except KeyError as e:
print("I got a KeyError - reason " +str(e))
return redirect(url_for('login'))
@app.route('/dashboard')
def dashboard():
totalblood=0
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from blood")
rows = cur.fetchall();
for row in rows:
totalblood += int(row['qty'])
cur.execute("select * from users")
users = cur.fetchall();
Apositive=0
Opositive=0
Bpositive=0
Anegative=0
Onegative=0
Bnegative=0
ABpositive=0
ABnegative = 0
print(rows)
cur.execute("select * from blood where type=?",('A+',))
type = cur.fetchall();
for a in type:
Apositive += int(a['qty'])
cur.execute("select * from blood where type=?",('A-',))
type = cur.fetchall();
for a in type:
Anegative += int(a['qty'])
cur.execute("select * from blood where type=?",('O+',))
type = cur.fetchall();
for a in type:
Opositive += int(a['qty'])
cur.execute("select * from blood where type=?",('O-',))
type = cur.fetchall();
for a in type:
Onegative += int(a['qty'])
cur.execute("select * from blood where type=?",('B+',))
type = cur.fetchall();
for a in type:
Bpositive += int(a['qty'])
cur.execute("select * from blood where type=?",('B-',))
type = cur.fetchall();
for a in type:
Bnegative += int(a['qty'])
cur.execute("select * from blood where type=?",('AB+',))
type = cur.fetchall();
for a in type:
ABpositive += int(a['qty'])
cur.execute("select * from blood where type=?",('AB-',))
type = cur.fetchall();
for a in type:
ABnegative += int(a['qty'])
bloodtypestotal = {'apos': Apositive,'aneg':Anegative,'opos':Opositive,'oneg':Onegative,'bpos':Bpositive,'bneg':Bnegative,'abpos':ABpositive,'abneg':ABnegative}
return render_template("requestdonors.html",rows = rows,totalblood = totalblood,users=users,bloodtypestotal=bloodtypestotal)
@app.route('/bloodbank')
def bl():
conn = sqlite3.connect('database.db')
print("Opened database successfully")
conn.execute('CREATE TABLE IF NOT EXISTS blood (id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT, donorname TEXT, donorsex TEXT, qty TEXT, dweight TEXT, donoremail TEXT, phone TEXT)')
print( "Table created successfully")
conn.close()
return render_template('/adddonor.html')
@app.route('/addb',methods =['POST','GET'])
def addb():
msg = ""
if request.method == 'POST':
try:
type = request.form['blood_group']
donorname = request.form['donorname']
donorsex = request.form['gender']
qty = request.form['qty']
dweight = request.form['dweight']
email = request.form['email']
phone = request.form['phone']
with sqlite3.connect("database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO blood (type,donorname,donorsex,qty,dweight,donoremail,phone) VALUES (?,?,?,?,?,?,?)",(type,donorname,donorsex,qty,dweight,email,phone) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
flash("added new entry!")
return redirect(url_for('dashboard'))
con.close()
else:
return render_template("rest.html",msg=msg)
@app.route("/editdonor/<id>", methods=('GET', 'POST'))
def editdonor(id):
msg =""
if request.method == 'GET':
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from blood where id=?",(id,))
rows = cur.fetchall();
return render_template("editdonor.html",rows = rows)
if request.method == 'POST':
try:
type = request.form['blood_group']
donorname = request.form['donorname']
donorsex = request.form['gender']
qty = request.form['qty']
dweight = request.form['dweight']
email = request.form['email']
phone = request.form['phone']
with sqlite3.connect("database.db") as con:
cur = con.cursor()
cur.execute("UPDATE blood SET type = ?, donorname = ?, donorsex = ?, qty = ?,dweight = ?, donoremail = ?,phone = ? WHERE id = ?",(type,donorname,donorsex,qty,dweight,email,phone,id) )
con.commit()
msg = "Record successfully updated"
except:
con.rollback()
msg = "error in insert operation"
finally:
flash('saved successfully')
return redirect(url_for('dashboard'))
con.close()
@app.route("/myprofile/<email>", methods=('GET', 'POST'))
def myprofile(email):
msg =""
if request.method == 'GET':
con = sqlite3.connect('database.db')
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute("select * from users where email=?",(email,))
rows = cur.fetchall();
return render_template("myprofile.html",rows = rows)
if request.method == 'POST':
try:
name = request.form['name']
addr = request.form['addr']
city = request.form['city']
pin = request.form['pin']
bg = request.form['bg']
emailid = request.form['email']
print(name,addr)
with sqlite3.connect("database.db") as con:
cur = con.cursor()
cur.execute("UPDATE users SET name = ?, addr = ?, city = ?, pin = ?,bg = ?, email = ? WHERE email = ?",(name,addr,city,pin,bg,emailid,email) )
con.commit()
msg = "Record successfully updated"
except:
con.rollback()
msg = "error in insert operation"
finally:
flash('profile saved')
return redirect(url_for('index'))
con.close()
@app.route('/contactforblood/<emailid>', methods=('GET', 'POST'))
def contactforblood(emailid):
if request.method == 'GET':
conn = sqlite3.connect('database.db')
print("Opened database successfully")
conn.execute('CREATE TABLE IF NOT EXISTS request (id INTEGER PRIMARY KEY AUTOINCREMENT, toemail TEXT, formemail TEXT, toname TEXT, toaddr TEXT)')
print( "Table created successfully")
fromemail = session['username']
name = request.form['nm']
addr = request.form['add']
print(fromemail,emailid)
conn.execute("INSERT INTO request (toemail,formemail,toname,toaddr) VALUES (?,?,?,?)",(emailid,fromemail,name,addr) )
conn.commit()
conn.close()
flash('request sent')
return redirect(url_for('index'))
if request.method == 'POST':
conn = sqlite3.connect('database.db')
print("Opened database successfully")
conn.execute('CREATE TABLE IF NOT EXISTS request (id INTEGER PRIMARY KEY AUTOINCREMENT, toemail TEXT, formemail TEXT, toname TEXT, toaddr TEXT)')
print( "Table created successfully")
fromemail = session['username']
name = request.form['nm']
addr = request.form['add']
print(fromemail,emailid)
conn.execute("INSERT INTO request (toemail,formemail,toname,toaddr) VALUES (?,?,?,?)",(emailid,fromemail,name,addr) )
conn.commit()
conn.close()
flash('request sent')
return redirect(url_for('index'))
@app.route('/notifications',methods=('GET','POST'))
def notifications():
if request.method == 'GET':
conn = sqlite3.connect('database.db')
print("Opened database successfully")
conn.row_factory = sqlite3.Row
cur = conn.cursor()
cor = conn.cursor()
cur.execute('select * from request where toemail=?',(session['username'],))
cor.execute('select * from request where toemail=?',(session['username'],))
row = cor.fetchone();
rows = cur.fetchall();
if row==None:
return render_template('notifications.html')
else:
return render_template('notifications.html',rows=rows)
@app.route('/deleteuser/<useremail>',methods=('GET', 'POST'))
def deleteuser(useremail):
if request.method == 'GET':
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('delete from users Where email=?',(useremail,))
flash('deleted user:'+useremail)
conn.commit()
conn.close()
return redirect(url_for('dashboard'))
@app.route('/deletebloodentry/<id>',methods=('GET', 'POST'))
def deletebloodentry(id):
if request.method == 'GET':
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('delete from blood Where id=?',(id,))
flash('deleted entry:'+id)
conn.commit()
conn.close()
return redirect(url_for('dashboard'))
@app.route('/deleteme/<useremail>',methods=('GET', 'POST'))
def deleteme(useremail):
if request.method == 'GET':
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('delete from users Where email=?',(useremail,))
flash('deleted user:'+useremail)
conn.commit()
conn.close()
session.pop('username', None)
session.pop('logged_in',None)
return redirect(url_for('index'))
@app.route('/deletenoti/<id>',methods=('GET', 'POST'))
def deletenoti(id):
if request.method == 'GET':
conn = sqlite3.connect('database.db')
cur = conn.cursor()
cur.execute('delete from request Where id=?',(id,))
flash('deleted notification:'+id)
conn.commit()
conn.close()
return redirect(url_for('notifications'))
if __name__ == '__main__':
app.run(debug=True)