-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
626 lines (560 loc) · 19.2 KB
/
main.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="Shraddha@2004",
database="fooddb"
)
mycursor = mydb.cursor(buffered=True)
@app.route("/home")
def home():
return render_template("index.html")
@app.route("/")
def Login():
return render_template("index.html")
@app.route("/About")
def About():
return render_template("About.html")
@app.route("/Contact")
def Contact():
return render_template("Contact.html")
donor_id=0;
ngo_id=0
admin_username=""
ngo_username=""
donor_username=""
@app.route("/load_login")
def load_login():
return render_template('login_register.html')
@app.route("/load_login",methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']
mycursor.execute("select username,password from admin")
flag = False
global ngo_username,admin_username,donor_username
for x in mycursor:
if ((x[0] == username) and (x[1] == password)):
flag = True
admin_username=x[0]
break
if (flag == True):
print("Login Done")
return render_template('admin_dashboard.html',val=1)
else:
mycursor.execute("select donor_id,username,password from donor")
global donor_id;
for x in mycursor:
if ((x[1] == username) and (x[2] == password)):
flag = True
donor_id=x[0]
donor_username=x[1]
break
if(flag==True):
return render_template('donor_dashboard.html',donor_username=donor_username,success=True)
else:
mycursor.execute("select ngo_id,username,password from ngo")
global ngo_id
for x in mycursor:
if ((x[1] == username) and (x[2] == password)):
flag = True
ngo_id=x[0]
ngo_username=x[1]
print(ngo_id)
break
if(flag==True):
return render_template('ngo_dashboard.html',ngo_username=ngo_username,val=1)
return render_template('login_register.html',error=1)
#Admin Page
@app.route("/donor")
def view_donor():
global mycursor
list1 = []
mycursor.callproc("donor_details")#Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('admin_donor_details.html',list1=list1)
#Food_Item
@app.route("/Food")
def Food():
global mycursor
list1 = []
mycursor.callproc("food_details")#Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('admin_food_details.html',list1=list1)
#Admin_NGO
@app.route("/NGO")
def receive():
global mycursor
list1 = []
mycursor.callproc("ngo_details")#Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('admin_ngo_details.html',list1=list1)
#Admin_Needy
@app.route("/Needy")
def view_needy():
global mycursor
list1 = []
mycursor.callproc("needy_details1") #Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('admin_needy_details.html',list1=list1)
@app.route("/admin_ngo_count")
def admin_ngo_count():
global mycursor
list1 = []
mycursor.execute("select name from donor_ngo_view")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
return render_template('admin_ngo_count.html',list1=list1)
@app.route("/check_ngo_count",methods=['POST'])
def check_ngo_count():
print("ngo count")
global mycursor
ngoname=request.form.get('ngo_name')
print(ngoname)
list1=[]
sql="select ngo_id from ngo where name=%s"
mycursor.execute(sql,(ngoname,))
myresult=mycursor.fetchall()
ngo_id = 0;
for x in myresult:
ngo_id = x[0]
print(ngo_id)
sql = "select distinct food_id from receive where ngo_id=%s"
mycursor.execute(sql, (ngo_id,))
myresult = mycursor.fetchall()
food_id, donor_id = [], [];
for x in myresult:
food_id.append(x[0])
print(food_id)
for i in food_id:
sql = "SELECT DISTINCT donor_id FROM donation WHERE food_id = %s"
mycursor.execute(sql, (i,))
myresult = mycursor.fetchall()
for x in myresult:
donor_id.append(x[0])
print(donor_id)
dname = []
phno = []
colony=[]
city=[]
list1=[]
for i in donor_id:
sql = "SELECT name,phno,colony,city FROM donor WHERE donor_id = %s"
mycursor.execute(sql, (i,))
myresult = mycursor.fetchall()
# Extract donor_ids from the query results and append to donor_ids list
for x in myresult:
dname.append(x[0])
phno.append(x[1])
colony.append(x[2])
city.append(x[3])
list1.append(x)
print(dname, phno)
cnt= len(list1)
#return render_template("show_details_donor.html",dname=dname,phno=phno,colony=colony,city=city)
return render_template("show_details_donor.html",list1=list1,cnt=cnt)
@app.route("/admin_donor_count")
def admin_donor_count():
global mycursor
list1 = []
mycursor.execute("select name from donor")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
return render_template('admin_donor_count.html',list1=list1)
@app.route("/admin_check_donor",methods=['POST'])
def admin_check_donor():
print("donor details")
global mycursor
donorname=request.form.get('donor_name')
print(donorname)
qunatity,foodtype=[],[]
sql="select donor_id from donor where name=%s"
mycursor.execute(sql,(donorname,))
myresult=mycursor.fetchall()
donor_id = 0;
for x in myresult:
donor_id = x[0]
print(donor_id)
sql = "select distinct food_id from donation where donor_id=%s"
mycursor.execute(sql, (donor_id,))
myresult = mycursor.fetchall()
food_id, donor_id = [], [];
for x in myresult:
food_id.append(x[0])
print(food_id)
list1=[]
foodname=[]
foodtype=[]
qunatity=[]
for i in food_id:
sql = "SELECT distinct food_id,foodname,quantity,foodtype FROM fooditem WHERE food_id = %s"
mycursor.execute(sql, (i,))
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
foodname.append(x[0])
foodtype.append(x[2])
qunatity.append(x[1])
return render_template("show_admin_donor.html",list1=list1)
@app.route("/register_ngo")
def register_ngo():
return render_template('ngo_register.html')
@app.route("/register_needy")
def register_needy():
return render_template('needy_register.html')
@app.route("/register_donor")
def register_donor():
print("Donor")
return render_template("donor_register.html")
#function
@app.route("/donor_insert",methods=['POST'])
def donor_insert():
dname=request.form.get('dname')
phno=request.form.get('phno')
city=request.form.get('city')
colony=request.form.get('colony')
uname=request.form.get('uname')
psw=request.form.get('psw');
val=(dname,phno,colony,city,uname,psw)
print(val)
mycursor.execute("SELECT insert_donor_details1(%s,%s,%s,%s,%s,%s)", val)
success = mycursor.fetchone()[0]
if success == 1:
print("Data inserted successfully!")
else:
print("Failed to insert data.")
mydb.commit()
#return render_template("load_login.html")
return redirect(url_for('load_login'))
@app.route("/ngo_insert",methods=['POST'])
def ngo_insert():
name=request.form.get('name')
phno=request.form.get('phno')
status=request.form.get('status')
task=request.form.get('task')
uname = request.form.get('uname')
pwd=request.form.get('password')
val=(name,phno,status,task,uname,pwd)
print(val)
mycursor.execute("SELECT insert_ngo_details(%s,%s,%s,%s,%s,%s)", val) # function
success = mycursor.fetchone()[0]
if success == 1:
print("Data inserted successfully!")
else:
print("Failed to insert data.")
mydb.commit()
#return render_template("load_login.html")
return redirect(url_for('load_login'))
#sign up form for needy people
@app.route("/needy_people_insert",methods=['POST'])
def needy_people_insert():
print("Needy_people Register form")
name = request.form.get('needname')
phno = request.form.get('phno')
city = request.form.get('city')
colony = request.form.get('colony')
income = request.form.get('income')
# Add other form fields as needed (e.g., email)
mycursor = mydb.cursor()
query="INSERT INTO needy_people (name,phno,city,colony,income) VALUES (%s, %s, %s,%s,%s);"
val=(name,phno,city,colony,income)
count=mycursor.execute(query,val)
mydb.commit()
print("Signup Successful!")
# return render_template('login_register.html')
return redirect(url_for('load_login'))
#view
#Donor
@app.route("/donor_ngo")
def donor_ngo():
global mycursor
list1 = []
mycursor.execute("select * from donor_ngo_view")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
return render_template('donor_ngo.html',list1=list1)
#view
@app.route("/donate")
def donate():
global mycursor
list1 = []
mycursor.execute("select distinct foodtype from fooditem")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x[0])
return render_template('donate.html',list1=list1)
foodtype=""
foodname=""
@app.route("/donate",methods=['POST'])
def donate_food():
global mycursor,foodtype
list1 = []
foodtype=request.form.get('foodtype')
mycursor.execute("select distinct foodtype from fooditem")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x[0])
if(foodtype=="None"):
return render_template('donate.html', list1=list1,msg="You can not donate")
mycursor.execute("select distinct foodname from fooditem where foodtype=%s",(foodtype,))
list2=[]
myresult = mycursor.fetchall()
for x in myresult:
list2.append(x[0])
print(list2)
return render_template('donate_food.html',list1=list2)
@app.route("/donate_food_check",methods=['POST'])
def donate_food_check():
global mycursor,foodname,foodtype,donor_id,donor_username
list1 = []
list2=[];
foodname=request.form.get('foodname')
print(foodname)
mycursor.execute("select distinct foodname from fooditem where foodtype=%s", (foodtype,))
myresult = mycursor.fetchall()
for x in myresult:
list2.append(x[0])
print(list2)
if(foodname=="None"):
return render_template('donate_food.html', list1=list2,msg="You can not donate")
quantity = request.form['qun']
print(foodtype, quantity)
val = (foodtype, quantity, foodname)
sql = "INSERT INTO fooditem (foodtype,quantity,foodname) VALUES (%s,%s,%s)"
val = (foodtype, quantity, foodname)
print(val)
mycursor.execute(sql, val)
mycursor.execute("select food_id from fooditem order by food_id desc limit 1")
for x in mycursor:
food_id = x[0]
print(x)
sql = "INSERT INTO Donation (food_id,donor_id) VALUES (%s,%s)"
val = (food_id, donor_id);
print(food_id, donor_id)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
return render_template('donor_dashboard.html')
#mycursor.execute("select foodname from fooditem where foodtype=%s",(foodtype,))
"""list1=[]
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x[0])
print(list1)"""
@app.route("/donate",methods=['POST'])
def donate_info():
return render_template('donor_dashboard.html')
global mycursor,donor_id,donor_username,foodname,foodtype;
print(donor_id)
quantity = request.form['qun']
print(type,quantity)
val=(type,quantity,donor_id)
sql = "INSERT INTO fooditem (foodtype,quantity,foodname) VALUES (%s,%s,%s)"
val = (type,quantity,foodname)
print(val)
mycursor.execute(sql, val)
mycursor.execute("select food_id from fooditem order by food_id desc limit 1")
for x in mycursor:
food_id=x[0]
print(x)
sql = "INSERT INTO Donation (food_id,donor_id) VALUES (%s,%s)"
val = (food_id,donor_id);
print(food_id,donor_id)
mycursor.execute(sql, val)
"""
mycursor.execute("SELECT donate(%s,%s,%s)", val) #function call
success = mycursor.fetchone()[0]
if success == 1:
print("Data inserted successfully!")
else:
print("Failed to insert data.")
mydb.commit()
"""
mydb.commit()
print(mycursor.rowcount, "record inserted.")
return render_template('donor_dashboard.html')
#donor_history
@app.route("/donation_history")
def donation_history():
global mycursor
list1 = []
global donor_id;
"""
#sql="select name,phno,colony,city from donor where donor_id in(select donor_id from donation where food_id in(select food_id from receive where ngo_id =%s))"
sql="select foodtype,quantity from fooditem where food_id in(select food_id from donation where donor_id=%s)"
mycursor.execute(sql,(donor_id,))
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
print(list1)"""
mycursor.callproc("donation_history",(donor_id,)) # Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('donar_history.html',list1=list1)
#NGO : View donor
@app.route("/ngo_view_donor")
def ngo_view_donor():
global mycursor
list1 = []
global ngo_id;
"""
sql="select name,phno,colony,city from donor where donor_id in(select donor_id from donation where food_id in(select food_id from receive where ngo_id =%s))"
mycursor.execute(sql,(ngo_id,))
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
print(list1)"""
mycursor.callproc("view_donor", (ngo_id,)) # Procedure Call
for result in mycursor.stored_results():
rows = result.fetchall()
for row in rows:
list1.append(row)
return render_template('ngo_donor.html',list1=list1)
#NGO
@app.route("/receive_donation")
def receive_donation():
list1 = []
list2=[]
list3=[]
try:
mycursor.execute("SELECT food_id,foodtype,foodname,quantity from FoodItem where food_id not in(select food_id from receive)")
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x[0])
list2.append(x[1])
list3.append(x)
print(list3)
except mysql.connector.Error as error:
# Handle MySQL errors
print("MySQL Error: {}".format(error))
return render_template('receive_donation.html', list3=list3,list1=list1,list2=list2)
@app.route("/receive_food",methods=['POST'])
def receive_food():
global mycursor,ngo_id
print(ngo_id)
food_id = (request.form.get('food_id'))
print(food_id)
#food=food_id.split("")
#print(food[1])
#food_id=food[1]
"""sql="insert into receive(food_id,ngo_id) values(%s,%s)"
sql="receive_food"
val=(food_id,ngo_id)
print(val)
mycursor.execute(sql,val)"""
mycursor.execute("SELECT receive_food(%s,%s)", (food_id,ngo_id))
success = mycursor.fetchone()[0]
if success == 1:
print("Data inserted successfully!")
else:
print("Failed to insert data.")
mydb.commit()
print(mycursor.rowcount, "record inserted.")
return render_template('ngo_dashboard.html')
#NGO_History
@app.route("/ngo_history")
def ngo_history():
global mycursor
list1 = []
global ngo_id;
#sql="select name,phno,colony,city from donor where donor_id in(select donor_id from donation where food_id in(select food_id from receive where ngo_id =%s))"
sql="select foodtype,foodname,quantity from fooditem where food_id in (select food_id from receive where ngo_id=%s)"
mycursor.execute(sql,(ngo_id,))
myresult = mycursor.fetchall()
for x in myresult:
list1.append(x)
print(list1)
return render_template('ngo_history.html',list1=list1)
@app.route("/ngo_profile")
def ngo_profile():
global mycursor
list1 = []
global ngo_id;
sql="select name,phno,status,task from ngo where ngo_id=%s"
mycursor.execute(sql,(ngo_id,))
myresult = mycursor.fetchall()
name,status,task="","",""
phno=""
for x in myresult:
name=x[0]
phno=x[1]
status=x[2]
task=x[3]
print(list1)
return render_template('ngo_profile.html', name=name,phno=phno,status=status,task=task)
@app.route("/update_ngo",methods=['POST'])
def update_ngo():
global mycursor,donor_username
list1 = []
global donor_id;
name= request.form.get('nname')
phno = request.form.get('phno')
status = request.form.get('status')
task = request.form.get('task')
print(name,phno,status,task)
sql="update ngo set name=%s,phno=%s,status=%s,task=%s where ngo_id=%s"
val=(name,phno,status,task,ngo_id)
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
return render_template('ngo_dashboard.html', ngo_username=ngo_username)
print(ngo_id)
@app.route("/donor_profile")
def donor_profile():
global mycursor
list1 = []
global donor_id;
print(donor_id)
sql="select name,phno,colony,city from donor where donor_id=%s"
mycursor.execute(sql,(donor_id,))
myresult = mycursor.fetchall()
name,colony,city="","",""
phno=""
for x in myresult:
name=x[0]
phno=x[1]
colony=x[2]
city=x[3]
print(list1)
return render_template('donor_profile.html', name=name,phno=phno,colony=colony,city=city)
@app.route("/update_donor",methods=['POST'])
def update_donor():
global mycursor,donor_username
list1 = []
global donor_id;
name= request.form.get('dname')
phno = request.form.get('phno')
colony = request.form.get('colony')
city = request.form.get('city')
print(name,phno,colony,city)
sql="update donor set name=%s,phno=%s,colony=%s,city=%s where donor_id=%s"
val=(name,phno,colony,city,donor_id)
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
return render_template('donor_dashboard.html', donor_username=donor_username)
print(donor_id)
@app.route('/logout')
def logout():
return redirect(url_for('login'))
if __name__ == '__main__':
app.run(debug=True)