Example #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)

        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())
Example #2
0
def userlist_page():
    projection = {"password" : False}
    sort = [("user_info.rating", pymongo.DESCENDING)]
    users = id_clear(db.users.find(
        filter={"user_info.number_of_battles": {"$ne": 0}},
        projection=projection,
        sort=sort))
    return render_template("rank_list.html", users=users)
Example #3
0
def battles():
    if request.method == 'GET':
        try:
            query = json.loads(request.args.get("query", "{}"))
            sort = json.loads(request.args.get("sort", "[]"))
        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(id_clear(db.battles.find(
            filter=mongo_query,
            sort=mongo_sort)))

    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})
Example #4
0
def battles():
    if request.method == 'GET':
        #need config (condition, sort)
        return jsonify(id_clear(db.battles.find()))
    elif request.method == 'POST':
        request_json = request.get_json(force=True)
        succ, battle = BattleFactory.create_battle(int(time.time()),
                                                   request_json['battle_info'],
                                                   request_json['board_type'],
                                                   db)
        if not succ:
            return jsonify({"message": battle})
        return jsonify({"id": battle.id})
Example #5
0
def user():
    if request.method == 'GET':
        #TODO remove password please
        return jsonify(id_clear(db.users.find(proj)))
    elif request.method == 'POST':
        request_json = request.get_json(force=True)

        user = User.create(db, id_generate, request_json['username'],
                           request_json['password'])

        if user == False:
            return False

        login_user(user)
        return True