-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
773 lines (611 loc) · 28.6 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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators, SelectField
from passlib.hash import sha256_crypt
from functools import wraps
from itsdangerous import URLSafeTimedSerializer
from flask_mail import Mail, Message
from threading import Thread
import datetime
app = Flask(__name__)
app.config['SESSION_TYPE'] = 'memcached'
app.config['SECRET_KEY'] = 'some key'
# Config MySQL
app.config['MYSQL_HOST'] = 'rds host'
app.config['MYSQL_USER'] = 'user'
app.config['MYSQL_PASSWORD'] = 'pass'
# app.config['MYSQL_HOST'] = 'localhost'
# app.config['MYSQL_USER'] = 'root'
# app.config['MYSQL_PASSWORD'] = 'admin'
app.config['MYSQL_DB'] = 'flasktest'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
app.config['MAIL_SERVER'] = 'smtp.zoho.com'
app.config['MAIL_PORT'] = 465
app.config['MAIL_USE_TLS'] = 0
app.config['MAIL_USE_SSL'] = 1
app.config['MAIL_USERNAME'] = 'email'
app.config['MAIL_PASSWORD'] = 'pass'
app.config['MAIL_DEFAULT_SENDER'] = '[email protected]'
# init MySQL
mysql = MySQL(app)
mail = Mail(app)
def send_async_email(msg):
with app.app_context():
mail.send(msg)
def send_email(subject, recipients, text_body, html_body):
msg = Message(subject, recipients=recipients)
msg.body = text_body
msg.html = html_body
thr = Thread(target=send_async_email, args=[msg])
thr.start()
@app.route('/')
def index():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/internships')
def internships():
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles ORDER BY id DESC;")
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)
cur.close()
@app.route('/viewapplications/<string:id>/')
def viewapplications(id):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM application INNER JOIN details ON application.username = details.username WHERE application.internship_id = %s;",([id]))
articles= cur.fetchall()
if(result>0):
return render_template('viewapplications.html', articles = articles)
else:
msg = "No applications found for Internship ID : " + id
flash(msg, 'danger')
return redirect(url_for('dashboard'))
cur.close()
@app.route('/viewusersall-admin')
def viewusersall():
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users LEFT JOIN details ON users.username = details.username order by users.id;")
articles= cur.fetchall()
if(result>0):
return render_template('viewusers.html', articles = articles)
else:
msg = "No users found "
flash(msg, 'danger')
return redirect(url_for('login'))
cur.close()
@app.route('/viewcompanyall')
def viewcompanyall():
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE users.isadmin = 1 order by users.id;")
articles= cur.fetchall()
if(result>0):
return render_template('viewusers.html', articles = articles)
else:
msg = "No users found "
flash(msg, 'danger')
return redirect(url_for('login'))
cur.close()
@app.route('/internship/<string:id>/')
def internship(id):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article= cur.fetchone()
return render_template('article.html', article = article)
@app.route('/confirm/<token>')
def confirm_email(token):
try:
confirm_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
email = confirm_serializer.loads(token, salt='email-confirmation-salt', max_age=3600)
except:
flash('The confirmation link is invalid or has expired.', 'error')
return redirect(url_for('login'))
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE email = %s", [email])
user = cur.fetchone()
if user['email_confirmed']:
flash('Account already confirmed. Please login.', 'info')
else:
cur.execute("UPDATE users SET email_confirmed = %s, email_confirmed_on = %s WHERE email = %s;", [1 , str(datetime.datetime.now()), email])
flash('Thank you for confirming your email address!', 'success')
mysql.connection.commit()
cur.close()
return redirect(url_for('login'))
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
mobile = StringField('Mobile', [validators.Length(min=10, max=13)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message = 'Passwords do not match')
])
confirm = PasswordField('Confirm Password')
@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.lower()
username = form.username.data.lower()
mobile = form.mobile.data
password = sha256_crypt.encrypt(str(form.password.data))
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE email = %s or username = %s", [email, username])
if(result>0):
flash('User Exists with Similar Email or Username', 'danger')
return redirect(url_for('register'))
send_confirmation_email(email)
cur.execute("INSERT INTO users(name, email, username, password, mobile) VALUES(%s, %s, %s, %s, %s)", (name, email, username, password, mobile))
mysql.connection.commit()
flash('Thanks for registering! Please check your email to confirm your email address.', 'success')
return redirect(url_for('login'))
cur.close()
return render_template('register.html', form = form)
def send_confirmation_email(user_email):
confirm_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
token=confirm_serializer.dumps(user_email, salt='email-confirmation-salt')
confirm_url = 'http://www.intern.ecelldtu.in/confirm/' + token
html = render_template('email_confirmation.html', confirm_url=confirm_url)
send_email('Confirm Your Email Address for InternDTU', [user_email], 'Your account on ECELL DTU Internship Portal was successfully created. Please click the link below to confirm your email address and activate your account:', html)
class RegisterCom(Form):
name = StringField('Company Name (Will be visible in Internship listings)', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=100)])
mobile = StringField('Mobile', [validators.Length(min=10, max=13)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message = 'Passwords do not match')
])
confirm = PasswordField('Confirm Password')
@app.route('/registercompany', methods=['GET','POST'])
def registercompany():
form = RegisterCom(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email= form.email.data.lower()
username = form.username.data.lower()
mobile = form.mobile.data
password = sha256_crypt.encrypt(str(form.password.data))
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM users WHERE email = %s or username = %s", [email, username])
if(result>0):
flash('User Exists with Similar Email or Username', 'danger')
return redirect(url_for('registercompany'))
send_confirmation_email(email)
cur.execute("INSERT INTO users(name, email, username, password, isadmin, mobile, resume) VALUES(%s, %s, %s, %s, %s, %s, %s)", (name, email, username, password, 1, mobile, 1))
mysql.connection.commit()
cur.close()
flash('Thanks for registering! Please check your email to confirm your email address.', 'success')
return redirect(url_for('login'))
return render_template('registercom.html', form = form)
class EmailForm(Form):
email = StringField('Email', [validators.Length(min=6, max=50)])
def send_password_reset_email(user_email):
password_reset_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
token = password_reset_serializer.dumps(user_email, salt='password-reset-salt')
password_reset_url = 'http://www.intern.ecelldtu.in/reset/' + token
html = render_template('email_password_reset.html', password_reset_url=password_reset_url)
send_email('Password Reset Requested for InternDTU', [user_email], "You requested that the password for your ECELL DTU Internship Portal (InternDTU) account be reset. Please click the link below to reset your password:", html)
#USer logging
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# Get form fields
username = request.form['username'].lower()
password_candidate = request.form['password']
# create cursor
cur = mysql.connection.cursor()
# get user by username
result = cur.execute("SELECT * FROM users WHERE username = %s", [username])
if result > 0:
# Get stored hash
data = cur.fetchone()
password = data['password']
isadmin = data['isadmin']
resume = data['resume']
email = data['email'].lower()
name = data['name']
#compare Passwords
if sha256_crypt.verify(password_candidate, password):
if data['email_confirmed'] == 0:
send_confirmation_email(email)
error = 'Kindly confirm email Id'
return render_template('login.html', error= error)
# passed
session['logged_in'] = True
session['username'] = username
session['isadmin'] = isadmin
session['email'] = email
session['name'] = name
session['resume'] = resume
flash('You are now logged in', 'success')
if(resume == 0 and isadmin == 0):
return redirect(url_for('details'))
else:
return redirect(url_for('dashboard'))
else:
error = 'Invalid Login'
return render_template('login.html', error= error)
cur.close()
else:
error = 'Username not found'
return render_template('login.html', error= error)
return render_template('login.html')
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('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
class PasswordForm(Form):
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message = 'Passwords do not match')
])
confirm = PasswordField('Confirm Password')
@app.route('/reset/<token>', methods=["GET", "POST"])
def reset_with_token(token):
try:
password_reset_serializer = URLSafeTimedSerializer(app.config['SECRET_KEY'])
email = password_reset_serializer.loads(token, salt='password-reset-salt', max_age=3600)
except:
flash('The password reset link is invalid or has expired.', 'error')
return redirect(url_for('login'))
form = PasswordForm(request.form)
if request.method == 'POST' and form.validate():
cur = mysql.connection.cursor()
# get user by username
password = sha256_crypt.encrypt(str(form.password.data))
result = cur.execute("SELECT * FROM users WHERE email = %s", [email])
article = cur.fetchone()
if result > 0:
cur.execute("UPDATE users SET password = %s WHERE email = %s; ", [password, email])
mysql.connection.commit()
cur.close()
flash('Your password has been updated!', 'success')
return redirect(url_for('login'))
else:
flash('Invalid email address!', 'error')
return render_template('password_reset_email.html', form=form)
return render_template('reset_password_with_token.html', form=form, token=token)
@app.route('/reset', methods=["GET", "POST"])
def reset():
form = EmailForm(request.form)
if request.method == 'POST' and form.validate():
email=form.email.data.lower()
cur = mysql.connection.cursor()
# get user by username
result = cur.execute("SELECT * FROM users WHERE email = %s", [email])
article = cur.fetchone()
if result > 0:
if article['email_confirmed']:
send_password_reset_email(email)
flash('Please check your email for a password reset link.', 'success')
else:
flash('Your email address must be confirmed before attempting a password reset.', 'danger')
return redirect(url_for('login'))
else:
flash('Invalid email address!', 'error')
return render_template('password_reset_email.html', form=form)
return render_template('password_reset_email.html', form=form)
@app.route('/resetadminumang/<id>', methods=["GET", "POST"])
def resetadminumang(id):
form = PasswordForm(request.form)
if request.method == 'POST' and form.validate():
cur = mysql.connection.cursor()
# get user by username
password = sha256_crypt.encrypt(str(form.password.data))
result = cur.execute("SELECT * FROM users WHERE id = %s", [id])
article = cur.fetchone()
if result > 0:
cur.execute("UPDATE users SET password = %s, email_confirmed = %s WHERE id = %s; ", [password, 1, id])
mysql.connection.commit()
cur.close()
flash('Your password has been updated!', 'success')
return redirect(url_for('login'))
else:
flash('Invalid email address!', 'error')
return render_template('password_reset_email.html', form=form)
return render_template('reset_password_with_id.html', form=form, id=id)
class DetailsForm(Form):
title = StringField('Title :', [validators.Length(min=1, max=200)])
body = TextAreaField('About Yourself :', [validators.Length(min=1)])
skills = StringField('Skills :', [validators.Length(min=1, max=200)])
grade = StringField('Grade / CGPA :', [validators.Length(min=1, max=200)])
branch = StringField('Branch, Roll No. :', [validators.Length(min=1, max=200)])
year = StringField('Year :', [validators.Length(min=1, max=10)])
@app.route('/details', methods = ['GET', 'POST'])
@is_logged_in
def details():
form = DetailsForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
body = form.body.data
skills = form.skills.data
grade = form.grade.data
branch = form.branch.data
year = form.year.data
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM details WHERE username = %s", [session['username']])
result1 = cur.execute("SELECT mobile FROM users WHERE username = %s", [session['username']])
data = cur.fetchone()
if(result>0):
flash('Details Filled Already', 'danger')
return redirect(url_for('dashboard'))
cur.execute("INSERT INTO details(title, body, skills, grade, username, name, mobile, email, branch, year) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",(title, body, skills, grade, session['username'], session['name'], data['mobile'], session['email'], branch, year))
cur.execute("UPDATE users SET resume = 1 WHERE username = %s; ", [session['username']])
mysql.connection.commit()
cur.close()
flash('Resume Created', 'success')
return redirect(url_for('dashboard'))
return render_template('details.html', form = form)
@app.route('/edit_details', methods = ['GET', 'POST'])
@is_logged_in
def edit_details():
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM details WHERE username = %s", [session['username']])
article= cur.fetchone()
form = DetailsForm(request.form)
form.title.data = article['title']
form.body.data = article['body']
form.skills.data = article['skills']
form.grade.data = article['grade']
form.branch.data = article['branch']
form.year.data = article['year']
if(article['username'] != session['username']):
flash('Invalid Edit Request', 'danger')
return redirect(url_for('dashboard'))
if request.method == 'POST' and form.validate():
title = request.form['title']
body = request.form['body']
skills = request.form['skills']
grade = request.form['grade']
branch = request.form['branch']
year = request.form['year']
cur = mysql.connection.cursor()
cur.execute("UPDATE details SET title=%s, body = %s, skills = %s, grade = %s, branch = %s, year = %s WHERE username = %s", (title, body, skills, grade, branch, year, session['username']))
mysql.connection.commit()
cur.close()
flash('Details Updated', 'success')
return redirect(url_for('dashboard'))
return render_template('edit_details.html', form = form)
@app.route('/dashboard')
@is_logged_in
def dashboard():
cur = mysql.connection.cursor()
if(session['isadmin'] == 1):
result = cur.execute("SELECT * FROM articles WHERE company = %s or company = %s",[session["name"], session["username"]])
articles = cur.fetchall()
if(result>0):
return render_template('dashboard.html', articles = articles)
else:
msg ='No Internship Posted found'
return render_template('dashboard.html', msg = msg)
cur.close()
else:
result = cur.execute("SELECT * FROM details WHERE username = %s",[session["username"]])
data = cur.fetchone()
if(result>0):
return render_template('dashboard.html', data = data)
else:
msg ='No Details found Kindly go to /details'
return redirect(url_for('details'))
cur.close()
class ArticleForm(Form):
profile = StringField('Profile Information', [validators.Length(min=1, max=500)])
title = StringField('Intern Title', [validators.Length(min=1, max=200)])
skills = StringField('Skills', [validators.Length(min=1, max=200)])
grade = StringField('Stipend', [validators.Length(min=1, max=500)])
info = StringField('About the Company (Website/ Links)', [validators.Length(min=1, max=200)])
@app.route('/add_internship', methods = ['GET', 'POST'])
@is_logged_in
def add_internship():
form = ArticleForm(request.form)
if request.method == 'POST' and form.validate():
title = form.title.data
profile = form.profile.data
skills = form.skills.data
grade = form.grade.data
info = form.info.data
cur = mysql.connection.cursor()
cur.execute("INSERT INTO articles(profile, title, company, skills, grade, info) VALUES(%s, %s, %s, %s, %s, %s)",(profile, title, session['name'], skills, grade, info))
mysql.connection.commit()
cur.close()
flash('Article Created', 'success')
return redirect(url_for('dashboard'))
return render_template('add_article.html', form = form)
class ApplyForm(Form):
whyhire = StringField('Why should the company Hire you?', [validators.Length(min=1, max=200)])
prevexp = StringField('Any Previous Experience related to the profile?', [validators.Length(min=1, max=200)])
skillsrelated = StringField('List out your Skills Related to this profile :', [validators.Length(min=1, max=200)])
other = StringField('Any other info you want to share?', [validators.Length(min=1, max=200)])
@app.route('/apply/<string:id>', methods = ['GET', 'POST'])
@is_logged_in
def apply(id):
form = ApplyForm(request.form)
if session['resume'] == 0:
flash('Kindly fill details before applying', 'success')
return redirect(url_for('details'))
if request.method == 'POST' and form.validate() and session['isadmin'] == 0:
whyhire = form.whyhire.data
prevexp = form.prevexp.data
skillsrelated = form.skillsrelated.data
other = form.other.data
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM application WHERE username = %s AND internship_id = %s", [session['username'], id])
if(result>0):
flash('Already Applied in this Internship', 'danger')
return redirect(url_for('dashboard'))
cur.execute("INSERT INTO application(internship_id, username, whyhire, prevexp, skillsrelated, other) VALUES(%s, %s, %s, %s, %s, %s)",(id, session['username'], whyhire, prevexp, skillsrelated, other))
mysql.connection.commit()
cur.close()
flash('Applied Successfully', 'success')
return redirect(url_for('dashboard'))
elif request.method == 'POST' and session['isadmin'] == 1:
flash('Organisations cannot apply', 'danger')
return redirect(url_for('dashboard'))
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article= cur.fetchone()
return render_template('apply.html', article = article, form = form)
class QuizForm(Form):
name = StringField('Name : ', [validators.Length(min=1, max=200)])
ans = StringField('Answer to Current Question on the Screen :', [validators.Length(min=1, max=200)])
mobile = StringField('Mobile No :', [validators.Length(min=1, max=200)])
email = StringField('Email Id : ', [validators.Length(min=1, max=200)])
@app.route('/quiz', methods = ['GET', 'POST'])
@is_logged_in
def quiz():
form = QuizForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
ans = form.ans.data
mobile = form.mobile.data
email = form.email.data
cur = mysql.connection.cursor()
cur.execute("INSERT INTO quiz(name, username, ans, mobile, email) VALUES(%s, %s, %s, %s, %s)",(name, session['username'], ans, mobile, email))
mysql.connection.commit()
cur.close()
flash('Question Answered Successfully get Ready for the Next Question!', 'success')
return redirect(url_for('quiz'))
return render_template('quiz.html', form = form)
@app.route('/viewquiz')
@is_logged_in
def viewquiz():
cur = mysql.connection.cursor()
if session['isadmin'] != 1:
flash('Invalid Access!', 'danger')
return redirect('/login')
result = cur.execute("SELECT * FROM quiz order by id;")
articles= cur.fetchall()
return render_template('viewquiz.html', articles = articles)
cur.close()
@app.route('/edit_internship/<string:id>', methods = ['GET', 'POST'])
@is_logged_in
def edit_internship(id):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article= cur.fetchone()
form = ArticleForm(request.form)
form.title.data = article['title']
form.profile.data = article['profile']
form.skills.data = article['skills']
form.grade.data = article['grade']
form.info.data = article['info']
cur.close()
if(article['company'].lower() != session['name'].lower() and article['company'].lower() != session['username'].lower()):
flash('Invalid Edit Request', 'danger')
return redirect(url_for('dashboard'))
if request.method == 'POST' and form.validate():
title = request.form['title']
profile = request.form['profile']
skills = request.form['skills']
grade = request.form['grade']
info = request.form['info']
cur = mysql.connection.cursor()
cur.execute("UPDATE articles SET title=%s, profile = %s, skills = %s, grade = %s, info = %s WHERE id = %s", (title, profile, skills, grade, info, id))
mysql.connection.commit()
cur.close()
flash('Internship Updated', 'success')
return redirect(url_for('dashboard'))
return render_template('edit_article.html', form = form)
@app.route('/delete_article/<string:id>', methods= ['POST'])
@is_logged_in
def delete_article(id):
cur = mysql.connection.cursor()
result = cur.execute("SELECT * FROM articles WHERE id = %s", [id])
article= cur.fetchone()
if(article['company'].lower() != session['name'].lower() and article['company'].lower() != session['username'].lower()):
flash('Invalid Delete Request', 'danger')
return redirect(url_for('dashboard'))
cur.execute("DELETE FROM articles WHERE id = %s", [id])
mysql.connection.commit()
cur.close()
flash('Internship Post Deleted', 'success')
return redirect(url_for('dashboard'))
class TicketForm(Form):
name = StringField('Name of Buyer', [validators.Length(min=1, max=200)])
email = StringField('Email Id', [validators.Length(min=1, max=200)])
mobile = StringField('Mobile No.', [validators.Length(min=1, max=200)])
paid = StringField('Amount Paid', [validators.Length(min=1, max=500)])
balance = StringField('Amount Left to Pay', [validators.Length(min=1, max=200)])
@app.route('/add_ticket', methods = ['GET', 'POST'])
@is_logged_in
def add_ticket():
form = TicketForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
mobile = form.mobile.data
paid = form.paid.data
balance = form.balance.data
cur = mysql.connection.cursor()
cur.execute("INSERT INTO ticket(name, email, mobile, paid, balance, seller) VALUES(%s, %s, %s, %s, %s, %s)",(name, email, mobile, paid, balance, session['name']))
mysql.connection.commit()
id = cur.lastrowid
flash('Ticket Added to database successfully at id : ' + str(id), 'success')
return redirect(url_for('add_ticket'))
cur.close()
return render_template('add_ticket.html', form = form, seller = session['name'])
class TicketSearchForm(Form):
choices = [('name', 'Name of Buyer'),
('seller', 'Ticket Seller'),
('mobile', 'Mobile of Buyer'),
('id', 'Ticket Id')]
select = SelectField('Search for Tickets By:', choices=choices)
search = StringField('Search Text:')
@app.route('/search', methods=['GET', 'POST'])
def search():
search = TicketSearchForm(request.form)
if request.method == 'POST':
return search_results(search)
return render_template('search.html', form=search)
@app.route('/results')
def search_results(search):
results = []
search_string = search.data['search']
cur = mysql.connection.cursor()
if search.data['search'] == '':
result = cur.execute("SELECT * FROM ticket order by id;")
else:
if search.data['select'] == 'name':
result = cur.execute("SELECT * FROM ticket where name = %s order by id;", [search_string])
elif search.data['select'] == 'seller':
result = cur.execute("SELECT * FROM ticket where seller = %s order by id;", [search_string])
elif search.data['select'] == 'id':
result = cur.execute("SELECT * FROM ticket where id = %s order by id;", [search_string])
elif search.data['select'] == 'mobile':
result = cur.execute("SELECT * FROM ticket where mobile = %s order by id;", [search_string])
articles= cur.fetchall()
if result == 0:
flash('No results found!', 'danger')
return redirect('/search')
cur.close()
return render_template('viewtickets.html', articles=articles)
@app.route('/viewtickets')
def viewtickets():
cur = mysql.connection.cursor()
if session['isadmin'] != 1:
flash('Invalid Access!', 'danger')
return redirect('/login')
result = cur.execute("SELECT * FROM ticket order by id;")
articles= cur.fetchall()
return render_template('viewtickets.html', articles = articles)
cur.close()
if(__name__ == '__main__'):
app.run(threaded = True , debug = True)