Esempio n. 1
0
def get_progress(request):
    """get all relevant data concerning the subject's progress"""
    try:
        subject_id = int(request.matchdict["subject_id"])
    except:
        raise APIError(400, "illegal_subject_id", "no valid subject_id given")

    subject = Subject.get_subject(subject_id)
    if not subject:
        raise APIError(404, "subject_not_found", "subject not found")

    try:
        achievement_id = int(request.GET["achievement_id"])
    except:
        achievement_id = None

    try:
        achievement_history = int(request.GET["achievement_history"])
    except:
        achievement_history = 2

    output = _get_progress(achievements_for_subject=subject,
                           requesting_subject=request.subject,
                           achievement_id=achievement_id,
                           achievement_history=achievement_history)
    output = copy.deepcopy(output)

    for i in range(len(output["achievements"])):
        if "new_levels" in output["achievements"][i]:
            del output["achievements"][i]["new_levels"]

    return output
Esempio n. 2
0
def increase_multi_values(request):
    try:
        doc = request.json_body
    except:
        raise APIError(400, "invalid_json", "no valid json body")

    try:
        achievement_history = int(request.GET["achievement_history"])
    except:
        achievement_history = 2

    ret = {}
    for subject_id, values in doc.items():
        subject = Subject.get_subject(subject_id)
        if not subject:
            raise APIError(404, "subject_not_found",
                           "subject %s not found" % (subject_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,
                                                 subject_id):
                        raise APIError(
                            403, "forbidden",
                            "You may not increase the variable %s for subject %s."
                            % (variable_name, subject_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, subject, value, key)

        output = _get_progress(achievements_for_subject=subject,
                               requesting_subject=request.subject,
                               achievement_history=achievement_history)
        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[subject_id] = output

    return ret
Esempio n. 3
0
def increase_value(request):
    """increase a value for the subject"""

    subject_id = int(request.matchdict["subject_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"]

    subject = Subject.get_subject(subject_id)
    if not subject:
        raise APIError(404, "subject_not_found", "subject 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 AuthUser.may_increase(variable, request, subject_id):
            raise APIError(
                403, "forbidden",
                "You may not increase the variable for this subject.")

    Value.increase_value(variable_name,
                         subject["id"],
                         value,
                         key,
                         at_datetime=dt_now())

    try:
        achievement_history = int(request.GET["achievement_history"])
    except:
        achievement_history = 2

    output = _get_progress(achievements_for_subject=subject,
                           requesting_subject=request.subject,
                           achievement_history=achievement_history)
    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