|
0
|
1 |
from django import template |
|
|
2 |
from django.conf import settings |
|
|
3 |
from django.shortcuts import get_object_or_404, render_to_response |
|
|
4 |
from django.contrib.auth.decorators import login_required, permission_required |
|
|
5 |
from utils import next_redirect, confirmation_view |
|
|
6 |
from django.contrib import comments |
|
|
7 |
from django.contrib.comments import signals |
|
29
|
8 |
from django.views.decorators.csrf import csrf_protect |
|
0
|
9 |
|
|
29
|
10 |
@csrf_protect |
|
|
11 |
@login_required |
|
0
|
12 |
def flag(request, comment_id, next=None): |
|
|
13 |
""" |
|
|
14 |
Flags a comment. Confirmation on GET, action on POST. |
|
|
15 |
|
|
|
16 |
Templates: `comments/flag.html`, |
|
|
17 |
Context: |
|
|
18 |
comment |
|
|
19 |
the flagged `comments.comment` object |
|
|
20 |
""" |
|
|
21 |
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) |
|
|
22 |
|
|
|
23 |
# Flag on POST |
|
|
24 |
if request.method == 'POST': |
|
29
|
25 |
perform_flag(request, comment) |
|
0
|
26 |
return next_redirect(request.POST.copy(), next, flag_done, c=comment.pk) |
|
|
27 |
|
|
|
28 |
# Render a form on GET |
|
|
29 |
else: |
|
|
30 |
return render_to_response('comments/flag.html', |
|
|
31 |
{'comment': comment, "next": next}, |
|
|
32 |
template.RequestContext(request) |
|
|
33 |
) |
|
|
34 |
|
|
29
|
35 |
@csrf_protect |
|
|
36 |
@permission_required("comments.can_moderate") |
|
0
|
37 |
def delete(request, comment_id, next=None): |
|
|
38 |
""" |
|
|
39 |
Deletes a comment. Confirmation on GET, action on POST. Requires the "can |
|
|
40 |
moderate comments" permission. |
|
|
41 |
|
|
|
42 |
Templates: `comments/delete.html`, |
|
|
43 |
Context: |
|
|
44 |
comment |
|
|
45 |
the flagged `comments.comment` object |
|
|
46 |
""" |
|
|
47 |
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) |
|
|
48 |
|
|
|
49 |
# Delete on POST |
|
|
50 |
if request.method == 'POST': |
|
|
51 |
# Flag the comment as deleted instead of actually deleting it. |
|
29
|
52 |
perform_delete(request, comment) |
|
0
|
53 |
return next_redirect(request.POST.copy(), next, delete_done, c=comment.pk) |
|
|
54 |
|
|
|
55 |
# Render a form on GET |
|
|
56 |
else: |
|
|
57 |
return render_to_response('comments/delete.html', |
|
|
58 |
{'comment': comment, "next": next}, |
|
|
59 |
template.RequestContext(request) |
|
|
60 |
) |
|
|
61 |
|
|
29
|
62 |
@csrf_protect |
|
|
63 |
@permission_required("comments.can_moderate") |
|
0
|
64 |
def approve(request, comment_id, next=None): |
|
|
65 |
""" |
|
|
66 |
Approve a comment (that is, mark it as public and non-removed). Confirmation |
|
|
67 |
on GET, action on POST. Requires the "can moderate comments" permission. |
|
|
68 |
|
|
|
69 |
Templates: `comments/approve.html`, |
|
|
70 |
Context: |
|
|
71 |
comment |
|
|
72 |
the `comments.comment` object for approval |
|
|
73 |
""" |
|
|
74 |
comment = get_object_or_404(comments.get_model(), pk=comment_id, site__pk=settings.SITE_ID) |
|
|
75 |
|
|
|
76 |
# Delete on POST |
|
|
77 |
if request.method == 'POST': |
|
|
78 |
# Flag the comment as approved. |
|
29
|
79 |
perform_approve(request, comment) |
|
0
|
80 |
return next_redirect(request.POST.copy(), next, approve_done, c=comment.pk) |
|
|
81 |
|
|
|
82 |
# Render a form on GET |
|
|
83 |
else: |
|
|
84 |
return render_to_response('comments/approve.html', |
|
|
85 |
{'comment': comment, "next": next}, |
|
|
86 |
template.RequestContext(request) |
|
|
87 |
) |
|
|
88 |
|
|
29
|
89 |
# The following functions actually perform the various flag/aprove/delete |
|
|
90 |
# actions. They've been broken out into seperate functions to that they |
|
|
91 |
# may be called from admin actions. |
|
|
92 |
|
|
|
93 |
def perform_flag(request, comment): |
|
|
94 |
""" |
|
|
95 |
Actually perform the flagging of a comment from a request. |
|
|
96 |
""" |
|
|
97 |
flag, created = comments.models.CommentFlag.objects.get_or_create( |
|
|
98 |
comment = comment, |
|
|
99 |
user = request.user, |
|
|
100 |
flag = comments.models.CommentFlag.SUGGEST_REMOVAL |
|
|
101 |
) |
|
|
102 |
signals.comment_was_flagged.send( |
|
|
103 |
sender = comment.__class__, |
|
|
104 |
comment = comment, |
|
|
105 |
flag = flag, |
|
|
106 |
created = created, |
|
|
107 |
request = request, |
|
|
108 |
) |
|
|
109 |
|
|
|
110 |
def perform_delete(request, comment): |
|
|
111 |
flag, created = comments.models.CommentFlag.objects.get_or_create( |
|
|
112 |
comment = comment, |
|
|
113 |
user = request.user, |
|
|
114 |
flag = comments.models.CommentFlag.MODERATOR_DELETION |
|
|
115 |
) |
|
|
116 |
comment.is_removed = True |
|
|
117 |
comment.save() |
|
|
118 |
signals.comment_was_flagged.send( |
|
|
119 |
sender = comment.__class__, |
|
|
120 |
comment = comment, |
|
|
121 |
flag = flag, |
|
|
122 |
created = created, |
|
|
123 |
request = request, |
|
|
124 |
) |
|
0
|
125 |
|
|
|
126 |
|
|
29
|
127 |
def perform_approve(request, comment): |
|
|
128 |
flag, created = comments.models.CommentFlag.objects.get_or_create( |
|
|
129 |
comment = comment, |
|
|
130 |
user = request.user, |
|
|
131 |
flag = comments.models.CommentFlag.MODERATOR_APPROVAL, |
|
|
132 |
) |
|
0
|
133 |
|
|
29
|
134 |
comment.is_removed = False |
|
|
135 |
comment.is_public = True |
|
|
136 |
comment.save() |
|
0
|
137 |
|
|
29
|
138 |
signals.comment_was_flagged.send( |
|
|
139 |
sender = comment.__class__, |
|
|
140 |
comment = comment, |
|
|
141 |
flag = flag, |
|
|
142 |
created = created, |
|
|
143 |
request = request, |
|
|
144 |
) |
|
0
|
145 |
|
|
29
|
146 |
# Confirmation views. |
|
0
|
147 |
|
|
|
148 |
flag_done = confirmation_view( |
|
|
149 |
template = "comments/flagged.html", |
|
|
150 |
doc = 'Displays a "comment was flagged" success page.' |
|
|
151 |
) |
|
|
152 |
delete_done = confirmation_view( |
|
|
153 |
template = "comments/deleted.html", |
|
|
154 |
doc = 'Displays a "comment was deleted" success page.' |
|
|
155 |
) |
|
|
156 |
approve_done = confirmation_view( |
|
|
157 |
template = "comments/approved.html", |
|
|
158 |
doc = 'Displays a "comment was approved" success page.' |
|
|
159 |
) |