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():
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("Anonymous users cannot vote")
|
2005-07-13 09:25:57 +08:00
|
|
|
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:
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("Invalid comment ID")
|
2005-07-13 09:25:57 +08:00
|
|
|
if comment.user_id == request.user.id:
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("No voting for yourself")
|
2005-07-13 09:25:57 +08:00
|
|
|
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))
|