state.py 文件源码

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

项目:airbnb_clone 作者: bennettbuchanan 项目源码 文件源码
def handle_states():
    '''Returns all the states from the database as JSON objects with a GET
    request, or adds a new state to the database with a POST request. Refer to
    exception rules of peewee `get()` method for additional explanation of
    how the POST request is handled:
    http://docs.peewee-orm.com/en/latest/peewee/api.html#SelectQuery.get
    '''
    if request.method == 'GET':
        list = ListStyle().list(State.select(), request)
        return jsonify(list), 200

    elif request.method == 'POST':
        params = request.values

        '''Check that all the required parameters are made in request.'''
        required = set(["name"]) <= set(request.values.keys())
        if required is False:
            return jsonify(msg="Missing parameter."), 400

        try:
            State.select().where(State.name == request.form['name']).get()
            return jsonify(code=10001, msg="State already exists"), 409
        except State.DoesNotExist:
            state = State.create(name=request.form['name'])
            return jsonify(state.to_dict()), 201
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号