-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
674 lines (566 loc) · 18.5 KB
/
views.py
File metadata and controls
674 lines (566 loc) · 18.5 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
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
from flask import Blueprint, request,session
import os
from dotenv import load_dotenv
import psycopg2
import bcrypt
# get enviroment variables
load_dotenv()
views = Blueprint(__name__, "views")
# delimeters for sending info back
delimiter = "!@#"
delimiter2 = "$%^"
# route for creating a new user, send role, username and password
# returns id
@views.route("/createUser", methods=["POST"])
def createUser():
conn = openConnect()
cursor = conn.cursor()
data = request.json
role = data.get('role')
username = data.get('username')
password = data.get('password')
salt = bcrypt.gensalt()
# get rid of
hpassword = bcrypt.hashpw(str(password).encode('utf-8'), salt)
password = hpassword.decode('utf-8')
check = 'SELECT * FROM users WHERE username = %s'
cursor.execute(check,(username,))
result = cursor.fetchone()
if result is not None:
return "Username Already Exists!"
insert = 'INSERT INTO users (role, username, password) VALUES (%s, %s, %s)'
cursor.execute(insert, (role,username,password))
check = 'SELECT * FROM users WHERE username = %s '
cursor.execute(check, (username,))
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
return str(result[0])
# route to login
# send username and password
# returns id
@views.route('/login', methods=["POST"])
def login():
conn = openConnect()
cursor = conn.cursor()
data = request.json
username = data.get('username')
password = data.get('password')
check = 'SELECT * FROM users WHERE username = %s '
cursor.execute(check, (username,))
result = cursor.fetchone()
cursor.close()
conn.close()
# username doesn't exist
if result is None:
return "Username or Password is incorrect!"
hashed_password = result[3]
if bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8')):
session['username'] = username
session['role'] = result[1]
return str(result[0])
return "Username or Password is incorrect!"
# route to change password
# send username and password
# send back message
@views.route('/changePassword', methods=['POST'])
def changePassword():
if not verifyLogin():
return "Access Denied"
conn = openConnect()
cursor = conn.cursor()
username = request.json.get('username')
password = request.json.get('password')
salt = bcrypt.gensalt()
hpassword = bcrypt.hashpw(str(password).encode('utf-8'), salt)
password = hpassword.decode('utf-8')
update = '''
UPDATE users
SET password = %s
WHERE username = %s '''
cursor.execute(update, (password,username))
cursor.close()
conn.commit()
conn.close()
return 'Password changed'
# check if a user is logged in
@views.route('/verifyLogin', methods=["GET"])
def home():
if verifyLogin():
return f"{session.get('username')} is logged in"
return "Access Denied"
# send id, or empty (for current user)
# get back all info for a user account
@views.route('/myAccount', methods=["GET"])
def myAccount():
#or session.get('role') != 'applicant'
if not verifyLogin():
return "Access Denied"
username = session.get('username')
if(request.args.get('username')):
username = request.args.get('username')
conn = openConnect()
cursor = conn.cursor()
select = 'SELECT user_id, name, address, phone, email, about_me, workhistory, education from user_info join users ON user_info.user_id = users.id WHERE username = %s'
cursor.execute(select, (username,))
result = cursor.fetchone()
cursor.close()
conn.close()
response = ""
# name address phone email aboutme
if result:
for row in result:
response += str(row) + delimiter
return response
# send employer id, get back company name
@views.route('/getCompanyName', methods=["GET"])
def getCompanyName():
if not verifyLogin():
return "Access Denied"
id = request.args.get('id')
conn = openConnect()
cursor = conn.cursor()
select = 'SELECT company_name FROM employer_info WHERE employer_user_id = %s'
cursor.execute(select, (id,))
result = cursor.fetchone()
if result is None:
return 'None'
cursor.close()
conn.close()
return str(result[0])
# send id, get back employer or applicant
@views.route('/getRole', methods=["GET"])
def getRole():
if not verifyLogin():
return "Access Denied"
id = request.args.get('id')
conn = openConnect()
cursor = conn.cursor()
select = 'SELECT role FROM users WHERE id = %s'
cursor.execute(select, (id,))
result = cursor.fetchone()
cursor.close()
conn.close()
return str(result[0])
# get back all jobs
# send nothing for all active jobs
# send active=false for all inactive jobs
#send active=all for both
@views.route('/allJobs', methods=["GET"])
def allJobs():
if not verifyLogin():
return "Access Denied"
active = request.args.get('active')
if active == 'all':
active = 'true or active = false'
select = f'''SELECT job_posting.id, employer_id, job_title, description, salary, type,location
FROM job_posting JOIN users
ON job_posting.employer_id = users.id
JOIN employer_info
ON employer_user_id = users.id
WHERE active ={active if active != None else True}'''
conn = openConnect()
cursor = conn.cursor()
cursor.execute(select)
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# send job id and get back info for the job
@views.route('/oneJob', methods=["GET"])
def oneJob():
if not verifyLogin():
return "Access Denied"
#/oneJob?id=<id>
id = request.args.get('id')
select = '''SELECT job_posting.id,job_title, description, salary, type, location
FROM job_posting JOIN users
ON job_posting.employer_id = users.id
JOIN employer_info
ON employer_user_id = users.id
WHERE job_posting.id = %s'''
conn = openConnect()
cursor = conn.cursor()
cursor.execute(select,(id,))
result = cursor.fetchone()
cursor.close()
conn.close()
response = ""
print(result)
if result:
for row in result:
response += str(row) + delimiter
else:
return "Job doesn't exist"
return response
# send job id and change status of job (active or inactive)
@views.route('/activeJob', methods=["GET"])
def activeJob():
if not verifyLogin():
return "Access Denied"
id = request.args.get('id')
active = request.args.get('active')
update = 'UPDATE job_posting SET active = %s WHERE id = %s'
conn = openConnect()
cursor = conn.cursor()
cursor.execute(update, (active,id))
cursor.close()
conn.commit()
conn.close()
return 'Job updated'
# send job id and can change fields in a job posting
@views.route('/updatePosting', methods=["POST"])
def updatePosting():
if not verifyLogin():
return "Access Denied"
id = request.json.get('id')
active = request.json.get('active')
salary = request.json.get('salary')
job_title = request.json.get('job_title')
description = request.json.get('description')
type = request.json.get('type')
update = '''
UPDATE job_posting
SET active = %s,
salary = %s,
job_title = %s,
description = %s,
type = %s
WHERE id = %s'''
conn = openConnect()
cursor = conn.cursor()
cursor.execute(update, (active,salary,job_title,description,type,id))
cursor.close()
conn.commit()
conn.close()
return 'Job updated'
# send user id and can update a users profile
@views.route('/updateProfile', methods=['POST'])
def updateProfile():
if not verifyLogin():
return "Access Denied"
id = request.json.get('id')
address = request.json.get('address')
about_me = request.json.get('about_me')
name = request.json.get('name')
phone = request.json.get('phone')
email = request.json.get('email')
workHistory = request.json.get('workHistory')
education = request.json.get('education')
print(id)
print(address)
print(about_me)
print(name)
print(phone)
print(email)
print(workHistory)
print(education)
update = '''
UPDATE user_info
SET address = %s,
about_me = %s,
name = %s,
phone = %s,
email = %s,
workHistory = %s,
education = %s
WHERE user_id = %s'''
conn = openConnect()
cursor = conn.cursor()
cursor.execute(update, (address, about_me, name, phone, email, workHistory, education, id))
cursor.close()
conn.commit()
conn.close()
return f"id:{id}, address:{address}, about_me:{about_me}, name:{name}, phone:{phone}, email{email}, workhistory:{workHistory}, education{education}"
# send employer id and can update an employers info
@views.route('/updateEmployer', methods=["POST"])
def updateEmployer():
if not verifyLogin():
return "Access Denied"
employer_user_id = request.json.get('employer_user_id')
location = request.json.get('location')
company_name = request.json.get('company_name')
conn = openConnect()
cursor = conn.cursor()
update = '''
UPDATE employer_info
SET company_name = %s,
location = %s
WHERE employer_user_id = %s'''
cursor.execute(update, (company_name, location, employer_user_id))
cursor.close()
conn.commit()
conn.close()
return "Employer Updated"
# send employer id, id of applicant, and a rating to insert rating for a company
@views.route('/insertRating', methods=["GET"])
def insertRating():
if not verifyLogin():
return "Access Denied"
employer_id = request.args.get('employer_id')
reviewer_id = request.args.get('reviewer_id')
rating = request.args.get('rating')
conn = openConnect()
cursor = conn.cursor()
delete = 'DELETE FROM ratings WHERE employer_id = %s and reviewer_id = %s'
insert = '''
INSERT INTO ratings
(employer_id, reviewer_id, rating)
VALUES (%s,%s,%s)'''
cursor.execute(delete, (employer_id, reviewer_id))
cursor.execute(insert, (employer_id, reviewer_id, rating))
cursor.close()
conn.commit()
conn.close()
return "Success"
# add an employer to the database, send id, location (lnglat), company name
@views.route('/insertEmployerInfo', methods=["POST"])
def insertEmployerInfo():
if not verifyLogin():
return "Access Denied"
employer_user_id = request.json.get('employer_user_id')
location = request.json.get('location')
company_name = request.json.get('company_name')
conn = openConnect()
cursor = conn.cursor()
insert = '''
INSERT INTO employer_info
(employer_user_id, company_name, location)
VALUES
(%s,%s,%s)
'''
cursor.execute(insert, (employer_user_id, company_name, location))
cursor.close()
conn.commit()
conn.close()
return "Employer Updated"
# add user info to database, send id, address, about me, name, phone, email, workhistory and education history
@views.route('/insertUserInfo', methods=["POST"])
def insertUserInfo():
if not verifyLogin():
return "Access Denied"
id = request.json.get('id')
address = request.json.get('address')
about_me = request.json.get('about_me')
name = request.json.get('name')
phone = request.json.get('phone')
email = request.json.get('email')
workHistory = request.json.get('workHistory')
education = request.json.get('education')
insert = '''
INSERT INTO user_info
(user_id, address, about_me, name, phone,email, workhistory,education)
VALUES
(%s, %s, %s, %s, %s, %s, %s, %s)
'''
conn = openConnect()
cursor = conn.cursor()
cursor.execute(insert, (id, address, about_me, name, phone, email, workHistory, education))
cursor.close()
conn.commit()
conn.close()
return "Created"
# insert an applicantion, send job id, applicant id, and a message
@views.route('/insertApp', methods=["POST"])
def insertApp():
if not verifyLogin():
return "Access Denied"
jp_id = request.json.get('jp_id')
applicant_id = request.json.get('applicant_id')
message = request.json.get('message')
conn = openConnect()
cursor = conn.cursor()
insert = '''
INSERT INTO applications
(jp_id, applicant_id, message,status)
VALUES
(%s,%s,%s, 'pending')'''
cursor.execute(insert, (jp_id, applicant_id, message))
cursor.close()
conn.commit()
conn.close()
return "Valid"
# send employer id, get back all reviews for that company
@views.route('/companyReviews', methods=["GET"])
def companyReviews():
if not verifyLogin():
return "Access Denied"
employer_id = request.args.get('employer_id')
conn = openConnect()
cursor = conn.cursor()
select = 'SELECT rating FROM ratings WHERE employer_id = %s'
cursor.execute(select, (employer_id,))
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# update an application, done by employer,
# send job id, applicant id, message and status (denied/approved)
@views.route('/updateApplication', methods=['POST'])
def updateApp():
if not verifyLogin():
return "Access Denied"
conn = openConnect()
cursor = conn.cursor()
message = request.json.get('message')
status = request.json.get('status')
jp_id = request.json.get('jp_id')
applicant_id = request.json.get('applicant_id')
update = '''
UPDATE applications
SET message = %s,
status = %s
WHERE jp_id = %s and applicant_id = %s'''
cursor.execute(update, (message, status, jp_id, applicant_id))
conn.commit()
cursor.close()
conn.close()
return "Success"
# get all applications from a user
#send user id
@views.route('/getUserApp', methods=['GET'])
def getUserApp():
if not verifyLogin():
return "Access Denied"
conn = openConnect()
cursor = conn.cursor()
id = request.args.get('id')
select = 'SELECT jp.job_title,ei.company_name,status FROM job_posting jp JOIN employer_info ei ON jp.employer_id = ei.employer_user_id LEFT JOIN applications a ON jp.id = a.jp_id WHERE applicant_id = %s'
cursor.execute(select, (id,))
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# get all applications sent to an employer
# send employer id
@views.route('/getEmployerApp', methods=['GET'])
def getEmployerApp():
if not verifyLogin():
return "Access Denied"
conn = openConnect()
cursor = conn.cursor()
id = request.args.get('id')
select = 'SELECT a.applicant_id , jp_id, status, u.username FROM job_posting jp JOIN applications a ON a.jp_id = jp.id JOIN users u ON a.applicant_id = u.id WHERE employer_id = %s'
cursor.execute(select, (id,))
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# get all jobs in a specific categroy
# send category
@views.route('/jobCategory', methods=['GET'])
def jobCategory():
if not verifyLogin():
return "Access Denied"
type = request.args.get('type').replace('%20'," ")
conn = openConnect()
cursor = conn.cursor()
select = '''
SELECT job_posting.id, employer_id,job_title, description, salary, type, location FROM job_posting
JOIN users
ON job_posting.employer_id = users.id
JOIN employer_info
ON employer_user_id = users.id
WHERE type = %s'''
cursor.execute(select, (type,))
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# get all jobs posted by a employer
# send employer id
@views.route('/jobByEmployer', methods=['GET'])
def jobByEmployer():
if not verifyLogin():
return "Access Denied"
employer_id = request.args.get('employer_id')
conn = openConnect()
cursor = conn.cursor()
select = '''
SELECT job_posting.id,job_title, description, salary, type, location FROM job_posting
JOIN users
ON job_posting.employer_id = users.id
JOIN employer_info
ON employer_user_id = users.id
WHERE employer_id = %s'''
cursor.execute(select, (employer_id,))
result = cursor.fetchall()
cursor.close()
conn.close()
response = ""
if result:
for row in result:
response += delimiter.join(map(str, row)) + delimiter2
return response
# verify if a user is logged in
def verifyLogin():
print(session)
return 'username' in session
# log the user out
@views.route('/logout', methods=["GET"])
def logout():
if verifyLogin():
session.pop('username', None)
session.pop('role', None)
return "You've been signed out"
else:
return "No one signed in"
# add a new job
# send employer id, title,description, salary and type
@views.route('/createJob', methods=['POST'])
def createJob():
if not verifyLogin():
return "Access Denied"
conn = openConnect()
cursor = conn.cursor()
employer_id = request.json.get('employer_id')
job_title = request.json.get('job_title')
description = request.json.get('description')
salary = request.json.get('salary')
type = request.json.get('type')
update = '''
INSERT INTO job_posting
(employer_id, job_title, description, salary,type, active)
VALUES
(%s, %s, %s, %s, %s, true)
'''
cursor.execute(update, (employer_id, job_title, description, salary, type))
conn.commit()
cursor.close()
conn.close()
return "Success"
@views.route("/")
def landing():
return "Hello World"
# connect to the database
def openConnect():
print(os.getenv('DBUSER'))
conn = psycopg2.connect(
dbname=os.getenv('DBNAME'),
user=os.getenv('DBUSER'),
password=os.getenv('PASSWORD'),
host=os.getenv('HOST'),
port=os.getenv('PORT')
)
return conn