def increase_value(request):
    """increase a value for the user"""
    
    user_id = int(request.matchdict["user_id"])
    try:
        value = float(request.POST["value"])
    except:
        raise APIError(400,"invalid_value","Invalid value provided")
    
    key = request.matchdict["key"] if "key" in request.matchdict else ""
    variable_name = request.matchdict["variable_name"]
    
    user = User.get_user(user_id)
    if not user:
        raise APIError(404, "user_not_found", "user not found")
    
    variable = Variable.get_variable_by_name(variable_name)
    if not variable:
        raise APIError(404, "variable_not_found", "variable not found")
    
    Value.increase_value(variable_name, user, value, key) 
    
    output = _get_progress(user,force_generation=True)[1]
    
    for aid in output["achievements"].keys():
        if len(output["achievements"][aid]["new_levels"])>0:
            del output["achievements"][aid]["levels"]
            del output["achievements"][aid]["priority"]
            del output["achievements"][aid]["goals"]
        else:
            del output["achievements"][aid]
    return output
def increase_value(request):
    """increase a value for the user"""

    user_id = int(request.matchdict["user_id"])
    try:
        value = float(request.POST["value"])
    except:
        raise APIError(400, "invalid_value", "Invalid value provided")

    key = request.matchdict["key"] if "key" in request.matchdict else ""
    variable_name = request.matchdict["variable_name"]

    user = User.get_user(user_id)
    if not user:
        raise APIError(404, "user_not_found", "user not found")

    variable = Variable.get_variable_by_name(variable_name)
    if not variable:
        raise APIError(404, "variable_not_found", "variable not found")

    Value.increase_value(variable_name, user, value, key)

    output = _get_progress(user, force_generation=True)[1]

    for aid in output["achievements"].keys():
        if len(output["achievements"][aid]["new_levels"]) > 0:
            del output["achievements"][aid]["levels"]
            del output["achievements"][aid]["priority"]
            del output["achievements"][aid]["goals"]
        else:
            del output["achievements"][aid]
    return output
Exemple #3
0
def increase_value(request):
    """increase a value for the user"""

    user_id = int(request.matchdict["user_id"])
    try:
        value = float(request.POST["value"])
    except:
        raise BadRequest("Invalid value provided")

    key = request.matchdict["key"] if request.matchdict.has_key("key") else ""
    variable_name = request.matchdict["variable_name"]

    user = User.get_user(user_id)
    if not user:
        raise NotFound("user not found")

    variable = Variable.get_variable_by_name(variable_name)
    if not variable:
        raise NotFound("variable not found")

    Value.increase_value(variable_name, user, value, key)

    output = get_progress(request, return_object=True)

    for aid in output["achievements"].keys():
        if len(output["achievements"][aid]["new_levels"]) > 0:
            del output["achievements"][aid]["levels"]
            del output["achievements"][aid]["priority"]
            del output["achievements"][aid]["goals"]
        else:
            del output["achievements"][aid]
    return output
def increase_value(request):
    """increase a value for the user"""
    
    user_id = int(request.matchdict["user_id"])
    try:
        value = float(request.POST["value"])
    except:
        raise BadRequest("Invalid value provided")
    
    key = request.matchdict["key"] if request.matchdict.has_key("key") else ""
    variable_name = request.matchdict["variable_name"]
    
    user = User.get_user(user_id)
    if not user:
        raise NotFound("user not found")
    
    variable = Variable.get_variable_by_name(variable_name)
    if not variable:
        raise NotFound("variable not found")
    
    Value.increase_value(variable_name, user, value, key) 
    
    output = get_progress(request,return_object=True)
    
    for aid in output["achievements"].keys():
        if len(output["achievements"][aid]["new_levels"])>0:
            del output["achievements"][aid]["levels"]
            del output["achievements"][aid]["priority"]
            del output["achievements"][aid]["goals"]
        else:
            del output["achievements"][aid]
    return output
def increase_multi_values(request):
    try:
        doc = request.json_body
    except:
        raise APIError(400, "invalid_json", "no valid json body")
    ret = {}
    for user_id, values in doc.items():
        user = User.get_user(user_id)
        if not user:
            raise APIError(404, "user_not_found",
                           "user %s not found" % (user_id, ))

        for variable_name, values_and_keys in values.items():
            for value_and_key in values_and_keys:
                variable = Variable.get_variable_by_name(variable_name)

                if not variable:
                    raise APIError(404, "variable_not_found",
                                   "variable %s not found" % (variable_name, ))

                if not 'value' in value_and_key:
                    raise APIError(400, "variable_not_found",
                                   "illegal value for %s" % (variable_name, ))

                value = value_and_key['value']
                key = value_and_key.get('key', '')

                Value.increase_value(variable_name, user, value, key)

        output = _get_progress(user, force_generation=True)[1]

        for aid in output["achievements"].keys():
            if len(output["achievements"][aid]["new_levels"]) > 0:
                del output["achievements"][aid]["levels"]
                del output["achievements"][aid]["priority"]
                del output["achievements"][aid]["goals"]
            else:
                del output["achievements"][aid]

        if len(output["achievements"]) > 0:
            ret[user_id] = output

    return ret
def increase_multi_values(request):
    try:
        doc = request.json_body
    except:
        raise APIError(400, "invalid_json", "no valid json body")
    ret = {}
    for user_id, values in doc.items():
        user = User.get_user(user_id)
        if not user:
            raise APIError(404, "user_not_found", "user %s not found" % (user_id,))

        for variable_name, values_and_keys in values.items():
            for value_and_key in values_and_keys:
                variable = Variable.get_variable_by_name(variable_name)
                
                if not variable:
                    raise APIError(404, "variable_not_found", "variable %s not found" % (variable_name,))

                if not 'value' in value_and_key:
                    raise APIError(400, "variable_not_found", "illegal value for %s" % (variable_name,))
                
                value = value_and_key['value']
                key = value_and_key.get('key','')
                
                Value.increase_value(variable_name, user, value, key)
    
        output = _get_progress(user,force_generation=True)[1]
        
        for aid in output["achievements"].keys():
            if len(output["achievements"][aid]["new_levels"])>0:
                del output["achievements"][aid]["levels"]
                del output["achievements"][aid]["priority"]
                del output["achievements"][aid]["goals"]
            else:
                del output["achievements"][aid]
        
        if len(output["achievements"])>0 :
            ret[user_id]=output
    
    return ret