コード例 #1
0
def show(work_id):
    schema = WorkSchema()
    work = Work.get(id=work_id)

    if not work:
        abort(404)

    return schema.dumps(work)
コード例 #2
0
def show(work_id):
    # This will serialize our data
    schema = WorkSchema()
    # This gets a work by ID
    work = Work.get(id=work_id)

    # If we can't find a work, send a 404 response
    if not work:
        abort(404)

    # otherwise, send back the work data as JSON
    return schema.dumps(work)
コード例 #3
0
def update(work_id):
    schema = WorkSchema()
    work = Work.get(id=work_id)

    if not work:
        abort(404)

    try:
        data = schema.load(request.get_json())
        work.set(**data)
        db.commit()
    except ValidationError as err:
        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422

    return schema.dumps(work)
コード例 #4
0
def create():

    schema = WorkSchema()

    try:

        data = schema.load(request.get_json())

        work = Work(**data)

        db.commit()
    except ValidationError as err:

        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422

    return schema.dumps(work), 201
コード例 #5
0
def create():
    # This will deserialize the JSON from insomnia
    schema = WorkSchema()

    try:
        # attempt to convert the JSON into a dict
        data = schema.load(request.get_json())
        # Use that to create a work object
        work = Work(**data, createdBy=g.current_user)
        # store it in the database
        db.commit()
    except ValidationError as err:
        # if the validation fails, send back a 422 response
        return jsonify({
            'message': 'Validation failed',
            'errors': err.messages
        }), 422

    # otherwise, send back the work data as JSON
    return schema.dumps(work), 201
コード例 #6
0
def index():
    # This will serialize our data
    # `many=True` because there are many works, ie we expect a list
    schema = WorkSchema(many=True)
    works = Work.select()  # get all the works
    return schema.dumps(works)  # `schema.dumps` converts the list to JSON
コード例 #7
0
def index():
    schema = WorkSchema(many=True)
    works = Work.select()
    return schema.dumps(works)