2005-07-13 09:25:57 +08:00
|
|
|
from django.core.exceptions import Http404
|
2005-09-23 06:26:56 +08:00
|
|
|
from django.core.extensions import DjangoContext, render_to_response
|
2005-07-13 09:25:57 +08:00
|
|
|
from django.models.comments import comments, karma
|
|
|
|
|
|
|
|
def vote(request, comment_id, vote):
|
|
|
|
"""
|
|
|
|
Rate a comment (+1 or -1)
|
|
|
|
|
|
|
|
Templates: `karma_vote_accepted`
|
|
|
|
Context:
|
|
|
|
comment
|
|
|
|
`comments.comments` object being rated
|
|
|
|
"""
|
|
|
|
rating = {'up': 1, 'down': -1}.get(vote, False)
|
|
|
|
if not rating:
|
|
|
|
raise Http404, "Invalid vote"
|
|
|
|
if request.user.is_anonymous():
|
|
|
|
raise Http404, "Anonymous users cannot vote"
|
|
|
|
try:
|
2005-07-27 00:11:43 +08:00
|
|
|
comment = comments.get_object(pk=comment_id)
|
2005-07-13 09:25:57 +08:00
|
|
|
except comments.CommentDoesNotExist:
|
|
|
|
raise Http404, "Invalid comment ID"
|
|
|
|
if comment.user_id == request.user.id:
|
|
|
|
raise Http404, "No voting for yourself"
|
|
|
|
karma.vote(request.user.id, comment_id, rating)
|
|
|
|
# Reload comment to ensure we have up to date karma count
|
2005-07-27 00:11:43 +08:00
|
|
|
comment = comments.get_object(pk=comment_id)
|
2005-09-23 06:26:56 +08:00
|
|
|
return render_to_response('comments/karma_vote_accepted', {'comment': comment}, context_instance=DjangoContext(request))
|