Esempio n. 1
0
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 asbool(get_settings().get("enable_user_authentication",
                                             False)):
                    if not Variable.may_increase(variable, request, user_id):
                        raise APIError(
                            403, "forbidden",
                            "You may not increase the variable %s for user %s."
                            % (variable_name, user_id))

                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(achievements_for_user=user,
                               requesting_user=request.user)
        output = copy.deepcopy(output)
        to_delete = list()
        for i in range(len(output["achievements"])):
            if len(output["achievements"][i]["new_levels"]) > 0:
                if "levels" in output["achievements"][i]:
                    del output["achievements"][i]["levels"]
                if "priority" in output["achievements"][i]:
                    del output["achievements"][i]["priority"]
                if "goals" in output["achievements"][i]:
                    del output["achievements"][i]["goals"]
            else:
                to_delete.append(i)

        for i in sorted(to_delete, reverse=True):
            del output["achievements"][i]

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

    return ret
Esempio n. 2
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:
        try:
            doc = request.json_body
            value = doc["value"]
        except:
            raise APIError(400, "invalid_value", "Invalid value provided")

    key = request.matchdict["key"] if (
        "key" in request.matchdict
        and request.matchdict["key"] is not None) 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")

    if asbool(get_settings().get("enable_user_authentication", False)):
        if not Variable.may_increase(variable, request, user_id):
            raise APIError(403, "forbidden",
                           "You may not increase the variable for this user.")

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

    output = _get_progress(achievements_for_user=user,
                           requesting_user=request.user)
    output = copy.deepcopy(output)
    to_delete = list()
    for i in range(len(output["achievements"])):
        if len(output["achievements"][i]["new_levels"]) > 0:
            if "levels" in output["achievements"][i]:
                del output["achievements"][i]["levels"]
            if "priority" in output["achievements"][i]:
                del output["achievements"][i]["priority"]
            if "goals" in output["achievements"][i]:
                del output["achievements"][i]["goals"]
        else:
            to_delete.append(i)

    for i in sorted(to_delete, reverse=True):
        del output["achievements"][i]

    return output