Beispiel #1
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)
Beispiel #2
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
Beispiel #3
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