forked from SHK4869/-TryCatchBruteForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
587 lines (499 loc) · 18.1 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
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
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
#from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
import MySQLdb# I was using it like this
from functools import wraps
import math
import json
app = Flask(__name__)
# Config MySQL
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'kjcse'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
# init MYSQL
mysql = MySQL(app)
#Articles = Articles()
# import necessary packages
try:
CONN = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'password', db = 'kjcse')
CURSOR = CONN.cursor()
CURSOR = CONN.cursor(MySQLdb.cursors.DictCursor)
except Exception as e:
print(e)
exit()
# Index
@app.route('/')
def index():
return render_template('home.html')
# Check if user logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
@app.route('/student')
@is_logged_in
def student_data():#navbar student ka hoga in card layout
return render_template('student.html')
@app.route('/student_track')
@is_logged_in
def student_track():
cur = mysql.connection.cursor()
data = {}
s1 = []
s2 = []
s3 = []
# Get articles8
#result = cur.execute("SELECT * FROM student")
if(cur.execute("SELECT s1 from student where login = %s", session["username"])):
s1 = cur.execute("SELECT attendance, tot_att FROM s1 where student_id = %s", session["username"])
s1 = cur.fetchall()
print(s1)
if int(s1[0]['attendance'])/int(s1[0]['tot_att']) * 100 < 70:
data['s1'] = str(math.ceil(70/100* int(s1[0]['tot_att'])/int(s1[0]['attendance'])))
else:
data['s1'] = "safe"
if(cur.execute("SELECT s2 from student where login = %s", session["username"])):
s2 = cur.execute("SELECT attendance, tot_att FROM s2 where student_id = %s", session["username"])
s2 = cur.fetchall()
print(s2)
print(int(s2[0]['attendance'])/int(s2[0]['tot_att']) *100)
if int(s2[0]['attendance'])/int(s2[0]['tot_att']) * 100 < 70:
data['s2'] = str(math.ceil(70/100* int(s2[0]['tot_att'])/int(s2[0]['attendance'])))
else:
data['s2'] = "safe"
if(cur.execute("SELECT s3 from student where login = %s", session["username"])):
s3 = cur.execute("SELECT attendance, tot_att FROM s3 where student_id = %s", session["username"])
s3 = cur.fetchall()
print(s3)
if int(s3[0]['attendance'])/int(s3[0]['tot_att']) * 100 < 70:
data['s3'] = str(math.ceil(70/100* int(s3[0]['tot_att'])/int(s3[0]['attendance'])))
else:
data['s3'] = "safe"
print(data)
return render_template('student_track.html', data=data)
# Close connection
cur.close()
@app.route('/student_committee')
@is_logged_in
def student_committee():
cur = mysql.connection.cursor()
data = {}
c1 = []
# Get articles
#result = cur.execute("SELECT * FROM student")
if(cur.execute("SELECT c1 from student where login = %s", session["username"])):
c1 = cur.execute("SELECT m1,m2 FROM c1 where c_id = 1 ")
c1 = cur.fetchall()
data['c1'] = c1
print(data['c1'])
if(cur.execute("SELECT c2 from student where login = %s", session["username"])):
c2 = cur.execute("SELECT m1,m2 FROM c1 where c_id = 2")
c2 = cur.fetchall()
data['c2'] = c2
print(data['c2'])
"""
if(cur.execute("SELECT c2 from student where login = %s", session["username"])):
data['c2'] = cur.execute("SELECT m1,m2 FROM c2")
data['c2'] = cur.fetchall()
if data['c2'] == "":
del data['c2']
"""
if len(data)!=0:
return render_template('student_committee.html', data=data)
else:
msg = 'No Notes'
return render_template('student_committee.html', msg=msg)
# Close connection
cur.close()
@app.route('/student_notes')
@is_logged_in
def student_notes():
cur = mysql.connection.cursor()
data = {}
s1=[]
s2=[]
s3=[]
# Get articles
#result = cur.execute("SELECT * FROM student")
if(cur.execute("SELECT notes from s1 where student_id = %s", session["username"])):
s1 = cur.execute("SELECT notes FROM s1 ")
s1= cur.fetchall()
data['s1'] = s1
print(data['s1'])
#if data['s1'] == "":
# del data['s1']
##return render_template('student_notes.html',s1=s1)
if(cur.execute("SELECT notes from s2 where student_id = %s",session["username"])):
s2 = cur.execute("SELECT notes FROM s2 ")
s2= cur.fetchall()
data['s2'] = s1
print(data['s2'])
"""
data['s2'] = cur.execute("SELECT notes FROM s1 LIMIT 1")
data['s2'] = cur.fetchall()
if data['s1'] == "":
del data['s1']
"""
if(cur.execute("SELECT notes from s3 where student_id = %s", session["username"])):
s3 = cur.execute("SELECT notes FROM s3 ")
s3= cur.fetchall()
data['s3'] = s1
print(data['s3'])
if len(data)!=0:
return render_template('student_notes.html', data=data)
else:
msg = 'No Notes'
return render_template('student_notes.html', msg=msg)
# Close connection
cur.close()
@app.route('/student_submissions')
@is_logged_in
def student_submissions():
cur = mysql.connection.cursor()
data = {}
s1 = []
s2 = []
s3 = []
# Get articles
#result = cur.execute("SELECT * FROM student")
if(cur.execute("SELECT submission from s1 where student_id = %s", session["username"])):
s1= cur.execute("SELECT submission FROM s1")
s1 = cur.fetchall()
data['s1'] = s1
if(cur.execute("SELECT submission from s2 where student_id = %s", session['username'])):
s2 = cur.execute("SELECT submission FROM s1")
s2 = cur.fetchall()
data['s2'] = s2
if(cur.execute("SELECT submission from s3 where student_id = %s", session['username'])):
s3 = cur.execute("SELECT submission FROM s1")
s3 = cur.fetchall()
data['s3'] = s3
print(data)
if len(data)!=0:
return render_template('student_submissions.html', data=data)
else:
msg = 'Hurray, no submissions!'
return render_template('student_submissions.html', msg=msg)
# Close connection
cur.close()
# About
@app.route('/about')
def about():
return render_template('about.html')
# Articles
@app.route('/articles')
def articles():
# Create cursor
cur = mysql.connection.cursor()
# Get articles
result = cur.execute("SELECT * FROM articles")
articles = cur.fetchall()
if result > 0:
return render_template('articles.html', articles=articles)
else:
msg = 'No Articles Found'
return render_template('articles.html', msg=msg)
# Close connection
cur.close()
#Single Article
@app.route('/article/<string:id>/')
def article(id):
# Create cursor
cur = mysql.connection.cursor()
# Get article
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article = cur.fetchone()
return render_template('article.html', article=article)
# Register Form Class
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('UserID', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match')
])
confirm = PasswordField('Confirm Password')
class1 = StringField('Class 1')
class2 = StringField('Class 2')
class3 = StringField('Class 3')
c1 = StringField('Committee Notifications 1')
c2 = StringField('Committee Notifications 2')
# User Register
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = form.password.data
class1 = form.class1.data
class2 = form.class2.data
class3 = form.class3.data
c1 = form.c1.data
c2 = form.c2.data
# Create cursor
cur = mysql.connection.cursor()
# Execute query
cur.execute("INSERT INTO student(login, password, s1, s2, s3,c1,c2) VALUES(%s, %s, %s, %s, %s, %s, %s)", (username, password, class1, class3, class3, c1,c2))
# Commit to DB
mysql.connection.commit()
# Close connection
cur.close()
flash('You are now registered and can log in', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
# User login
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get Form Fields
username = request.form['username']
password_candidate = request.form['password']
# Create cursor
cur = mysql.connection.cursor()
# Get user by username
result = cur.execute("SELECT * FROM student WHERE login = %s", [username])
if result > 0:
# Get stored hash
data = cur.fetchone()
password = data['password']
# print(sha256_crypt.checksum)
# print(password_candidate)
# print(password)
# Compare Passwords
if password == password_candidate:
# Passed
session['logged_in'] = True
session['username'] = username
flash('You are now logged in', 'success')
return redirect(url_for('student_submissions'))
else:
error = 'Invalid login'
return render_template('login.html', error=error)
# Close connection
cur.close()
else:
error = 'Username not found'
return render_template('login.html', error=error)
return render_template('login.html')
# Logout
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
@app.route('/teacher_dashboard')
@is_logged_in
def dashboard():
result=CURSOR.execute("SELECT notes FROM teacher")## NOTES TABLE
notes=CURSOR.fetchall()
print( notes)
sub_result=CURSOR.execute("SELECT submission FROM teacher")
sub=CURSOR.fetchall()
return render_template('teacher_dashboard.html',notes=notes,sub=sub)
class Notes(Form):
title= StringField('Notes')
@app.route('/add_submission',methods=['GET','POST'])
@is_logged_in
def add_submission():
form=Notes(request.form)
if request.method=='POST':
teacher_name=session['username']
notes=form.title.data
table_name = ""
subject_name=("SELECT subject from teacher WHERE login=%s",[teacher_name])
if(subject_name==1):
table_name = "s1"
if(subject_name==2):
table_name = "s2"
if(subject_name==3):
table_name = "s3"
CURSOR.execute("UPDATE %s SET submission=%s",(table_name,notes))
CONN.commit()
CURSOR.execute("UPDATE teacher SET submission=%s WHERE login=%s",(notes,teacher_name))
CONN.commit()
flash("Submission Added",'success')
return redirect(url_for('dashboard'))
return render_template("add_submission.html",form=form)
@app.route('/mark',methods=['GET','POST'])
@is_logged_in
def mark():
if request.method=='POST':
#print request.form.getlist('check_list')
return redirect(url_for('dashboard'))
#teacher_name=session['username']
#subject_name=CURSOR.execute("SELECT subject FROM teacher where login=%s",[teacher_name])
#student_details=CURSOR.execute("SELECT student_id FROM %s",(subject_name))
else:
student_details=CURSOR.execute("SELECT student_id FROM s1")
student_details=CURSOR.fetchall()
print( student_details)
return render_template('mark.html',student_details=student_details)
@app.route('/add_notes',methods=['GET','POST'])
@is_logged_in
def add_notes():
form=Notes(request.form)
if request.method=='POST':
teacher_name=session['username']
#teacher_name="1"
notes=form.title.data
subject_name=("SELECT subject from teacher WHERE login=%s",[teacher_name])
#CURSOR.execute("UPDATE s%s SET notes=%s",(subject_name,notes))
#CONN.commit()
CURSOR.execute("UPDATE teacher SET notes=%s WHERE login=%s",(notes,teacher_name))
CONN.commit()
flash("Notes Added",'success')
return redirect(url_for('dashboard'))
return render_template('add_notes.html',form=form)
class RegisterFromTeacher(Form):
login= StringField('Username',[validators.Length(min=1,max=20)])
password= PasswordField ('Password',[validators.DataRequired(),validators.EqualTo('confirm',message='password do not match')])
confirm=PasswordField('Confirm Password')
subject=StringField('Subject',[validators.Length(min=1,max=20)])
@app.route('/teacher_register',methods=['GET','POST'])
def register_teacher():
form =RegisterFrom(request.form)
if request.method=='POST' and form.validate():
login=form.login.data
password=form.password.data
subject=form.subject.data
# is there something wrong here?
#curso
#cur=mysql.connection.cursor()
CURSOR.execute("INSERT into teacher (login,password,subject) values(%s,%s,%s)",(login,password,subject))
#commit
CONN.commit()
#thats it??
#do it wherever you hav conn, cursor
#just here
#mysql.connection.commit()
#close connection
#cur.close()
#show form html template
flash("RESGISTRATION SUCCESSFULL",'success')
return redirect(url_for('dashboard'))
return render_template('teacher_register.html',form=form)
@app.route('/teacher_login',methods=['GET','POST'])
def login_teacher():
if request.method=='POST':
login=request.form['login']
password_given=request.form['password']
result=CURSOR.execute("SELECT * FROM teacher WHERE login=%s",[login])
if result>0:
data=CURSOR.fetchone() ## gets first row only
password=data['password']
if(password==password_given):
session['logged_in']=True
session['username']=login
flash('You are successfully logged in','success')
return redirect(url_for('dashboard'))
else:
error='Invalid password'
return render_template('teacher_login.html',error=error)
else:
error='No User Found'
return render_template('teacher_login.html',error=error)
return render_template('teacher_login.html')
""""
# Dashboard
@app.route('/dashboard')
@is_logged_in
def dashboard():
# Create cursor
cur = mysql.connection.cursor()
return redirect(url_for('dashboard'))
"""
"""
# Get articles
result = cur.execute("SELECT * FROM articles")
articles = cur.fetchall()
if result > 0:
return render_template('das*hboard.html', articles=articles)
else:
msg = 'No Articles Found'
return render_template('dashboard.html', msg=msg)
# Close connection
cur.close()
# Article Form Class
class ArticleForm(Form):
title = StringField('Title', [validators.Length(min=1, max=200)])
body = TextAreaField('Body', [validators.Length(min=30)])
# Add Article
@app.route('/add_article', methods=['GET', 'POST'])
@is_logged_in
def add_article():
form = ArticleForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
body = form.body.data
# Create Cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("INSERT INTO articles(title, body, author) VALUES(%s, %s, %s)",(title, body, session['username']))
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
flash('Article Created', 'success')
return redirect(url_for('dashboard'))
return render_template('add_article.html', form=form)
"""
# Edit Article
@app.route('/edit_article/<string:id>', methods=['GET', 'POST'])
@is_logged_in
def edit_article(id):
# Create cursor
cur = mysql.connection.cursor()
# Get article by id
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article = cur.fetchone()
cur.close()
# Get form
form = ArticleForm(request.form)
# Populate article form fields
form.title.data = article['title']
form.body.data = article['body']
if request.method == 'POST' and form.validate():
title = request.form['title']
body = request.form['body']
# Create Cursor
cur = mysql.connection.cursor()
app.logger.info(title)
# Execute
cur.execute ("UPDATE articles SET title=%s, body=%s WHERE id=%s",(title, body, id))
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
flash('Article Updated', 'success')
return redirect(url_for('dashboard'))
return render_template('edit_article.html', form=form)
# Delete Article
@app.route('/delete_article/<string:id>', methods=['POST'])
@is_logged_in
def delete_article(id):
# Create cursor
cur = mysql.connection.cursor()
# Execute
cur.execute("DELETE FROM articles WHERE id = %s", [id])
# Commit to DB
mysql.connection.commit()
#Close connection
cur.close()
flash('Article Deleted', 'success')
return redirect(url_for('dashboard'))
if __name__ == '__main__':
app.secret_key='secret123'
app.run(debug=True)