def get(self): """ Return all job instances """ try: jobinstances = [ strip_objectid(jobinstance) for jobinstance in mongo.db.jobinstances.aggregate([{ '$match': { 'is_approved': False } }, { '$lookup': { 'from': 'profiles', 'localField': 'participant_ids', 'foreignField': '_id', 'as': 'participants' } }, { '$lookup': { 'from': 'jobs', 'localField': 'job_id', 'foreignField': '_id', 'as': 'job' } }, { '$unwind': { 'path': '$job' } }]) ] except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 return {'jobinstances': jobinstances}
def get(self, profile_id): """ Return a specific profile as an object """ profile = ProfilesModel.find_by_id(profile_id) if profile is None: return {"message": "A profile with that ID does not exist"}, 404 return strip_objectid(profile)
def get(self, job_id): """ Return a specific job """ job = JobsModel.find_by_id(job_id) if job is None: return {"message": "A job with that ID does not exist"}, 404 return strip_objectid(job)
def post(self, jobinstance_id): """ Endpoint to approve a job instance where no edit required """ try: with mongo.cx.start_session() as session: with session.start_transaction(): job_instance = JobInstanceModel.find_by_id(jobinstance_id) if not job_instance: return { "message": "A job instance with that ID does not exist" }, 404 # Update JOBINSTANCE if job_instance["is_approved"]: return {"message": "already approved"}, 400 job_instance["is_approved"] = True mongo.db.jobinstances.update( {"_id": ObjectId(jobinstance_id)}, job_instance) # Update PROFILES job_id = str(job_instance["job_id"]) job = JobsModel.find_by_id(job_id) for profile_id in job_instance["participant_ids"]: updated_profile = ProfilesModel.find_by_id(profile_id) # Completed Jobs if job_id in updated_profile["completed_jobs"]: updated_profile["completed_jobs"][job_id][ "number_completed_instances"] += 1 else: updated_profile["completed_jobs"][job_id] = { "job_name": job["name"], "number_completed_instances": 1 } # Money Owed updated_profile["money_owed"] += job["reward"] # Total_money_earned updated_profile["total_money_earned"] += job["reward"] print(updated_profile) mongo.db.profiles.update({"_id": profile_id}, updated_profile) return strip_objectid(job_instance) except Exception as error: traceback.print_exc(error) return {"message": "unknown error"}, 500 return "foo bar"
def put(self, jobinstance_id): """ Edit the participants in a job instance """ request_data = JobInstance.parser.parse_args() try: with mongo.cx.start_session() as session: with session.start_transaction(): job_instance = JobInstanceModel.find_by_id(jobinstance_id) participants = [] if not job_instance: return { "message": "A job instance with that ID does not exist" }, 404 if job_instance["is_approved"]: return {"message": "already approved"}, 400 if not request_data["participant_ids"]: return { "message": "A job instance must have participants" }, 400 for participant_id in request_data["participant_ids"]: profile = ProfilesModel.find_by_id(participant_id) if not profile: return { "message": f"Participant {participant_id} not found in database" } participants.append(profile) # Updating the job instance job_instance["participant_ids"] = [ ObjectId(participant_id) for participant_id in request_data["participant_ids"] ] mongo.db.jobinstances.update( {"_id": ObjectId(jobinstance_id)}, job_instance) job_instance["participants"] = participants job_instance["job"] = JobsModel.find_by_id( job_instance["job_id"]) return strip_objectid(job_instance) except ValidationError as error: return {"message": error.message}, 400 except Exception as error: traceback.print_exc(error) return {"message": "unknown error"}, 500
def get(self): """ Return all jobs """ try: jobs = [ strip_objectid(job) for job in mongo.db.jobs.find().sort('name', DESCENDING) # TODO: sort in name Ascending ] except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 return {'jobs': jobs}
def get(self): """ Return all profiles """ try: profiles = [ strip_objectid(profile) for profile in mongo.db.profiles.find().sort('name', DESCENDING) ] except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 return { 'profiles': profiles }
def put(self, job_id): """ Edit a specific job """ request_data = Job.parser.parse_args() if not JobsModel.find_by_id(job_id): return {"message": "A job with that ID does not exist"}, 404 try: # TODO: transaction - also update jobs saved in user's profiles # TODO: transaction - also update jobs saved in Job instances updated_job = JobsModel.build_job_from_request(request_data) mongo.db.jobs.update({"_id": ObjectId(job_id)}, updated_job) return strip_objectid(updated_job) except ValidationError as error: return {"message": error.message}, 400
def put(self, profile_id): """ Edit a specific profile """ request_data = Profile.parser.parse_args() if not ProfilesModel.find_by_id(profile_id): return {"message": "A profile with that ID does not exist"}, 404 try: updated_profile = ProfilesModel.build_profile_from_request( request_data) mongo.db.profiles.update( {"_id": ObjectId(profile_id)}, updated_profile) updated_profile['_id'] = profile_id return strip_objectid(updated_profile) except ValidationError as error: return {"message": error.message}, 400
def post(self): """ Create a new job instance """ try: request_data = self.parser.parse_args() except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 try: with mongo.cx.start_session() as session: with session.start_transaction(): # Building the jobInstance print(request_data) new_jobinstance = JobInstanceModel.build_jobinstance_from_request( request_data) if new_jobinstance['is_approved']: raise ValidationError( "Job instance cannot be complete uppon creation") # TODO: prevent multiple creations (cannot check on name) result = mongo.db.jobinstances.insert_one(new_jobinstance) new_jobinstance['_id'] = result.inserted_id # Update the last_completed field on the job model updated_job = JobsModel.find_by_id( new_jobinstance['job_id']) updated_job['last_completed'] += 1 mongo.db.jobs.update( {"_id": ObjectId(new_jobinstance['job_id'])}, updated_job) return strip_objectid(new_jobinstance) except ValidationError as error: return {"message": error.message}, 400
def post(self): """ Create a new profile """ try: request_data = Profile.parser.parse_args() print(request_data) except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 try: new_profile = ProfilesModel.build_profile_from_request(request_data) if ProfilesModel.find_by_name(new_profile['name']): return {'message': 'A user with that name already exists'}, 400 result = mongo.db.profiles.insert_one(new_profile) new_profile['_id'] = result.inserted_id return strip_objectid(new_profile) except ValidationError as error: return {'message': error.message}, 400
def get(self, jobinstance_id): """ Return a specific job instance """ jobinstance = next( mongo.db.jobinstances.aggregate([{ '$match': { '_id': ObjectId(jobinstance_id) } }, { '$lookup': { 'from': 'profiles', 'localField': 'participant_ids', 'foreignField': '_id', 'as': 'participants' } }, { '$lookup': { 'from': 'jobs', 'localField': 'job_id', 'foreignField': '_id', 'as': 'job' } }, { '$unwind': { 'path': '$job' } }])) # print(jobinstance) if jobinstance is None: return {"message": "A job with that ID does not exist"}, 404 # stripped_jobinstance = return strip_objectid(jobinstance)
def post(self): """ Add a new job to the db """ try: request_data = Job.parser.parse_args() except Exception as error: print(error) return {'message': "Malformed input. Check the console"}, 400 try: new_job = JobsModel.build_job_from_request(request_data) if JobsModel.find_by_name(new_job['name']): return {'message': 'A job with that name already exists'}, 400 # Generate and add code for post it note design new_job['postit_id'] = JobsModel.generate_postit_id() result = mongo.db.jobs.insert_one(new_job) new_job['_id'] = result.inserted_id return strip_objectid(new_job) except ValidationError as error: return {'message': error.message}, 400