Exemple #1
0
def users():
    if request.method == 'GET':
        try:
            query = json.loads(request.args.get("query", ""))
            sort = json.loads(request.args.get("sort", "[]"))
        except Exception as e:
            return failure(repr(e))

        #remove password
        projection = {"password": False}
        return success(
            id_clear(
                db.users.find(filter=query, projection=projection, sort=sort)))

    elif request.method == 'POST':
        request_json = request.get_json(force=True)

        check_res = field_checker(request_json,
                                  ['username', 'email', 'password', 'token'])
        if check_res is not None:
            return failure(check_res)

        try:
            assert (get_email_from_token(
                request_json['token']) == request_json['email'])
        except Exception as e:
            return failure("Permission Denied")

        user = User.create(db, id_generate, request_json['username'],
                           request_json['email'], request_json['password'])
        if isinstance(user, str):
            return failure(user)
        login_user(user)

        return success(user.dump())
Exemple #2
0
def battle(battle_id):
    battle = BattleFactory.load_battle(battle_id, db)
    user_id = current_user.user_id

    if isinstance(battle, str):
        return failure(battle)

    if request.method == 'GET':
        require = require_format(request.args)
        return success(battle.get_state(current_time(), user_id, require))

    elif request.method == 'POST':
        #todo check user_id match player_id
        request_json = request.get_json(force=True)

        check_res = field_checker(request_json,
                                  ['player_id', 'piece_id', 'position'])
        if check_res is not None:
            return failure(check_res)

        battle.try_drop_piece(current_time(), request_json['player_id'],
                              current_user.user_id, request_json['piece_id'],
                              request_json['position'])
        return success(
            battle.get_state(current_time(), request_json['player_id']))
Exemple #3
0
def battles():
    if request.method == 'GET':
        try:
            query = json.loads(request.args.get("query", "{}"))
            sort = json.loads(request.args.get("sort", "[]"))
            start = json.loads(request.args.get('start',
                                                "{'start': 0}"))['start']
            limit = json.loads(request.args.get('limit',
                                                "{'limit': 30}"))['limit']
        except:
            return failure("request syntax error! need json string!")

        if "username" in query and not username_checker(db, query['username']):
            return failure("user not exist")

        mongo_query = filter_condition_generate(query)
        if isinstance(query, str):
            return failure(query)

        mongo_sort = sort_condition_generate(sort)
        if isinstance(sort, str):
            return failure(sort)

        current_user.update_perference("condition", {
            "query": query,
            "sort": sort
        })

        return success({
            "start":
            start,
            "battle_list":
            history_clear(
                db.battles.find(filter=mongo_query,
                                sort=mongo_sort)[start:start + limit])
        })

    elif request.method == 'POST':
        if current_user.user_id == -1:
            return failure("need login first!")

        request_json = request.get_json(force=True)
        check_res = field_checker(
            request_json,
            ['battle_name', 'accuracy_time', 'additional_time', 'board_type'])
        if check_res is not None:
            return failure(check_res)

        battle = BattleFactory.create_battle(current_time(), request_json,
                                             request_json['board_type'], db)

        if isinstance(battle, str):
            return failure(battle)

        current_user.update_perference("create", request_json)

        return success({"id": battle.id})
Exemple #4
0
def password_resetter():
    request_json = request.get_json(force=True)

    check_res = field_checker(request_json, ['email'])
    if check_res is not None:
        return failure(check_res)

    result = send_reset_mail(
        request_json['email'], url_head + "/password_resetter?token=" +
        generate_register_token(request_json['email']), email_config)

    if result != "success":
        return failure(result)
    return success("")
Exemple #5
0
def confirm():
    request_json = request.get_json(force=True)
    check_res = field_checker(request_json, ['email', 'password'])
    if check_res is not None:
        return failure(check_res)
    if not current_user.check_password(request_json['password']):
        return failure("原密码错误")

    result = send_confirm_email(
        current_user.username, request_json['email'], url_head +
        "/confirm?token=" + generate_register_token(request_json['email']),
        email_config)

    if result != "success":
        return failure(result)
    return success("")
Exemple #6
0
def user_email_password_resetter(user_id):
    request_json = request.get_json(force=True)

    check_res = field_checker(request_json, ['token', 'password'])
    if check_res is not None:
        return failure(check_res)
    try:
        token = request_json["token"]
        email = get_email_from_token(token)
        user = User.load_from_email(db, email)
        if email == False:
            return failure("Permission denied")
    except Exception:
        return failure("Permission denied")

    user.update('password', request_json['password'])
    login_user(user)

    return success("")
Exemple #7
0
def user(user_id):
    if current_user.user_id == -1 or current_user.user_id != user_id:
        return failure("perission denied")
    request_json = request.get_json(force=True)

    check_res = field_checker(request_json, ['old_password'])
    if check_res is not None:
        return failure(check_res)

    if not current_user.check_password(request_json['old_password']):
        return failure("原密码错误")

    legal_field = ['username', 'password']
    for key in list(request_json):
        if key not in legal_field:
            request_json.pop(key)

    for key in request_json:
        current_user.update(key, request_json[key])

    return success(current_user.dump())
Exemple #8
0
def chat_logs(battle_id):
    if current_user.user_id == -1:
        return failure("need login first!")

    request_json = request.get_json(force=True)
    check_res = field_checker(request_json, ['content'])
    if check_res is not None:
        return failure(check_res)

    battle = BattleFactory.load_battle(battle_id, db)
    if isinstance(battle, str):
        return failure(battle)

    require = request_json.get("require", {})
    result = battle.append_chat_log(current_time(),
                                    current_user.username,
                                    request_json['content'],
                                    require=require)
    if isinstance(result, str):
        return failure(result)

    return success(result)