-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
423 lines (343 loc) · 18.4 KB
/
app.py
File metadata and controls
423 lines (343 loc) · 18.4 KB
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
#----------------------------------------------------------------------------#
# Imports
#----------------------------------------------------------------------------#
from flask import Flask,flash,Blueprint,render_template,request,redirect,session,url_for,abort,send_file,safe_join
from flask_bootstrap import Bootstrap
from flask_mongoalchemy import MongoAlchemy
from flask_uploads import UploadSet,configure_uploads,DOCUMENTS
from models import *
import bcrypt
import logging
from logging import Formatter, FileHandler
import os
import boto3,botocore
from shutil import copyfile, rmtree
import numpy
import difflib
import sys
import subprocess as s
import csv
import stripe
from textanalyser.textanalyser import find
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config.from_object('config')
files = UploadSet('files',DOCUMENTS)
app.config['UPLOADED_FILES_DEST'] = 'static/resumes'
app.config['UPLOADED_FILES_ALLOW']=['doc','docx','pdf','jpg','png']
configure_uploads(app,files)
stripe_keys = {
'secret_key': 'sk_test_zgizVSrtc7DyWDMFPCACErVa',
'publishable_key':'pk_test_GzzeUfMBivRmjuVICl5rpAJZ'
}
stripe.api_key = stripe_keys['secret_key']
s3 = boto3.client('s3')
@app.route('/')
def index():
return render_template('forms/index.html')
### Authentication and Authorization
@app.route('/register/<type>', methods=['POST', 'GET'])
def register(type):
# store hashed password and credentials for POST request
if request.method == 'POST': # if data is being POSTed
if type =='employee':
employees = Employee.query.all()
for employee in employees: # looping through the users
if employee.email == request.form['email']: # check if the entered username matches to avoid collisions
flash('email already exists. Please pick another one')
return redirect(url_for('register',type='employee'))
elif len(request.form['password'])<8: # password length check
flash('Please provide a password which is atleast 8 characters long')
return redirect(url_for('register',type='employee'))
elif request.form['password'] != request.form['repeat_password']: # check passwords match
flash('Passwords mismatch. Please try again')
return redirect(url_for('register',type='employee'))
hashed_password = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt(10)) # hashing the password with a salt
user_data = Employee(email = request.form['email'],password = hashed_password.decode('utf-8'),
first_name = request.form['first_name'],last_name = request.form['last_name'],
resume='',jobs_applied=[],degree = 'Update Most Recent Degree',
area = 'Update Area Details',
institution = 'Update Institution Details',
phone = 'Update Contact',
location ='Update Location`',
skill1 = 'Add Skill',
skill2 = 'Add Skill',
skill3 = 'Add Skill',
skill4 = 'Add Skill',
skill5 = 'Add Skill',
skill6 = 'Add Skill',
skill7 = 'Add Skill',
skill8 = 'Add Skill',
skill9 = 'Add Skill')# storing the hashed password in the collection
user_data.save()
flash('Signup Success!') # flash messages
return redirect(url_for('index'))
# if no exception, go here
elif type == 'employer':
employers = Employer.query.all()
for employer in employers: # looping through the users
if employer.email == request.form['email']: # check if the entered username matches to avoid collisions
flash('email already exists. Please pick another one')
return redirect(url_for('register',type='employer'))
elif len(request.form['password']) < 8: # password length check
flash('Please provide a password which is atleast 8 characters long')
return redirect(url_for('register',type='employer'))
elif request.form['password'] != request.form['repeat_password']: # check passwords match
flash('Passwords mismatch. Please try again')
return redirect(url_for('register',type='employer'))
hashed_password = bcrypt.hashpw(request.form['password'].encode('utf-8'), bcrypt.gensalt(10)) # hashing the password with a salt
user_data = Employer(email = request.form['email'],password = hashed_password.decode('utf-8'),company_name = request.form['company_name'],first_name = request.form['first_name'],last_name = request.form['last_name'],contact_number=request.form['contact_number'],about_company=request.form['about_company'],location = request.form['location'] ,jobs_posted = []) # storing the hashed password in the collection
user_data.save() # save
flash('Signup Success!') # flash messages
return redirect(url_for('index'))
# render form for GET
return render_template('forms/index.html')
@app.route('/login/<type>', methods=['POST'])
def login(type):
if request.method == 'POST':
if type == 'employee': #employee login
employee = Employee.query.filter_by(email=request.form['email']).first()
if employee is None:
flash('No user has registered with this email')
return redirect(url_for('index'))
if bcrypt.hashpw(request.form['password'].encode('utf-8'),employee.password.encode('utf-8')) == employee.password.encode('utf-8'):
session['email'] = request.form['email']
session['user_type'] = type
session['employee_id'] = str(employee.mongo_id)
session['employee_resume'] = employee.resume
return redirect(url_for('employee_dashboard'))
else:
flash('Wrong password entered. Please try again.')
return redirect(url_for('index'))
elif type == 'employer':
employer = Employer.query.filter_by(email=request.form['email']).first()
if employer is None:
return redirect(url_for('index'))
flash('No user registered with the specifed email')
if bcrypt.hashpw(request.form['password'].encode('utf-8'),employer.password.encode('utf-8')) == employer.password.encode('utf-8'):
session['email'] = request.form['email']
session['user_type'] = type
session['employer_id'] = str(employer.mongo_id)
session['employer_company'] = employer.company_name
return redirect(url_for('employer_dashboard'))
else:
flash('Incorrect Credentials Entered')
return redirect(url_for('index'))
### employer routes
@app.route('/employee_dashboard')
def employee_dashboard():
# provision for text extractor and profile maker
employee = Employee.query.filter(Employee.mongo_id == session['employee_id']).first()
resume_data=''
if employee.resume == '':
session['profile_submitted'] = 'unset'
flash('Please submit your resume to apply for jobs')
else:
session['profile_submitted'] = 'set'
return render_template('pages/profile_emp.html',employee=employee,profile_submitted=session['profile_submitted'],resume=employee.resume)
@app.route('/employer_dashboard')
def employer_dashboard():
employer = Employer.query.filter(Employee.mongo_id == session['employer_id']).first()
jobs_posted = Job.query.filter(Job.company_name == employer.company_name ).all()
return render_template('pages/profile_company.html',employer=employer,jobs=jobs_posted)
@app.route('/applicants/')
def applicants():
job_ids = []
jobs = Job.query.filter(Job.company_name == session['employer_company'] ).all()
for job in jobs:
job_ids.append(job.mongo_id)
scores = Scores.query.all()
employees = Employee.query.all()
return render_template('pages/applicants.html',job_ids=job_ids,scores=scores,employees=employees)
### employee routes
@app.route('/update',methods=['POST','GET'])
def update():
employee = Employee.query.filter(Employee.email == session['email']).first()
if request.method=='POST':
employee.degree = request.form['degree']
employee.area = request.form['area']
employee.institution = request.form['institution']
employee.location = request.form['location']
employee.phone = request.form['phone']
employee.skill1 = request.form['skill-1']
employee.skill2 = request.form['skill-2']
employee.skill3 = request.form['skill-3']
employee.skill4 = request.form['skill-4']
employee.skill5 = request.form['skill-5']
employee.skill6 = request.form['skill-6']
employee.skill7 = request.form['skill-7']
employee.skill8 = request.form['skill-8']
employee.skill9 = request.form['skill-9']
employee.save()
return redirect(url_for('employee_dashboard'))
return render_template('pages/profile_emp_edit.html',employee=employee,profile_submitted=session['profile_submitted'],resume=employee.resume)
@app.route('/post_jobs',methods=['POST','GET'])
def post_jobs():
if request.method == 'GET':
return render_template('pages/post_a_job.html')
if request.method =='POST' and 'employer_solution' in request.files:
filename=files.save(request.files['employer_solution'])
file = request.files['employer_solution']
job = Job(company_name = session['employer_company'],skill_1=request.form['skill_1'],skill_2=request.form['skill_2'],skill_3=request.form['skill_3']
,skill_4=request.form['skill_4'],skill_5=request.form['skill_5'],position=request.form['position'],location=request.form['location'],
description=request.form['description'],start_date=request.form['start_date'],apply_date=request.form['apply_date'],duration=int(request.form['duration']),
stipend=int(request.form['stipend']),applicants=[],status='vacant',problem_statement=request.form['problem_statement'],solution=filename)
job.save()
flash('Your job has been posted!')
return redirect(url_for('employer_dashboard'))
@app.route('/resume_uploader',methods=['POST','GET'])
def resume_uploader():
if request.method == 'POST' and 'file' in request.files:
filename = files.save(request.files['file'])
file = request.files['file']
employee = Employee.query.filter(Employee.email == session['email']).first()
employee.resume = filename
employee.save()
flash('Resume uploaded!')
return redirect(url_for('employee_dashboard'))
@app.route('/skill_matcher_job_vacancies',methods=['POST','GET'])
def skill_matcher_job_vacancies():
jobs = Job.query.all()
vacancies = Job.query.filter(Job.status == 'vacant').all()
perc = {}
for job in jobs:
file = open('job_desc.txt','w') #open a job_desc.txt
file.write(job.description) #write job description to it
file.close()# close the file
if os.path.exists('static/CV'):
os.rmdir('static/CV')
os.mkdir('static/CV')# make a directory
copyfile(os.path.join('static/resumes/',session['employee_resume']),os.path.join('static/CV/',session['employee_resume']))# copied file contents
perc[job.description] = int((find('job_desc.txt','static/CV','textanalyser/model')[0][0])*100)
os.remove(os.path.join('static/CV/',session['employee_resume']))
os.rmdir('static/CV')
os.remove('job_desc.txt')
file = open('perc.txt','w')
file.write(str(perc[job.description]))
file.close()
session['skill_matcher'] = 'set'
return render_template('pages/job_vacancies.html',jobs=vacancies,perc=perc)
# upload to local storage
@app.route('/upload_files/<job_id>/<employee_id>',methods=['POST','GET'])
def upload_files(job_id,employee_id):
if request.method == 'POST' and 'answer' in request.files:
filename = files.save(request.files['answer'])
file = request.files['answer']
resume = Employee.query.filter(Employee.mongo_id == session['employee_id']).first().resume
applicant = Applicants(applicant_id = session['employee_id'],resume=resume,percentage_match = session['percentage_match'],job_id=session['job_id'],filename=filename)
applicant.save()
flash('You final submission has been received! You\'ll receive a confirmation mail if you\'ve been selected!')
return redirect(url_for('photo_analysis',job_id=job_id,employee_id = employee_id))
@app.route('/photo_analysis/<job_id>/<employee_id>')
def photo_analysis(job_id,employee_id):
filename = Applicants.query.filter(Applicants.job_id==job_id,Applicants.applicant_id==employee_id).first().filename
employer_solution = Job.query.filter(Job.mongo_id == job_id).first().solution
if os.path.exists('static/employee_solutions'):
rmtree('static/employee_solutions')
os.mkdir('static/employee_solutions') # create a directory for temporary assessment of applicant solutions
if os.path.exists('static/employer_solutions'):
rmtree('static/employer_solutions')
os.mkdir('static/employer_solutions') # create a directory for employer solution
s.call("python3 photoanalysistool0/sliding_window_approach/sliding_window.py -i "+os.path.join(app.config['UPLOADED_FILES_DEST'],filename), shell=True) # Run sliding window algorithm on applicant solution
copyfile('extracted_info.txt','static/employee_solutions/employee_solution.txt') #copy into temporary directory
file1 = 'static/employee_solutions/employee_solution.txt' #plug the path into a python variable
os.remove('extracted_info.txt')
s.call("python3 photoanalysistool0/sliding_window_approach/sliding_window.py -i "+os.path.join(app.config['UPLOADED_FILES_DEST'],employer_solution), shell=True) # Run sliding window algorithm on employer solution
copyfile('extracted_info.txt','static/employer_solutions/employer_solution.txt') # copy into employer sol dir
file2 = 'static/employer_solutions/employer_solution.txt' #plug the path into a python variable
with open(file1, 'r') as myfile:
file1=myfile.read().replace('\n', '')
with open(file2, 'r') as myfile:
file2=myfile.read().replace('\n', '')
perc = difflib.SequenceMatcher(None, file1, file2)
print ('perc is %f' %(perc.ratio()*100)) # percentage difference between answers
score = int(round(perc.ratio()*100,1))
os.remove('extracted_info.txt')
os.remove('static/employee_solutions/employee_solution.txt')
os.rmdir('static/employee_solutions')
os.remove('static/employer_solutions/employer_solution.txt')
os.rmdir('static/employer_solutions')
applicant = Applicants.query.filter(Applicants.job_id==job_id,Applicants.applicant_id==employee_id).first()
score = Scores(applicant_id=employee_id,job_id=job_id,applicant_solution=applicant.filename,score=score,percentage_match=int(session['percentage_match']),job_company=session['job_company'])
score.save()
applicant.save()
flash('Your solution has been successfully submitted. The results will be corressponded to you via mail.')
return redirect(url_for('employee_dashboard'))
@app.route('/vacancies')
def vacancies():
vacancies = Job.query.filter(Job.status == 'vacant').all()
return render_template('pages/job_vacancies.html',jobs = vacancies,perc={})
@app.route('/applied_jobs')
def applied_jobs():
applicant_id = Employee.query.filter(Employee.mongo_id == session['employee_id']).first().mongo_id
applicants = Applicants.query.filter(Applicants.applicant_id == str(applicant_id)).all()
jobs = []
for applicant in applicants:
job = Job.query.filter(Job.mongo_id == applicant.job_id).first()
jobs.append(job)
return render_template('pages/appliedJobs.html',jobs = jobs)
@app.route('/project_details/<job_id>',methods=['POST','GET'])
def project_details(job_id):
job = Job.query.filter(Job.mongo_id == job_id).first()
about_company = Employer.query.filter(Employer.company_name == job.company_name).first().about_company
session['job_id'] = job_id
session['job_company'] = job.company_name
session['percentage_match'] = request.form['percentage_match']
return render_template('pages/projectDetails.html',job = job,job_id=job_id,percentage_match = request.form['percentage_match'],about_company=about_company)
@app.route('/test_portal/<job_id>/<employee_id>',methods=['POST','GET'])
def test_portal(job_id,employee_id):
session['job_id'] = job_id
session['employee_id'] = employee_id
portal_question = Job.query.filter(Job.mongo_id == session['job_id']).first().problem_statement
return render_template('pages/test_screen.html',question = portal_question)
##############
# payment routes
@app.route('/payments',methods=['POST','GET'])
def payments():
return render_template('pages/payment_page.html', key=stripe_keys['publishable_key'])
@app.route('/charge', methods=['GET','POST'])
def charge():
# Amount in cents
amount = 7500
customer = stripe.Customer.create(
email='customer@example.com',
source=request.form['stripeToken']
)
charge = stripe.Charge.create(
customer=customer.id,
amount=amount,
currency='usd',
description='Axizoun Payment'
)
return render_template('pages/charge.html', amount=amount)
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('index'))
@app.route('/team_page')
def team_page():
return render_template('pages/team_page.html')
# Error handlers.
if not app.debug:
file_handler = FileHandler('error.log')
file_handler.setFormatter(
Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')
)
app.logger.setLevel(logging.INFO)
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
app.logger.info('errors')
#----------------------------------------------------------------------------#
# Launch.
#----------------------------------------------------------------------------#
if __name__ == "__main__":
app.run()
# Or specify port manually:
'''
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=80)
'''