コード例 #1
0
ファイル: state.py プロジェクト: mavenave/sohigh
def fetch_state(room_name):
    if not redis.sismember('states', room_name):
        abort(404)

    if request.method == 'GET':
        if redis.sismember('dirty_state', room_name):
            state = json.loads(redis.get('state:'+room_name))
            if not state:
                abort(404, 'Please submit initial state first with POST')

            redis.srem('dirty_state', room_name)
            state['dirty'] = True
            return jsonify(state)
        else:
            return jsonify({
                'dirty': False
            })

    if request.method == 'POST':
        new_state = request.form.get('newState')
        if not new_state:
            abort(400)

        try:
            json.loads(new_state)
        except ValueError:
            abort(400)

        redis.set('state:'+room_name, new_state)
        redis.sadd('dirty_state', room_name)
        return '', 200