app/django/contrib/comments/views/karma.py
author Todd Larsen <tlarsen@google.com>
Fri, 03 Oct 2008 01:32:34 +0000
changeset 263 9b39d93b677f
parent 54 03e267d67478
permissions -rw-r--r--
Make findNearestUsers() code in soc/logic/site/id_user.py more generic and move it to soc/logic/model.py. Orginal findNearest...() functions in id_user.py are now convenience wrappers. Add typed-query string construction functions to model.py. Move getFulLClassName() from key_name.py model.py, since it has more to do with Model types than key names. Swap 'offset' and 'limit' and make 'limit' arguments non-optional. Also, stop adding 1 inside the ...ForLimitAndOffset() functions and make the callers do it (since it was being added for a very UI-specific reason of whether or not to display a "Next>" link). Patch by: Todd Larsen Review by: Pawel Solyga Review URL: http://codereviews.googleopensourceprograms.com/1401

from django.http import Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.contrib.comments.models import Comment, KarmaScore
from django.utils.translation import ugettext as _

def vote(request, comment_id, vote, extra_context=None, context_processors=None):
    """
    Rate a comment (+1 or -1)

    Templates: `karma_vote_accepted`
    Context:
        comment
            `comments.comments` object being rated
    """
    if extra_context is None: extra_context = {}
    rating = {'up': 1, 'down': -1}.get(vote, False)
    if not rating:
        raise Http404, "Invalid vote"
    if not request.user.is_authenticated():
        raise Http404, _("Anonymous users cannot vote")
    try:
        comment = Comment.objects.get(pk=comment_id)
    except Comment.DoesNotExist:
        raise Http404, _("Invalid comment ID")
    if comment.user.id == request.user.id:
        raise Http404, _("No voting for yourself")
    KarmaScore.objects.vote(request.user.id, comment_id, rating)
    # Reload comment to ensure we have up to date karma count
    comment = Comment.objects.get(pk=comment_id)
    return render_to_response('comments/karma_vote_accepted.html', {'comment': comment},
        context_instance=RequestContext(request, extra_context, context_processors))