def create(job: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: """ Creates new Job db record.""" try: assert job['userId'] == get_jwt_identity(), 'Not an owner' new_job = Job( name=job['name'], description=job['description'], user_id=job['userId'] ) if 'startAt' in job and job['startAt'] is not None: setattr(new_job, 'start_at', job['startAt']) if 'stopAt' in job and job['stopAt'] is not None: setattr(new_job, 'stop_at', job['stopAt']) new_job.save() except AssertionError as e: if e.args[0] == 'Not an owner': content, status = {'msg': GENERAL['unprivileged']}, HTTPStatus.FORBIDDEN.value else: content = {'msg': JOB['create']['failure']['invalid'].format(reason=e)} status = HTTPStatus.UNPROCESSABLE_ENTITY.value except ValueError: # Invalid string format for datetime content = {'msg': JOB['create']['failure']['invalid']} status = HTTPStatus.UNPROCESSABLE_ENTITY.value except Exception as e: log.critical(e) content, status = {'msg': GENERAL['internal_error']}, HTTPStatus.INTERNAL_SERVER_ERROR.value else: content = { 'msg': JOB['create']['success'], 'job': new_job.as_dict() } status = HTTPStatus.CREATED.value finally: return content, status
def new_running_job(new_user): new_user.save() job = Job(name='running_job', description='A running job', user_id=new_user.id, _status=JobStatus.running) job.save() return job
def new_job(new_user): new_user.save() job = Job(name='job_name', description='testDescription', user_id=new_user.id, _status=JobStatus.not_running) job.save() return job
def new_admin_job(new_user, new_admin, new_task): new_user.save() new_admin.save() job = Job(name='admin_job', description='Admin is the owner of this job', user_id=new_admin.id, _status=JobStatus.not_running) job.save() job.add_task(new_task) return job