place_book.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:airbnb_clone 作者: bennettbuchanan 项目源码 文件源码
def handle_books_id(place_id, book_id):
    '''Returns a JSON object of the book_id with a GET request method. Updates
    a booking's attributes with a POST request method. Removes a booking with a
    DELETE request method.

    Keyword arguments:
    place_id: The id of the place with the booking.
    book_id: The id of the booking.
    '''
    try:
        book = PlaceBook.select().where(PlaceBook.id == book_id).get()
    except PlaceBook.DoesNotExist:
        raise Exception("There is no placebook with this id.")

    if request.method == 'GET':
        return jsonify(book.to_dict()), 200

    elif request.method == 'PUT':
        params = request.values
        for key in params:
            if key == 'user':
                return jsonify(msg="You may not change the user."), 409
            if key == 'updated_at' or key == 'created_at':
                continue
            setattr(book, key, params.get(key))
        book.save()
        return jsonify(msg="Place book information updated successfully."), 200

    elif request.method == 'DELETE':
        try:
            book = PlaceBook.delete().where(PlaceBook.id == book_id)
        except PlaceBook.DoesNotExist:
            raise Exception("There is no place with this id.")
        book.execute()
        return jsonify(msg="Place book deleted successfully."), 200
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号