97 lines
2.7 KiB
Python
97 lines
2.7 KiB
Python
import pymongo
|
|
import os
|
|
import time
|
|
|
|
client = pymongo.MongoClient(os.getenv("MONGO_URL"))
|
|
db = client.VE
|
|
|
|
class Job:
|
|
def __init__(self, title, education, pph):
|
|
self.title = title
|
|
self.education = education
|
|
self.pph = pph
|
|
|
|
|
|
def create_new_job(title, education, pph):
|
|
coll = db.jobs
|
|
job = coll.find_one({"title": title})
|
|
|
|
### Check if there is a job with that title, if so ask the admin if they want to update that job with the new education requirement and pph
|
|
if job is not None:
|
|
do_update = input("A job with that title has already been created. Do you want to update it? (y/n): ")
|
|
if do_update == "y":
|
|
coll.update_one({"title": title}, {"$set": {"education": education, "pph": pph}})
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
### Check if there is any job with the id 0, if not then add it
|
|
first_job = coll.find_one({"_id": 0})
|
|
|
|
if first_job is None:
|
|
coll.insert_one({"_id": 0, "title": title, "education": education, "pph": pph})
|
|
return True
|
|
|
|
### Get the all the IDs of the jobs in the db
|
|
all_jobs = coll.find({})
|
|
jobs = []
|
|
for job in all_jobs:
|
|
jobs.append(job["_id"])
|
|
|
|
### Insert the job into the db, but get the last ID in the array and add 1 to it.
|
|
coll.insert_one({"_id": (jobs[-1] + 1), "title": title, "education": education, "pph": pph})
|
|
return True
|
|
|
|
def get_job_details(job_id):
|
|
coll = db.jobs
|
|
job = coll.find_one({"_id": job_id})
|
|
|
|
return Job(job['title'], job['education'], job['pph'])
|
|
|
|
def get_all_jobs():
|
|
coll = db.jobs
|
|
jobs = coll.find({})
|
|
|
|
for job in jobs:
|
|
print(f"""
|
|
Job ID: {job['_id']}
|
|
Job Title: {job['title']}
|
|
Education Requirements: {job['education']}
|
|
Pay Per Hour: {job['pph']}
|
|
""")
|
|
|
|
return
|
|
|
|
def assign_job(code, job_id):
|
|
account_coll = db.accounts
|
|
job_coll = db.jobs
|
|
|
|
user = account_coll.find_one({"_id": code})
|
|
job = job_coll.find_one({"_id": job_id})
|
|
|
|
if user is None or job is None:
|
|
print("Either that user doesn't exist, or the job doesn't exist")
|
|
return None
|
|
|
|
user_education = user['education']
|
|
job_education = job['education']
|
|
|
|
if user_education < job_education:
|
|
print("We are sorry, but you are not qualified for this job."
|
|
"Please go to the education center to get a higher education level.")
|
|
time.sleep(1)
|
|
return False
|
|
else:
|
|
account_coll.update_one({"_id": code}, {"$set": {"job": job_id}})
|
|
return True
|
|
|
|
|
|
def get_user_job(code):
|
|
coll = db.accounts
|
|
user = coll.find_one({"_id": code})
|
|
|
|
if user is None:
|
|
print("That user could not be found")
|
|
return None
|
|
|
|
return user['job'] |