/
web_page.py
665 lines (531 loc) · 21.4 KB
/
web_page.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
import sqlite3
import database as adb
import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from flask import Flask, flash, render_template, request, url_for, redirect, \
g, escape, session, send_from_directory
from werkzeug import secure_filename
#init stuff
DATABASE = 'SICP.db'
PORT = 5003
DEBUG = True
LOGO_FOLDER = r'./static/images/'
UPLOAD_FOLDER = r'/home/icsfsu/FlaskApp/uploads/'
DESC_FOLDER = r'./database/descs/'
RESUME_FOLDER = r'./database/resumes/'
ALLOWED_IMG_EXT = set(['png', 'jpg', 'jpeg', 'gif'])
ALLOWED_TXT_EXT = set(['txt', 'doc', 'docx'])
app = Flask(__name__)
app.config['LOGO_FOLDER'] = LOGO_FOLDER
app.config['LOGO_ACCESS'] = LOGO_FOLDER
app.config['DESC_FOLDER'] = DESC_FOLDER
app.config['RESUME_FOLDER'] = RESUME_FOLDER
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 30 * 1024 #limit for image files
app.secret_key = "asdfq3495basdfbsdpo2451"
#################################
#############WEBPAGE#############
#################################
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/uploads/<path:filename>')
def download_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/uploads/<path:filename>')
def fileResume(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/descriptions/<path:filename>')
def fileDesc(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
@app.route('/')
def main():
app.logger.debug('Main page accessed')
return render_template('index.html')
@app.route('/SICP')
def choice():
app.logger.debug('Main page accessed')
try:
if escape(session['type']) == 'student':
return redirect('/Student/Home')
if escape(session['type']) == 'employer':
return redirect('/Employer/Home')
except:
pass
return render_template('choice.html')
@app.route('/SCIP')
def fix():
return redirect('/SICP')
@app.route('/Employer')
def fixE():
return redirect('Employers')
@app.route('/Student')
def fixS():
return redirect('/Students')
@app.route('/Student/MyApplications')
def fixJ():
return redirect('/Students/View')
@app.route('/logout')
def logout():
session.clear()
return redirect('SCIP')
#######################
#####Student pages#####
#######################
@app.route('/Students')
def student():
try:
if escape(session['type']) == 'student':
return redirect('/Student/Home')
except:
pass
return render_template('students.html')
@app.route('/Student/Login', methods=['GET', 'POST'])
def studentLogin():
try:
if escape(session['type']) == 'student':
return redirect('/Student/Home')
except:
pass
error = None
if request.method == 'POST':
#valid_login not implemented
if valid_login(request.form['email'],
request.form['password'],
True):
session['uname'] = request.form['email']
session['type'] = "student"
return redirect('/Student/Home')
else:
error = 'Invalid username/password'
return render_template('studentlogin.html', error=error)
@app.route('/Student/Register', methods=['GET', 'POST'])
def studentRegister():
try:
if escape(session['type']) == 'student':
return redirect('/Student/Home')
except:
pass
error = None
if request.method == 'POST':
#valid_login not implemented
#returns 0 if successful, 1 if username already taken
#2 if passwords dont match up, 3 if email already taken/invalid
flag = reg_student(request.form['first'] + ' ' + request.form['last'],
request.form['password'],
request.form['cpassword'],
request.form['email'])
if not flag: #log in not implemented
session['uname'] = request.form['email']
session['type'] = 'student'
return redirect('/Student/Home')
elif flag == 1:
error = 'Invalid username/password'
elif flag == 2:
error = 'Invalid e-mail'
elif flag == 4:
error = 'E-mail already in use!'
return render_template('studentregister.html', error=error)
@app.route('/Student/Home')
def studentHome():
try:
if escape(session['type']) == 'student':
return render_template('studenthome.html')
except:
pass
return redirect('/Students')
@app.route('/Student/Search', methods=['GET', 'POST'])
def studentSearch():
if escape(session.get('type')) == 'student':
table=[(i[0], i[4], i[5], i[6], os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"desc{}.txt".format(i[6]))), is_imgfile(i[2]), i[2]) for i in adb.view_cjoini_t() if i[8]]
if request.method == 'POST':#once we have search fields
return render_template('studentsearch.html')
else:
return render_template('studentsearch.html',
table=table,
desc=True)
return redirect('/Students')
@app.route('/Student/Apply/<iid>')
def studentApply(iid):
if escape(session['type']) == 'student':
email = escape(session['uname']) #email of logged in student
if adb.int_isactive(iid) and adb.apply_student(email, iid): #if success, (hasn't already applied)
sid = adb.get_sid(email)
toemail = adb.get_cemail(iid)
send_email(sid, iid, toemail, email)
flash('You have successfully applied to this internship!')
return redirect("/Student/Search")
elif not adb.int_isactive(iid):
flash('Could not apply to this internship.')
return redirect("/Student/Search")
else: #student has already applied
flash('You have already applied to this internship!', 'error')
return redirect("/Student/Search")
return escape(session['type'])
@app.route('/Student/Resume', methods=['GET', 'POST'])
def studentResume():
if escape(session['type']) == 'student':
if request.method == 'GET':
sid = adb.get_sid(escape(session['uname']))
if get_txtfile(sid):
#txtfile = open(get_txtfile(sid))
return render_template('studenteditresume.html', sid=sid, ext=get_txtext(get_txtfile(sid)))
else:
return render_template('studentaddresume.html')
if request.method == 'POST':
#use the logged in uname(email) and position name
#to create position in db module
sid = adb.get_sid(escape(session['uname']))
txtfile = request.files['txt_file']
if txtfile and allowed_text(txtfile.filename):
fname = "resume{}.{}".format(sid, get_txtext(txtfile.filename))
txtfile.save(os.path.join(app.config['UPLOAD_FOLDER'],
fname))
flash("Successfully added your resume!")
return redirect('/Student/Home')
else:
flash("Could not add your resume")
return redirect('/Student/Home')
return redirect('/Student')
@app.route('/Student/View')
def studentViewApps():
if escape(session['type']) == 'student':
email = escape(session['uname']) #email of logged in student
sid = adb.get_sid(escape(session['uname']))
jobs = adb.get_jobs(sid)
table=[(i[0], i[4], i[5], i[6],
os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"desc{}.txt".format(i[6]))),
is_imgfile(i[2]), i[2]) for i in jobs if i[8]]
return render_template('studentview.html', table=table)
return redirect('/Student')
########################
#####Employee pages#####
########################
@app.route('/Employers')
def employer():
try:
if escape(session['type']) == 'employer':
return redirect('/Employer/Home')
except:
pass
return render_template('employers.html')
@app.route('/Employers/Login', methods=['GET', 'POST'])
def employerLogin():
try:
if escape(session['type']) == 'employer':
return redirect('/Employer/Home')
except:
pass
error = None
if request.method == 'POST':
#valid_login not implemented
if valid_login(request.form['email'],
request.form['password'],
False):
session['uname'] = request.form['email']
session['type'] = "employer"
return redirect('/Employer/Home') #log in not implemented
else:
error = 'Invalid username/password'
return render_template('employerlogin.html', error=error)
@app.route('/Employers/Register', methods=['GET', 'POST'])
def employerRegister():
try: #if already logged in as employer go straight to home
if escape(session['type']) == 'employer':
return redirect('/Employer/Home')
except:
pass
error = None
if request.method == 'POST':
#returns 0 if successful, 1 if username already taken
#2 if passwords dont match up, 3 if email already taken/invalid
flag = reg_employer(request.form['username'],
request.form['password'],
request.form['cpassword'],
request.form['email'])
if not flag: #log in not implemented
session['uname'] = request.form['email']
session['type'] = "employer"
imgfile = request.files['img_file']
if imgfile and allowed_image(imgfile.filename):
cid = adb.get_cid(escape(session['uname']))
fname = "logo{}.{}".format(cid, 'jpg')
imgfile.save(os.path.join(app.config['UPLOAD_FOLDER'],
fname))
flash("Successfully added your logo!")
else:
flash("Could not add logo")
return log_employer_in('email')
elif flag == 1:
error = 'Invalid username/password'
elif flag == 2:
error = 'Invalid e-mail'
elif flag == 3:
error = 'Company or E-mail already in use!'
return render_template('employerregister.html', error=error)
@app.route('/Employer/Home')
def employerHome():
try:
if escape(session['type']) == 'employer':
return render_template('employerhome.html')
except:
pass
return redirect('/Employers')
@app.route('/Employer/EditLogo', methods=['GET', 'POST'])
def employerEditLogo():
if escape(session['type']) == 'employer':
if request.method == 'GET':
cid = adb.get_cid(escape(session['uname']))
if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"logo{}.jpg".format(cid))):
return render_template('employereditlogo.html',
imgpath=app.config['UPLOAD_FOLDER'] +
"logo{}.jpg".format(cid), cid=cid)
else:
return render_template('employeraddlogo.html', cid=cid)
if request.method == 'POST':
#use the logged in uname(email) and position name
#to create position in db module
cid = adb.get_cid(escape(session['uname']))
imgfile = request.files['img_file']
if imgfile and allowed_image(imgfile.filename):
fname = "logo{}.{}".format(cid, 'jpg')
imgfile.save(os.path.join(app.config['UPLOAD_FOLDER'],
fname))
flash("Successfully added your logo!")
return redirect('/Employer/Home')
else:
flash("Could not add image file")
return redirect('/Employer/Home')
return 'employer'
@app.route('/Employer/AddInternships', methods=['GET', 'POST'])
def employerAddInt():
try:
if escape(session['type']) == 'employer':
if request.method == 'POST':
#use the logged in uname(email) and position name
#to create position in db module
iid = adb.add_internship(session['uname'], request.form['posname'])
txtfile = request.files['txt_file']
if txtfile and allowed_text(txtfile.filename):
fname = "desc{}.{}".format(iid, "txt")
txtfile.save(os.path.join(app.config['UPLOAD_FOLDER'],
fname))
flash("Successfully added your internship!")
return redirect('/Employer/Home')
else:
flash("Could not add description file")
return redirect('/Employer/Home')
return render_template('employeraddinternships.html')
except:
pass
return redirect('/Employers')
@app.route('/Employer/ViewInternships')
def employerViewInt():
if escape(session['type']) == 'employer':
sid = request.args.get('sid')
iid = request.args.get('iid')
cid = adb.get_cid(escape(session['uname']))
if sid and iid:
if adb.check_ci_ids(cid, iid):
adb.student_seen(sid, iid)
flash('Student has been removed.')
return redirect('/Employer/ViewInternships/{}'.format(iid))
internlist = []
for r in adb.view_cjoini_t():
if r[2] == cid and r[8]:
internlist.append(r)
return render_template('employerviewinternships.html', internlist=internlist)
return 'employer'
@app.route('/Employer/ViewInternships/<iid>')
def employerViewSpecificInt(iid):
if escape(session['type']) == 'employer':
cid = adb.get_cid(escape(session['uname']))
if adb.check_ci_ids(cid, iid):
studs = students_applied(iid)
sname, iname = adb.get_name(iid=iid)
return render_template('employerviewinternshipsiid.html', students=studs, iid=iid, posname=iname)
return redirect('/Employer')
@app.route('/Employer/View/<sid>')
def employerViewResume(sid):
if escape(session['type']) == 'employer':
try:
return render_template('employerviewresume.html', sid=sid, ext=get_txtext(get_txtfile(sid)))
except:
flash('Could not open resume')
return render_template('employerviewinternships.html')
return redirect('Employer')
@app.route('/Employer/ViewInternships/ChangeDescription/<iid>', methods=['POST', 'GET'])
def employerChangeDescription(iid):
if escape(session['type']) == 'employer':
cid = adb.get_cid(escape(session['uname']))
if adb.check_ci_ids(cid, iid):
if request.method == 'GET':
if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"desc{}.txt".format(iid))):
txtfile = open(os.path.join(app.config['UPLOAD_FOLDER'],
"desc{}.txt".format(iid)))
return render_template('employereditdescription.html',
txt=txtfile.read())
else:
return render_template('employeradddescription.html')
if request.method == 'POST':
#use the logged in uname(email) and position name
#to create position in db module
txtfile = request.files['txt_file']
if txtfile and allowed_text(txtfile.filename):
fname = "desc{}.{}".format(iid, 'txt')
txtfile.save(os.path.join(app.config['UPLOAD_FOLDER'],
fname))
flash("Successfully added your description!")
return redirect('/Employer/ViewInternships')
else:
flash("Could not add your description.")
return redirect('/Employer/ViewInternships')
return redirect('/Employer')
@app.route('/Employer/ViewInternships/Delete/<iid>')
def employerDeleteInt(iid):
if escape(session['type']) == 'employer':
cid = adb.get_cid(escape(session['uname']))
if adb.check_ci_ids(cid, iid) and adb.int_isactive(iid):
adb.int_makeinactive(iid)
flash("Deleted your internship.")
return redirect('/Employer/ViewInternships')
return redirect('/Employer')
#####Non-routing functions#####
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = connect_to_database()
return db
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
def valid_login(email, password, student = True):
if student and adb.student_login(email, password):
return True
if not student and adb.company_login(email, password):
return True
return False
def log_employer_in(username):
return redirect('/Employer/Home')
def log_student_in(username):
return redirect('/Student/Home')
def get_txtfile(sid): #returns path of text file
if os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.txt".format(sid))):
return os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.txt".format(sid))
elif os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.doc".format(sid))):
return os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.doc".format(sid))
elif os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.docx".format(sid))):
return os.path.join(app.config['UPLOAD_FOLDER'], "resume{}.docx".format(sid))
else:
return False
def get_txtext(filename):
return filename.rsplit('.', 1)[1]
def is_imgfile(iid):
jpg = os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"logo{}.jpg".format(iid)))
png = os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"logo{}.png".format(iid)))
gif = os.path.isfile(os.path.join(app.config['UPLOAD_FOLDER'],
"logo{}.gif".format(iid)))
return jpg or png or gif
#need to check if email is a possible valid email
#need to have some checks on arguments
def reg_employer(name, password, cpassword, email):
#return 1 if passwords not equal error
if password != cpassword:
return 1
if '@' not in email:
return 2
for r in adb.view_company_t(): #companies can't have same name
if name == r[0] or email == r[4]:
return 3
else:
adb.add_company(name, password, email)
return False
def reg_student(name, password, cpassword, email):
#return 1 if passwords not equal error
if password != cpassword:
return 1
if '@' not in email:
return 2
for r in adb.view_student_t(): #students cant have same email
if email == r[4]:
return 4
else:
adb.add_student(name, password, email)
return False
def students_applied(iid):
students = []
for r in adb.view_student_t():
if r[4][int(iid)] == '1':
students.append(r)
return students
def allowed_image(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_IMG_EXT
def allowed_text(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_TXT_EXT
def send_email(sid, iid, to, cc,
smtpserver='smtp.gmail.com:587'):
from_addr = "sicp.ad.2015@gmail.com"
login = "sicp.ad.2015@gmail.com"
password = "asdfq3495"
subject = "SICP Application Received"
#to = "sicp.ad.2015@gmail.com"
sname, iname = adb.get_name(sid, iid)
message = """Hello,\nAn applicant, {}, has applied to your position {}. Please review this application on your home page.\n\nThank you,\nSICP Administration""".format(sname, iname)
msg = MIMEMultipart()
msg['From'] = from_addr
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP(smtpserver)
server.ehlo()
server.starttls()
server.ehlo()
server.login(login, password)
text = msg.as_string()
problems = server.sendmail(from_addr, to, text)
server.quit()
####################################################
###########UTILITY DEBUGGING FUNCTIONS##############
####################################################
@app.route('/Admin/RecreateDB')
def adminDB():
adb.create_db()
return 'done'
@app.route('/Admin/ViewT')
def adminSType():
return session['type']
@app.route('/Admin/ViewC')
def adminViewC():
s = ""
for r in adb.view_company_t():
s += str(r) + '\n'
return s
@app.route('/Admin/ViewS')
def adminViewS():
s = ""
for r in adb.view_student_t():
s += str(r) + '\n'
return s
@app.route('/Admin/ViewI')
def adminViewI():
s = ""
for r in adb.view_internship_t():
s += str(r) + '\n'
return s
if __name__ == "__main__":
app.logger.setLevel(0)
app.run(port=PORT, debug=False)