|
1 from django.shortcuts import render_to_response, get_object_or_404 |
|
2 from django.template import RequestContext |
|
3 from django.http import Http404 |
|
4 from django.contrib.comments.models import Comment, ModeratorDeletion, UserFlag |
|
5 from django.contrib.auth.decorators import login_required |
|
6 from django.http import HttpResponseRedirect |
|
7 from django.conf import settings |
|
8 |
|
9 def flag(request, comment_id, extra_context=None, context_processors=None): |
|
10 """ |
|
11 Flags a comment. Confirmation on GET, action on POST. |
|
12 |
|
13 Templates: `comments/flag_verify`, `comments/flag_done` |
|
14 Context: |
|
15 comment |
|
16 the flagged `comments.comments` object |
|
17 """ |
|
18 if extra_context is None: extra_context = {} |
|
19 comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID) |
|
20 if request.POST: |
|
21 UserFlag.objects.flag(comment, request.user) |
|
22 return HttpResponseRedirect('%sdone/' % request.path) |
|
23 return render_to_response('comments/flag_verify.html', {'comment': comment}, |
|
24 context_instance=RequestContext(request, extra_context, context_processors)) |
|
25 flag = login_required(flag) |
|
26 |
|
27 def flag_done(request, comment_id, extra_context=None, context_processors=None): |
|
28 if extra_context is None: extra_context = {} |
|
29 comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID) |
|
30 return render_to_response('comments/flag_done.html', {'comment': comment}, |
|
31 context_instance=RequestContext(request, extra_context, context_processors)) |
|
32 |
|
33 def delete(request, comment_id, extra_context=None, context_processors=None): |
|
34 """ |
|
35 Deletes a comment. Confirmation on GET, action on POST. |
|
36 |
|
37 Templates: `comments/delete_verify`, `comments/delete_done` |
|
38 Context: |
|
39 comment |
|
40 the flagged `comments.comments` object |
|
41 """ |
|
42 if extra_context is None: extra_context = {} |
|
43 comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID) |
|
44 if not Comment.objects.user_is_moderator(request.user): |
|
45 raise Http404 |
|
46 if request.POST: |
|
47 # If the comment has already been removed, silently fail. |
|
48 if not comment.is_removed: |
|
49 comment.is_removed = True |
|
50 comment.save() |
|
51 m = ModeratorDeletion(None, request.user.id, comment.id, None) |
|
52 m.save() |
|
53 return HttpResponseRedirect('%sdone/' % request.path) |
|
54 return render_to_response('comments/delete_verify.html', {'comment': comment}, |
|
55 context_instance=RequestContext(request, extra_context, context_processors)) |
|
56 delete = login_required(delete) |
|
57 |
|
58 def delete_done(request, comment_id, extra_context=None, context_processors=None): |
|
59 if extra_context is None: extra_context = {} |
|
60 comment = get_object_or_404(Comment,pk=comment_id, site__id__exact=settings.SITE_ID) |
|
61 return render_to_response('comments/delete_done.html', {'comment': comment}, |
|
62 context_instance=RequestContext(request, extra_context, context_processors)) |