|
1 from django.http import Http404 |
|
2 from django.shortcuts import render_to_response |
|
3 from django.template import RequestContext |
|
4 from django.contrib.comments.models import Comment, KarmaScore |
|
5 from django.utils.translation import ugettext as _ |
|
6 |
|
7 def vote(request, comment_id, vote, extra_context=None, context_processors=None): |
|
8 """ |
|
9 Rate a comment (+1 or -1) |
|
10 |
|
11 Templates: `karma_vote_accepted` |
|
12 Context: |
|
13 comment |
|
14 `comments.comments` object being rated |
|
15 """ |
|
16 if extra_context is None: extra_context = {} |
|
17 rating = {'up': 1, 'down': -1}.get(vote, False) |
|
18 if not rating: |
|
19 raise Http404, "Invalid vote" |
|
20 if not request.user.is_authenticated(): |
|
21 raise Http404, _("Anonymous users cannot vote") |
|
22 try: |
|
23 comment = Comment.objects.get(pk=comment_id) |
|
24 except Comment.DoesNotExist: |
|
25 raise Http404, _("Invalid comment ID") |
|
26 if comment.user.id == request.user.id: |
|
27 raise Http404, _("No voting for yourself") |
|
28 KarmaScore.objects.vote(request.user.id, comment_id, rating) |
|
29 # Reload comment to ensure we have up to date karma count |
|
30 comment = Comment.objects.get(pk=comment_id) |
|
31 return render_to_response('comments/karma_vote_accepted.html', {'comment': comment}, |
|
32 context_instance=RequestContext(request, extra_context, context_processors)) |