示例#1
0
def get_all(userId: Optional[int]) -> Tuple[Content, HttpStatusCode]:
    """Fetches all Job records"""
    user_id = userId
    sync_all = True
    try:
        if user_id:
            # Owner or admin can fetch
            if not (is_admin() or get_jwt_identity() == user_id):
                raise ForbiddenException("not an owner")
            jobs = Job.query.filter(Job.user_id == user_id).all()
        else:
            # Only admin can fetch all
            if not is_admin():
                raise ForbiddenException("unauthorized")
            jobs = Job.all()
        if sync_all:
            for job in jobs:
                for task in job.tasks:
                    synchronize(task.id)
    except NoResultFound:
        content, status = {'msg': JOB['not_found']}, HTTPStatus.NOT_FOUND.value
    except ForbiddenException as fe:
        content, status = {'msg': JOB['all']['forbidden'].format(reason=fe)}, HTTPStatus.FORBIDDEN.value
    except Exception as e:
        log.critical(e)
        content, status = {'msg': GENERAL['internal_error']}, HTTPStatus.INTERNAL_SERVER_ERROR.value
    else:
        results = []
        for job in jobs:
            results.append(job.as_dict())
        content, status = {'msg': JOB['all']['success'], 'jobs': results}, HTTPStatus.OK.value
    finally:
        return content, status
示例#2
0
def test_delete_task(tables, client, new_job, new_task):
    new_job.add_task(new_task)
    new_job.save()

    resp = client.delete(ENDPOINT + '/{}'.format(new_task.id), headers=HEADERS)

    assert resp.status_code == HTTPStatus.OK
    assert len(Task.all()) == 0
    assert len(Job.all()) == 1
    assert len(CommandSegment.all()) == 0  # checks if segments from deleted task are deleted by cascade