generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
597 lines (463 loc) · 17.9 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
import os
from datetime import datetime, timedelta
from flask import (
Flask, flash, render_template,
redirect, request, session, url_for)
from flask_pymongo import PyMongo
from bson.objectid import ObjectId
from werkzeug.security import generate_password_hash, check_password_hash
if os.path.exists("env.py"):
import env
app = Flask(__name__)
app.config["MONGO_DBNAME"] = os.environ.get("MONGO_DBNAME")
app.config["MONGO_URI"] = os.environ.get("MONGO_URI")
app.secret_key = os.environ.get("SECRET_KEY")
mongo = PyMongo(app)
@app.route("/")
@app.route("/index.html")
def homepage():
return render_template("index.html")
@app.route("/register_jobseeker", methods=["GET", "POST"])
def register_jobseeker():
if request.method == "POST":
# do a check on a already existing username
saved_user = mongo.db.users.find_one(
{"email": request.form.get("email")})
if saved_user:
flash("Account with that email address already exists")
return redirect(url_for("register_jobseeker",
_external=True, _scheme='https'))
register = {
"name": request.form.get("name"),
"email": request.form.get("email"),
"password": generate_password_hash(request.form.get("password")),
"is_jobseeker": "Yes",
"saved_jobs": [],
}
mongo.db.users.insert_one(register)
# save the new user into "session"
session["user"] = request.form.get("email")
return redirect(url_for('profile', user=session["user"],
_external=True, _scheme='https'))
return render_template("register-jobseeker.html")
@ app.route("/register_employer", methods=["GET", "POST"])
def register_employer():
if request.method == "POST":
# do a check on a already existing username
saved_user = mongo.db.users.find_one(
{"email": request.form.get("email")})
if saved_user:
flash(
"Account with that email address already exists, please Sign In"
)
return redirect(url_for("register_employer",
_external=True, _scheme='https'))
register = {
"name": request.form.get("name"),
"email": request.form.get("email"),
"password": generate_password_hash(request.form.get("password")),
"company_name": request.form.get("company_name"),
"company_address": request.form.get("company_address"),
"is_employer": "Yes",
"posted_jobs": [],
}
mongo.db.users.insert_one(register)
# save the new user into "session"
session["user"] = request.form.get("email")
return redirect(url_for('profile', user=session["user"],
_external=True, _scheme='https'))
return render_template("register-employer.html")
def is_user_authorised():
# do a check on existing user
existing_user = mongo.db.users.find_one(
{"email": request.form.get("email")})
if existing_user:
# chek if the passowrd matches user input
if check_password_hash(
existing_user["password"], request.form.get("password")):
session["user"] = request.form.get("email")
return session
else:
return None
else:
return None
@ app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
# do a check on existing user
session = is_user_authorised()
if session:
return redirect(url_for(
'profile', user=session["user"],
_external=True, _scheme='https'))
else:
# username doesn't exists
flash("Invalid username and/or password")
return redirect(url_for("login",
_external=True, _scheme='https'))
return render_template("login.html")
@ app.route("/logout")
def logout():
flash("You have been logged out")
session.pop("user")
return redirect(url_for("login",
_external=True, _scheme='https'))
@ app.route("/profile", methods=["GET", "POST"])
def profile():
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
employer = request.form.get("employer_name")
jobseeker = request.form.get("jobseeker_name")
if request.method == "POST":
# check user type
if employer:
submit = {"$set": {
"name": employer,
"email": request.form.get("email"),
}}
# refresh session & update database
session["user"] = request.form.get("email")
mongo.db.users.update(
{"_id": user["_id"]}, submit)
return redirect(url_for("profile",
_external=True, _scheme='https'))
# check user type
elif jobseeker:
submit = {"$set": {
"name": jobseeker,
"email": request.form.get("email"),
}}
# refresh session & update database
session["user"] = request.form.get("email")
mongo.db.users.update(
{"_id": user["_id"]}, submit)
return redirect(url_for("profile",
_external=True, _scheme='https'))
# Check if user is jobseeker or employer and show relevent info
try:
user["is_jobseeker"]
except:
KeyError
else:
saved_jobs = user["saved_jobs"]
return render_template("profile.html", user=user,
saved_jobs=saved_jobs)
try:
user["is_employer"]
except:
KeyError
else:
posted_jobs = user["posted_jobs"]
return render_template("profile.html", user=user,
posted_jobs=posted_jobs)
return redirect(url_for("login",
_external=True, _scheme='https'))
@ app.route("/post_job", methods=["GET", "POST"])
def post_job():
# check if user is logged in and send them to correct page
try:
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
except:
KeyError
flash("Sign In to post jobs")
return redirect(url_for('login',
_external=True, _scheme='https'))
# check if user is employer and allow job posting
try:
user["is_employer"]
except:
KeyError
else:
company_name = str(user["company_name"])
company_address = str(user["company_address"])
posted_date = datetime.now().date()
if request.method == "POST":
new_job = {
"job_title": request.form.get("job_title"),
"job_category": request.form.get("job_category"),
"contract_type": request.form.get("contract_type"),
"job_description": request.form.get("job_description"),
"salary_range": request.form.get("salary_range"),
"posted_date": posted_date.strftime("%d/%m/%y"),
"company_name": company_name,
"company_address": company_address,
}
mongo.db.jobs.insert_one(new_job)
mongo.db.users.update_one(
{"_id": user["_id"]},
{"$push": {"posted_jobs": new_job}}
)
flash("Job Posted Successfully")
return redirect(url_for("post_job",
_external=True, _scheme='https'))
# check if user is jobseeker and don't allow job posting
try:
user["is_jobseeker"]
except:
KeyError
else:
flash("Register as employer to post jobs")
return redirect(url_for("register_employer",
_external=True, _scheme='https'))
return render_template('post_job.html')
@ app.route("/find-job", methods=["GET", "POST"])
def find_job():
# find all jobs
all_jobs = list(mongo.db.jobs.find())
# check if user exists in session
try:
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# handle keyerror and list jobs
except:
KeyError
if request.method == "POST":
job_category = request.form.get("job_category_name")
all_jobs = list(mongo.db.jobs.find({
"job_category": job_category}))
return render_template('find_job.html',
all_jobs=all_jobs,
job_category=job_category)
else:
return render_template('find_job.html', all_jobs=all_jobs)
# list relevant category jobs for logged in users
if request.method == "POST":
job_category = request.form.get("job_category_name")
all_jobs = list(mongo.db.jobs.find({
"job_category": job_category}))
return render_template('find_job.html',
all_jobs=all_jobs,
job_category=job_category, user=user)
return render_template('find_job.html',
all_jobs=all_jobs, user=user)
@ app.route("/find-job-mobile", methods=["GET", "POST"])
def find_job_mobile():
# find all jobs
all_jobs = list(mongo.db.jobs.find())
# check if user exists in session
try:
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# handle keyerror and list jobs
except:
KeyError
if request.method == "POST":
job_category = request.form.get("job_category_name")
all_jobs = list(mongo.db.jobs.find({
"job_category": job_category}))
return render_template('find_job_mobile.html',
all_jobs=all_jobs,
job_category=job_category)
else:
return render_template('find_job_mobile.html', all_jobs=all_jobs)
# list relevant category jobs for logged in users
if request.method == "POST":
job_category = request.form.get("job_category_name")
all_jobs = list(mongo.db.jobs.find({
"job_category": job_category}))
return render_template('find_job_mobile.html',
all_jobs=all_jobs, job_category=job_category,
user=user)
return render_template('find_job_mobile.html',
all_jobs=all_jobs, user=user)
@ app.route("/save-job", methods=["GET", "POST"])
def save_job():
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
if request.method == "POST":
# retrive id from subbmited job
job_id = request.form.get("job_id")
# find job by job_id
saved_job = mongo.db.jobs.find_one(
{
"_id": ObjectId(job_id)
}
)
# save all records from saved_jobs
saved_jobs = user["saved_jobs"]
# do a check if a job is already saved
if saved_job not in saved_jobs:
flash("Job saved successfully")
# update saved_jobs record
mongo.db.users.update_one(
{"_id": user["_id"]},
{"$push": {"saved_jobs": saved_job}}
)
else:
flash("Job already saved")
return redirect(url_for('find_job', _anchor='job-listing-wrap',
_external=True, _scheme='https'))
return redirect(url_for("find_job", _anchor='job-listing-wrap',
_external=True, _scheme='https'))
@ app.route("/save-job-mobile", methods=["GET", "POST"])
def save_job_mobile():
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
if request.method == "POST":
# retrive id from subbmited job
job_id = request.form.get("job_id")
# find job by job_id
saved_job = mongo.db.jobs.find_one(
{
"_id": ObjectId(job_id)
}
)
# save all records from saved_jobs
saved_jobs = user["saved_jobs"]
# do a check if a job is already saved
if saved_job not in saved_jobs:
flash("Job saved successfully")
# update saved_jobs record
mongo.db.users.update_one(
{"_id": user["_id"]},
{"$push": {"saved_jobs": saved_job}}
)
else:
flash("Job already saved")
return redirect(url_for('find_job_mobile',
_anchor='job-listing-wrap',
_external=True, _scheme='https'))
return redirect(url_for("find_job_mobile",
_anchor='job-listing-wrap',
_external=True, _scheme='https'))
@ app.route("/edit_job/<post>", methods=["GET", "POST"])
def edit_job(post):
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# retrive job from profile page
job = mongo.db.jobs.find_one({
"_id": ObjectId(post)
})
if request.method == "POST":
# remove the old version of the saved job from user collection
mongo.db.users.update(
{"_id": user["_id"]},
{"$pull": {"posted_jobs": {"_id": ObjectId(post)}}}
)
# update the selected job
update_job = {"$set": {
"job_title": request.form.get("job_title"),
"job_description": request.form.get("job_description"),
"salary_range": request.form.get("salary_range"),
}}
mongo.db.jobs.update_one(job, update_job)
flash("Job changed Successfully")
return redirect(url_for('edit_job', post=post,
_external=True, _scheme='https'))
# check if the job is already saved, add the job id not existing
saved_jobs = user["posted_jobs"]
if job in saved_jobs:
pass
elif job not in saved_jobs:
mongo.db.users.update(
{"_id": user["_id"]},
{"$push": {"posted_jobs": job}}
)
return render_template('edit_job.html', post=post, job=job)
@ app.route("/delete_posted_job/<post>", methods=["GET", "POST"])
def delete_posted_job(post):
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
deleted_job_employer = mongo.db.jobs.find_one(
{
"_id": ObjectId(post)
}
)
mongo.db.users.update_one(
{"_id": user["_id"]},
{"$pull": {"posted_jobs": deleted_job_employer}}
)
# remove a job from jobs collection
mongo.db.jobs.remove({
"_id": ObjectId(post)
})
mongo.db.users.update_one(
{"is_jobseeker": "Yes"},
{"$pull": {"saved_jobs":
deleted_job_employer}}
)
flash("Job deleted successfully")
return redirect(url_for("profile",
_external=True, _scheme='https',
post=post))
@ app.route("/delete_saved_job/<job>", methods=["GET", "POST"])
def delete_saved_job(job):
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# find job by job_id jobseeker & update the array in db
deleted_job_jobseeker = mongo.db.jobs.find_one(
{
"_id": ObjectId(job)
}
)
mongo.db.users.update_one(
{"_id": user["_id"]},
{"$pull": {"saved_jobs": deleted_job_jobseeker}}
)
flash("Job deleted successfully")
return redirect(url_for("profile",
_external=True, _scheme='https',
job=job))
@ app.route("/contact")
def contact():
return render_template("contact.html")
@ app.route("/search", methods=["POST", "GET"])
def search():
# find all jobs
all_jobs = list(mongo.db.jobs.find())
# check if user exists in session
try:
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# handle keyerror and list jobs
except:
KeyError
if KeyError:
search_term = request.form.get("search_box")
all_jobs = list(mongo.db.jobs.find(
{"$text": {"$search": search_term}}))
return render_template('find_job.html',
all_jobs=all_jobs)
search_term = request.form.get("search_box")
all_jobs = list(mongo.db.jobs.find({"$text": {"$search": search_term}}))
return render_template('find_job.html',
all_jobs=all_jobs,
user=user)
@ app.route("/search_mobile", methods=["POST", "GET"])
def search_mobile():
# find all jobs
all_jobs = list(mongo.db.jobs.find())
# check if user exists in session
try:
# retrive users name from database
user = mongo.db.users.find_one(
{"email": session["user"]})
# handle keyerror and list jobs
except:
KeyError
if KeyError:
search_term = request.form.get("search_box")
all_jobs = list(mongo.db.jobs.find(
{"$text": {"$search": search_term}}))
return render_template('find_job_mobile.html',
all_jobs=all_jobs)
search_term = request.form.get("search_box")
all_jobs = list(mongo.db.jobs.find({"$text": {"$search": search_term}}))
return render_template('find_job_mobile.html',
all_jobs=all_jobs,
user=user)
if __name__ == "__main__":
app.run(host=os.environ.get("IP"),
port=int(os.environ.get("PORT")),
debug=False)