2006-05-02 09:31:56 +08:00
|
|
|
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
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext as _
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
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"
|
2006-07-19 10:09:26 +08:00
|
|
|
if not request.user.is_authenticated():
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("Anonymous users cannot vote")
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2006-05-02 09:31:56 +08:00
|
|
|
comment = Comment.objects.get(pk=comment_id)
|
|
|
|
except Comment.DoesNotExist:
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("Invalid comment ID")
|
2006-05-02 09:31:56 +08:00
|
|
|
if comment.user.id == request.user.id:
|
2005-11-23 23:42:09 +08:00
|
|
|
raise Http404, _("No voting for yourself")
|
2006-05-02 09:31:56 +08:00
|
|
|
KarmaScore.objects.vote(request.user.id, comment_id, rating)
|
2005-07-13 09:25:57 +08:00
|
|
|
# Reload comment to ensure we have up to date karma count
|
2006-05-02 09:31:56 +08:00
|
|
|
comment = Comment.objects.get(pk=comment_id)
|
|
|
|
return render_to_response('comments/karma_vote_accepted.html', {'comment': comment}, context_instance=RequestContext(request))
|