Exemple #1
0
def vote_solution():
    sol_id = request.form.get('id', 0, type=int)
    sol = Solution.query.get(sol_id)

    is_upvote = request.form.get('is_upvote', 1, type=int)

    if is_upvote:
        assoc = current_user.has_upvoted(sol)
        if assoc:
            current_user.solutions_voted.remove(assoc)
            err_msg = 'Error in removing the upvote from the solution.'
            msg = 'Solution upvote removed.'
        else:
            # create a new assoc
            current_user.sols_voted.append(sol)
            err_msg = 'Error in upvoting the solution.'
            msg = 'Solution Upvoted.'
    else:
        assoc = current_user.has_downvoted(sol)
        if assoc:
            current_user.solutions_voted.remove(assoc)
            err_msg = 'Error in removing the downvote from the solution.'
            msg = 'Solution downvote removed.'
        else:
            assoc = UDS(sol)
            assoc.is_upvote = is_upvote
            current_user.solutions_voted.append(assoc)
            err_msg = 'Error in downvoting the solution.'
            msg = 'Solution downvoted.'

    add_to_db_ajax(current_user, err_msg)
    return jsonify(message=msg)
Exemple #2
0
def comment_blankvote(slug, comment_id):
    # Ensure comment belongs to slug
    # prop = Proposal.get(Proposal.slug == slug)
    comment = Comment.get(Comment.id == comment_id)

    result = {"diff": '0', "score": str(comment.score)}

    if current_user.id == comment.user_id or current_user.karma <= 0:
        return jsonify(result)

    score_mod = 1

    if current_user.has_upvoted(comment):
        comment.score -= 1
        score_mod = 2
        result["diff"] = str(-score_mod)
    elif current_user.has_downvoted(comment):
        comment.score += 1
        score_mod = 2
        result["diff"] = str(score_mod)

    Comment.save(comment)

    (CommentVote.insert(
        comment=comment, user=current_user.id, vote=0).on_conflict(
            conflict_target=[CommentVote.user, CommentVote.comment],
            preserve=[CommentVote.vote, CommentVote.timestamp]).execute())

    # FIXME move karma to another table
    # User.save(current_user)

    result["score"] = str(comment.score)

    return jsonify(result)
Exemple #3
0
def downvote():
    ques_id = request.form.get('question-id', 0, type=int)
    ques = Question.query.get_or_404(ques_id)

    if current_user.has_upvoted(ques):
        msg = "You can't upvote and downvote the same question"
        return bad_request(msg)

    if current_user.has_downvoted(ques):
        assoc = current_user.questions_downvoted.filter(
            DQ.question == ques).one()

        current_user.questions_downvoted.remove(assoc)
        msg = 'Downvote Removed'
    else:
        current_user.ques_downvoted.append(ques)
        msg = 'Downvoted'

    add_to_db_ajax(current_user, 'Downvote unsuccessful', 500)

    return jsonify(message=msg)
Exemple #4
0
def comment_upvote(slug, comment_id):
    # Ensure comment belongs to slug
    # prop = Proposal.get(Proposal.slug == slug)
    comment = Comment.get(Comment.id == comment_id)

    result = {"diff": '0', "score": str(comment.score)}

    if current_user.id == comment.user_id or current_user.karma <= 0:
        return jsonify(result)

    if current_user.has_upvoted(comment):
        # current_user.karma += 1
        comment.score -= 1
        CommentVote.delete().where((CommentVote.comment == comment) & (
            CommentVote.user_id == current_user.id)).execute()
        result["diff"] = '-1'
    else:
        # current_user.karma -= 1
        score_mod = 1
        if current_user.has_downvoted(comment):
            score_mod = 2

        comment.score += score_mod

        result["diff"] = '+' + str(score_mod)

        (CommentVote.insert(
            comment=comment, user=current_user.id, vote=1).on_conflict(
                conflict_target=[CommentVote.user, CommentVote.comment],
                preserve=[CommentVote.vote, CommentVote.timestamp]).execute())

    Comment.save(comment)
    # FIXME move karma to another table
    User.save(current_user)

    result["score"] = str(comment.score)

    return jsonify(result)
Exemple #5
0
def proposal_downvote(slug):
    prop = Proposal.get(Proposal.slug == slug)

    result = {"diff": '0', "votes": str(prop.votes)}

    if current_user.id == prop.author_id or current_user.karma <= 0:
        return jsonify(result)

    if current_user.has_downvoted(prop):
        # current_user.karma += 1
        prop.author.karma += 1
        prop.downvotes -= 1
        PostVote.delete().where((PostVote.post == prop) & (
            PostVote.user_id == current_user.id)).execute()
        result["diff"] = '+1'

    else:
        # current_user.karma -= 1
        prop.author.karma -= 1
        score_mod = 1
        if current_user.has_upvoted(prop):
            score_mod = 2
        result["diff"] = str(-score_mod)
        prop.downvotes += score_mod

        (PostVote.insert(post=prop, user=current_user.id, vote=-1).on_conflict(
            conflict_target=[PostVote.user, PostVote.post],
            preserve=[PostVote.vote, PostVote.timestamp]).execute())

    Proposal.save(prop)

    # FIXME move karma to another table
    User.save(current_user)
    User.save(prop.author)

    result["votes"] = str(prop.votes)

    return jsonify(result)