Beispiel #1
0
def update_author(author_id):
    """Updates the author matching the id author_id.
    Only the parameters to update or to add should be passed in the request body.
    """
    author = Author.objects(id=author_id).get()
    patched = Author(**dict(chain(author.to_dict().items(), request.get_json().items())))
    patched.save()

    return jsonify(patched.to_dict())
Beispiel #2
0
def authors():
    """Retrieves a list of authors from the database or create a new author.
    When retrieving a list of authors, the URL can contain pagination parameters
    page and per_page which default to 1 and 10 if omitted.
    """
    if request.method == 'GET':
        return paginate(resource_name='authors', endpoint='authors', objects=Author.objects)

    author = Author(**request.get_json())
    author.save()

    return jsonify(author.to_dict()), 201