Example #1
0
def modify_karma(request):
    """ Add a Karma note to the user profile """

    if not request.user.has_perm("member.change_profile"):
        raise PermissionDenied

    try:
        profile_pk = request.POST["profile_pk"]
    except (KeyError, ValueError):
        raise Http404

    profile = get_object_or_404(Profile, pk=profile_pk)
    if profile.is_private():
        raise PermissionDenied

    note = KarmaNote()
    note.user = profile.user
    note.staff = request.user
    note.comment = request.POST["warning"]
    try:
        note.value = int(request.POST["points"])
    except (KeyError, ValueError):
        note.value = 0

    note.save()

    profile.karma += note.value
    profile.save()

    return redirect(reverse("member-detail", args=[profile.user.username]))
Example #2
0
def modify_karma(request):
    """ Add a Karma note to the user profile """

    if not request.user.has_perm("member.change_profile"):
        raise PermissionDenied

    try:
        profile_pk = int(request.POST["profile_pk"])
    except (KeyError, ValueError):
        raise Http404

    profile = get_object_or_404(Profile, pk=profile_pk)
    if profile.is_private():
        raise PermissionDenied

    note = KarmaNote()
    note.user = profile.user
    note.staff = request.user
    note.comment = request.POST.get("warning", "")

    try:
        note.value = int(request.POST["points"])
    except (KeyError, ValueError):
        note.value = 0

    try:
        if note.comment == "":
            raise ValueError("note.comment must not be empty")
        elif note.value > 100 or note.value < -100:
            raise ValueError(
                "note.value must be between -100 and 100 {} given".format(
                    note.value))
        else:
            note.save()
            profile.karma += note.value
            profile.save()
    except ValueError as e:
        logging.getLogger("zds.member").warn(
            "ValueError: modifying karma failed because {}".format(e))

    return redirect(reverse("member-detail", args=[profile.user.username]))