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 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 delete(self, job_id): """ Delete a specific job """ job = JobsModel.find_by_id(job_id) if job is None: return {"message": "A job with that ID does not exist"} # TODO: remove job from user profile once it has been deleted mongo.db.jobs.remove({"_id": ObjectId(job_id)}) return {"message": "Job has been deleted"}
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
def build_jobinstance_from_request(request_data): built_jobinstance = { 'job_id': {}, 'participant_ids': [], 'completion_date': 0, 'is_approved': False, } # Job built_jobinstance['job_id'] = ObjectId(request_data.get('job_id', '')) if not built_jobinstance['job_id']: raise ValidationError("Job instance must include a job model") if not JobsModel.find_by_id(built_jobinstance['job_id']): raise ValidationError("Job model not found") # Participants built_jobinstance['participant_ids'] = [ ObjectId(participant_id) for participant_id in request_data.get('participant_ids', []) ] if len(built_jobinstance['participant_ids']) < 1: raise ValidationError("There must be at least one participant") for participant_id in built_jobinstance['participant_ids']: if not ProfilesModel.find_by_id(participant_id): raise ValidationError("Profile not found") # Completion Date built_jobinstance['completion_date'] = request_data.get( 'completion_date', 0) if built_jobinstance['completion_date'] <= 0: raise ValidationError('completion date must be greater than 0') # Is Approved built_jobinstance['is_approved'] = request_data['is_approved'] return built_jobinstance
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