bgneal@81
|
1 """
|
bgneal@81
|
2 Views for the forums application.
|
bgneal@81
|
3 """
|
bgneal@113
|
4 import datetime
|
bgneal@113
|
5
|
bgneal@83
|
6 from django.contrib.auth.decorators import login_required
|
bgneal@82
|
7 from django.http import Http404
|
bgneal@98
|
8 from django.http import HttpResponse
|
bgneal@89
|
9 from django.http import HttpResponseBadRequest
|
bgneal@90
|
10 from django.http import HttpResponseForbidden
|
bgneal@83
|
11 from django.http import HttpResponseRedirect
|
bgneal@83
|
12 from django.core.urlresolvers import reverse
|
bgneal@91
|
13 from django.core.paginator import InvalidPage
|
bgneal@82
|
14 from django.shortcuts import get_object_or_404
|
bgneal@81
|
15 from django.shortcuts import render_to_response
|
bgneal@97
|
16 from django.template.loader import render_to_string
|
bgneal@81
|
17 from django.template import RequestContext
|
bgneal@89
|
18 from django.views.decorators.http import require_POST
|
bgneal@108
|
19 from django.utils.text import wrap
|
bgneal@81
|
20
|
bgneal@90
|
21 from core.paginator import DiggPaginator
|
bgneal@98
|
22 from core.functions import email_admins
|
bgneal@113
|
23 from forums.models import Forum, Topic, Post, FlaggedPost, TopicLastVisit, \
|
bgneal@113
|
24 ForumLastVisit
|
bgneal@115
|
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
|
bgneal@115
|
26 SplitTopicForm
|
bgneal@114
|
27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \
|
bgneal@167
|
28 get_post_unread_status, get_unread_topics
|
bgneal@81
|
29
|
bgneal@117
|
30 from bio.models import UserProfile
|
bgneal@90
|
31 #######################################################################
|
bgneal@90
|
32
|
bgneal@93
|
33 TOPICS_PER_PAGE = 50
|
bgneal@113
|
34 POSTS_PER_PAGE = 20
|
bgneal@90
|
35
|
bgneal@167
|
36
|
bgneal@167
|
37 def get_page_num(request):
|
bgneal@167
|
38 """Returns the value of the 'page' variable in GET if it exists, or 1
|
bgneal@167
|
39 if it does not."""
|
bgneal@167
|
40
|
bgneal@167
|
41 try:
|
bgneal@167
|
42 page_num = int(request.GET.get('page', 1))
|
bgneal@167
|
43 except ValueError:
|
bgneal@167
|
44 page_num = 1
|
bgneal@167
|
45
|
bgneal@167
|
46 return page_num
|
bgneal@167
|
47
|
bgneal@167
|
48
|
bgneal@93
|
49 def create_topic_paginator(topics):
|
bgneal@93
|
50 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@93
|
51
|
bgneal@93
|
52 def create_post_paginator(posts):
|
bgneal@93
|
53 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
|
bgneal@90
|
54
|
bgneal@167
|
55
|
bgneal@167
|
56 def attach_topic_page_ranges(topics):
|
bgneal@167
|
57 """Attaches a page_range attribute to each topic in the supplied list.
|
bgneal@167
|
58 This attribute will be None if it is a single page topic. This is used
|
bgneal@167
|
59 by the templates to generate "goto page x" links.
|
bgneal@167
|
60 """
|
bgneal@167
|
61 for topic in topics:
|
bgneal@167
|
62 if topic.post_count > POSTS_PER_PAGE:
|
bgneal@167
|
63 pp = DiggPaginator(range(topic.post_count), POSTS_PER_PAGE,
|
bgneal@167
|
64 body=2, tail=3, margin=1)
|
bgneal@167
|
65 topic.page_range = pp.page(1).page_range
|
bgneal@167
|
66 else:
|
bgneal@167
|
67 topic.page_range = None
|
bgneal@167
|
68
|
bgneal@90
|
69 #######################################################################
|
bgneal@81
|
70
|
bgneal@81
|
71 def index(request):
|
bgneal@82
|
72 """
|
bgneal@82
|
73 This view displays all the forums available, ordered in each category.
|
bgneal@82
|
74 """
|
bgneal@167
|
75 # check for special forum queries
|
bgneal@167
|
76 query = request.GET.get("query")
|
bgneal@167
|
77 if query == "unread":
|
bgneal@167
|
78 return HttpResponseRedirect(reverse('forums-unread_topics'))
|
bgneal@167
|
79
|
bgneal@100
|
80 forums = Forum.objects.forums_for_user(request.user)
|
bgneal@113
|
81 get_forum_unread_status(forums, request.user)
|
bgneal@81
|
82 cats = {}
|
bgneal@81
|
83 for forum in forums:
|
bgneal@81
|
84 cat = cats.setdefault(forum.category.id, {
|
bgneal@81
|
85 'cat': forum.category,
|
bgneal@81
|
86 'forums': [],
|
bgneal@81
|
87 })
|
bgneal@81
|
88 cat['forums'].append(forum)
|
bgneal@81
|
89
|
bgneal@81
|
90 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
|
bgneal@81
|
91 cats = sorted(cats.values(), cmpdef)
|
bgneal@81
|
92
|
bgneal@81
|
93 return render_to_response('forums/index.html', {
|
bgneal@81
|
94 'cats': cats,
|
bgneal@81
|
95 },
|
bgneal@81
|
96 context_instance=RequestContext(request))
|
bgneal@81
|
97
|
bgneal@82
|
98
|
bgneal@81
|
99 def forum_index(request, slug):
|
bgneal@82
|
100 """
|
bgneal@82
|
101 Displays all the topics in a forum.
|
bgneal@82
|
102 """
|
bgneal@101
|
103 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
104
|
bgneal@100
|
105 if not forum.category.can_access(request.user):
|
bgneal@100
|
106 return HttpResponseForbidden()
|
bgneal@100
|
107
|
bgneal@107
|
108 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@114
|
109 get_topic_unread_status(forum, topics, request.user)
|
bgneal@114
|
110
|
bgneal@93
|
111 paginator = create_topic_paginator(topics)
|
bgneal@167
|
112 page_num = get_page_num(request)
|
bgneal@93
|
113 try:
|
bgneal@93
|
114 page = paginator.page(page_num)
|
bgneal@93
|
115 except InvalidPage:
|
bgneal@93
|
116 raise Http404
|
bgneal@97
|
117
|
bgneal@167
|
118 attach_topic_page_ranges(page.object_list)
|
bgneal@161
|
119
|
bgneal@97
|
120 # we do this for the template since it is rendered twice
|
bgneal@97
|
121 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
122
|
bgneal@111
|
123 can_moderate = _can_moderate(forum, request.user)
|
bgneal@82
|
124
|
bgneal@82
|
125 return render_to_response('forums/forum_index.html', {
|
bgneal@82
|
126 'forum': forum,
|
bgneal@93
|
127 'page': page,
|
bgneal@97
|
128 'page_nav': page_nav,
|
bgneal@111
|
129 'can_moderate': can_moderate,
|
bgneal@82
|
130 },
|
bgneal@82
|
131 context_instance=RequestContext(request))
|
bgneal@82
|
132
|
bgneal@82
|
133
|
bgneal@82
|
134 def topic_index(request, id):
|
bgneal@82
|
135 """
|
bgneal@82
|
136 Displays all the posts in a topic.
|
bgneal@82
|
137 """
|
bgneal@101
|
138 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@100
|
139
|
bgneal@100
|
140 if not topic.forum.category.can_access(request.user):
|
bgneal@100
|
141 return HttpResponseForbidden()
|
bgneal@100
|
142
|
bgneal@86
|
143 topic.view_count += 1
|
bgneal@86
|
144 topic.save()
|
bgneal@86
|
145
|
bgneal@86
|
146 posts = topic.posts.select_related()
|
bgneal@114
|
147
|
bgneal@93
|
148 paginator = create_post_paginator(posts)
|
bgneal@167
|
149 page_num = get_page_num(request)
|
bgneal@90
|
150 try:
|
bgneal@90
|
151 page = paginator.page(page_num)
|
bgneal@90
|
152 except InvalidPage:
|
bgneal@90
|
153 raise Http404
|
bgneal@117
|
154 get_post_unread_status(topic, page.object_list, request.user)
|
bgneal@117
|
155
|
bgneal@117
|
156 # Attach user profiles to each post to avoid using get_user_profile() in
|
bgneal@117
|
157 # the template.
|
bgneal@117
|
158 users = set(post.user.id for post in page.object_list)
|
bgneal@117
|
159
|
bgneal@117
|
160 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
|
bgneal@117
|
161 user_profiles = dict((profile.user.id, profile) for profile in profiles)
|
bgneal@117
|
162
|
bgneal@117
|
163 for post in page.object_list:
|
bgneal@117
|
164 post.user_profile = user_profiles[post.user.id]
|
bgneal@90
|
165
|
bgneal@90
|
166 last_page = page_num == paginator.num_pages
|
bgneal@86
|
167
|
bgneal@113
|
168 if request.user.is_authenticated() and last_page:
|
bgneal@113
|
169 _update_last_visit(request.user, topic)
|
bgneal@113
|
170
|
bgneal@97
|
171 # we do this for the template since it is rendered twice
|
bgneal@97
|
172 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@97
|
173
|
bgneal@109
|
174 can_moderate = _can_moderate(topic.forum, request.user)
|
bgneal@104
|
175
|
bgneal@104
|
176 can_reply = request.user.is_authenticated() and (
|
bgneal@104
|
177 not topic.locked or can_moderate)
|
bgneal@104
|
178
|
bgneal@86
|
179 return render_to_response('forums/topic.html', {
|
bgneal@86
|
180 'forum': topic.forum,
|
bgneal@86
|
181 'topic': topic,
|
bgneal@90
|
182 'page': page,
|
bgneal@97
|
183 'page_nav': page_nav,
|
bgneal@87
|
184 'last_page': last_page,
|
bgneal@104
|
185 'can_moderate': can_moderate,
|
bgneal@104
|
186 'can_reply': can_reply,
|
bgneal@106
|
187 'form': NewPostForm(initial={'topic_id': topic.id}),
|
bgneal@86
|
188 },
|
bgneal@86
|
189 context_instance=RequestContext(request))
|
bgneal@83
|
190
|
bgneal@83
|
191
|
bgneal@83
|
192 @login_required
|
bgneal@83
|
193 def new_topic(request, slug):
|
bgneal@83
|
194 """
|
bgneal@83
|
195 This view handles the creation of new topics.
|
bgneal@83
|
196 """
|
bgneal@101
|
197 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@100
|
198
|
bgneal@100
|
199 if not forum.category.can_access(request.user):
|
bgneal@100
|
200 return HttpResponseForbidden()
|
bgneal@100
|
201
|
bgneal@83
|
202 if request.method == 'POST':
|
bgneal@102
|
203 form = NewTopicForm(request.user, forum, request.POST)
|
bgneal@83
|
204 if form.is_valid():
|
bgneal@102
|
205 topic = form.save(request.META.get("REMOTE_ADDR"))
|
bgneal@108
|
206 _bump_post_count(request.user)
|
bgneal@83
|
207 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
|
bgneal@83
|
208 kwargs={'tid': topic.pk}))
|
bgneal@83
|
209 else:
|
bgneal@102
|
210 form = NewTopicForm(request.user, forum)
|
bgneal@83
|
211
|
bgneal@83
|
212 return render_to_response('forums/new_topic.html', {
|
bgneal@83
|
213 'forum': forum,
|
bgneal@83
|
214 'form': form,
|
bgneal@83
|
215 },
|
bgneal@83
|
216 context_instance=RequestContext(request))
|
bgneal@83
|
217
|
bgneal@83
|
218
|
bgneal@83
|
219 @login_required
|
bgneal@83
|
220 def new_topic_thanks(request, tid):
|
bgneal@83
|
221 """
|
bgneal@83
|
222 This view displays the success page for a newly created topic.
|
bgneal@83
|
223 """
|
bgneal@101
|
224 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
|
bgneal@83
|
225 return render_to_response('forums/new_topic_thanks.html', {
|
bgneal@83
|
226 'forum': topic.forum,
|
bgneal@83
|
227 'topic': topic,
|
bgneal@83
|
228 },
|
bgneal@83
|
229 context_instance=RequestContext(request))
|
bgneal@89
|
230
|
bgneal@89
|
231
|
bgneal@89
|
232 @require_POST
|
bgneal@89
|
233 def quick_reply_ajax(request):
|
bgneal@89
|
234 """
|
bgneal@89
|
235 This function handles the quick reply to a thread function. This
|
bgneal@89
|
236 function is meant to be the target of an AJAX post, and returns
|
bgneal@89
|
237 the HTML for the new post, which the client-side script appends
|
bgneal@89
|
238 to the document.
|
bgneal@89
|
239 """
|
bgneal@90
|
240 if not request.user.is_authenticated():
|
bgneal@108
|
241 return HttpResponseForbidden('Please login or register to post.')
|
bgneal@90
|
242
|
bgneal@106
|
243 form = NewPostForm(request.POST)
|
bgneal@89
|
244 if form.is_valid():
|
bgneal@108
|
245 if not _can_post_in_topic(form.topic, request.user):
|
bgneal@108
|
246 return HttpResponseForbidden("You don't have permission to post in this topic.")
|
bgneal@100
|
247
|
bgneal@108
|
248 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
|
bgneal@114
|
249 post.unread = True
|
bgneal@122
|
250 post.user_profile = request.user.get_profile()
|
bgneal@108
|
251 _bump_post_count(request.user)
|
bgneal@113
|
252 _update_last_visit(request.user, form.topic)
|
bgneal@89
|
253 return render_to_response('forums/display_post.html', {
|
bgneal@89
|
254 'post': post,
|
bgneal@113
|
255 'can_moderate': _can_moderate(form.topic.forum, request.user),
|
bgneal@120
|
256 'can_reply': True,
|
bgneal@89
|
257 },
|
bgneal@89
|
258 context_instance=RequestContext(request))
|
bgneal@89
|
259
|
bgneal@108
|
260 return HttpResponseBadRequest("Invalid post.");
|
bgneal@89
|
261
|
bgneal@91
|
262
|
bgneal@91
|
263 def goto_post(request, post_id):
|
bgneal@91
|
264 """
|
bgneal@91
|
265 This function calculates what page a given post is on, then redirects
|
bgneal@91
|
266 to that URL. This function is the target of get_absolute_url() for
|
bgneal@91
|
267 Post objects.
|
bgneal@91
|
268 """
|
bgneal@101
|
269 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
|
bgneal@91
|
270 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
|
bgneal@91
|
271 page = count / POSTS_PER_PAGE + 1
|
bgneal@91
|
272 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
|
bgneal@91
|
273 '?page=%s#p%s' % (page, post.id)
|
bgneal@91
|
274 return HttpResponseRedirect(url)
|
bgneal@91
|
275
|
bgneal@98
|
276
|
bgneal@98
|
277 @require_POST
|
bgneal@98
|
278 def flag_post(request):
|
bgneal@98
|
279 """
|
bgneal@98
|
280 This function handles the flagging of posts by users. This function should
|
bgneal@98
|
281 be the target of an AJAX post.
|
bgneal@98
|
282 """
|
bgneal@98
|
283 if not request.user.is_authenticated():
|
bgneal@99
|
284 return HttpResponseForbidden('Please login or register to flag a post.')
|
bgneal@98
|
285
|
bgneal@98
|
286 id = request.POST.get('id')
|
bgneal@98
|
287 if id is None:
|
bgneal@98
|
288 return HttpResponseBadRequest('No post id')
|
bgneal@98
|
289
|
bgneal@98
|
290 try:
|
bgneal@98
|
291 post = Post.objects.get(pk=id)
|
bgneal@98
|
292 except Post.DoesNotExist:
|
bgneal@98
|
293 return HttpResponseBadRequest('No post with id %s' % id)
|
bgneal@98
|
294
|
bgneal@98
|
295 flag = FlaggedPost(user=request.user, post=post)
|
bgneal@98
|
296 flag.save()
|
bgneal@98
|
297 email_admins('A Post Has Been Flagged', """Hello,
|
bgneal@98
|
298
|
bgneal@98
|
299 A user has flagged a forum post for review.
|
bgneal@98
|
300 """)
|
bgneal@98
|
301 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
|
bgneal@98
|
302 'Thanks for helping to improve the discussions on this site.')
|
bgneal@106
|
303
|
bgneal@106
|
304
|
bgneal@106
|
305 @login_required
|
bgneal@106
|
306 def edit_post(request, id):
|
bgneal@106
|
307 """
|
bgneal@106
|
308 This view function allows authorized users to edit posts.
|
bgneal@106
|
309 The superuser, forum moderators, and original author can edit posts.
|
bgneal@106
|
310 """
|
bgneal@106
|
311 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@108
|
312
|
bgneal@109
|
313 can_moderate = _can_moderate(post.topic.forum, request.user)
|
bgneal@108
|
314 can_edit = can_moderate or request.user == post.user
|
bgneal@106
|
315
|
bgneal@106
|
316 if not can_edit:
|
bgneal@106
|
317 return HttpResponseForbidden("You don't have permission to edit that post.")
|
bgneal@106
|
318
|
bgneal@106
|
319 if request.method == "POST":
|
bgneal@106
|
320 form = PostForm(request.POST, instance=post)
|
bgneal@106
|
321 if form.is_valid():
|
bgneal@115
|
322 post = form.save(commit=False)
|
bgneal@115
|
323 post.touch()
|
bgneal@115
|
324 post.save()
|
bgneal@106
|
325 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@106
|
326 else:
|
bgneal@106
|
327 form = PostForm(instance=post)
|
bgneal@106
|
328
|
bgneal@123
|
329 post.user_profile = request.user.get_profile()
|
bgneal@123
|
330
|
bgneal@106
|
331 return render_to_response('forums/edit_post.html', {
|
bgneal@106
|
332 'forum': post.topic.forum,
|
bgneal@106
|
333 'topic': post.topic,
|
bgneal@106
|
334 'post': post,
|
bgneal@106
|
335 'form': form,
|
bgneal@108
|
336 'can_moderate': can_moderate,
|
bgneal@106
|
337 },
|
bgneal@106
|
338 context_instance=RequestContext(request))
|
bgneal@107
|
339
|
bgneal@107
|
340
|
bgneal@107
|
341 @require_POST
|
bgneal@107
|
342 def delete_post(request):
|
bgneal@107
|
343 """
|
bgneal@107
|
344 This view function allows superusers and forum moderators to delete posts.
|
bgneal@107
|
345 This function is the target of AJAX calls from the client.
|
bgneal@107
|
346 """
|
bgneal@107
|
347 if not request.user.is_authenticated():
|
bgneal@107
|
348 return HttpResponseForbidden('Please login to delete a post.')
|
bgneal@107
|
349
|
bgneal@107
|
350 id = request.POST.get('id')
|
bgneal@107
|
351 if id is None:
|
bgneal@107
|
352 return HttpResponseBadRequest('No post id')
|
bgneal@107
|
353
|
bgneal@107
|
354 post = get_object_or_404(Post.objects.select_related(), pk=id)
|
bgneal@107
|
355
|
bgneal@107
|
356 can_delete = request.user.is_superuser or \
|
bgneal@107
|
357 request.user in post.topic.forum.moderators.all()
|
bgneal@107
|
358
|
bgneal@107
|
359 if not can_delete:
|
bgneal@107
|
360 return HttpResponseForbidden("You don't have permission to delete that post.")
|
bgneal@107
|
361
|
bgneal@147
|
362 delete_single_post(post)
|
bgneal@147
|
363 return HttpResponse("The post has been deleted.")
|
bgneal@147
|
364
|
bgneal@147
|
365
|
bgneal@147
|
366 def delete_single_post(post):
|
bgneal@147
|
367 """
|
bgneal@147
|
368 This function deletes a single post. It handles the case of where
|
bgneal@147
|
369 a post is the sole post in a topic by deleting the topic also. It
|
bgneal@147
|
370 adjusts any foreign keys in Topic or Forum objects that might be pointing
|
bgneal@147
|
371 to this post before deleting the post to avoid a cascading delete.
|
bgneal@147
|
372 """
|
bgneal@107
|
373 if post.topic.post_count == 1 and post == post.topic.last_post:
|
bgneal@107
|
374 _delete_topic(post.topic)
|
bgneal@107
|
375 else:
|
bgneal@107
|
376 _delete_post(post)
|
bgneal@107
|
377
|
bgneal@107
|
378
|
bgneal@107
|
379 def _delete_post(post):
|
bgneal@107
|
380 """
|
bgneal@107
|
381 Internal function to delete a single post object.
|
bgneal@107
|
382 Decrements the post author's post count.
|
bgneal@107
|
383 Adjusts the parent topic and forum's last_post as needed.
|
bgneal@107
|
384 """
|
bgneal@107
|
385 # Adjust post creator's post count
|
bgneal@107
|
386 profile = post.user.get_profile()
|
bgneal@107
|
387 if profile.forum_post_count > 0:
|
bgneal@107
|
388 profile.forum_post_count -= 1
|
bgneal@107
|
389 profile.save()
|
bgneal@107
|
390
|
bgneal@107
|
391 # If this post is the last_post in a topic, we need to update
|
bgneal@107
|
392 # both the topic and parent forum's last post fields. If we don't
|
bgneal@107
|
393 # the cascading delete will delete them also!
|
bgneal@107
|
394
|
bgneal@107
|
395 topic = post.topic
|
bgneal@107
|
396 if topic.last_post == post:
|
bgneal@107
|
397 topic.last_post_pre_delete()
|
bgneal@107
|
398 topic.save()
|
bgneal@107
|
399
|
bgneal@107
|
400 forum = topic.forum
|
bgneal@107
|
401 if forum.last_post == post:
|
bgneal@107
|
402 forum.last_post_pre_delete()
|
bgneal@107
|
403 forum.save()
|
bgneal@107
|
404
|
bgneal@107
|
405 # Should be safe to delete the post now:
|
bgneal@107
|
406 post.delete()
|
bgneal@107
|
407
|
bgneal@107
|
408
|
bgneal@107
|
409 def _delete_topic(topic):
|
bgneal@107
|
410 """
|
bgneal@107
|
411 Internal function to delete an entire topic.
|
bgneal@107
|
412 Deletes the topic and all posts contained within.
|
bgneal@107
|
413 Adjusts the parent forum's last_post as needed.
|
bgneal@107
|
414 Note that we don't bother adjusting all the users'
|
bgneal@107
|
415 post counts as that doesn't seem to be worth the effort.
|
bgneal@107
|
416 """
|
bgneal@147
|
417 if topic.forum.last_post and topic.forum.last_post.topic == topic:
|
bgneal@107
|
418 topic.forum.last_post_pre_delete()
|
bgneal@107
|
419 topic.forum.save()
|
bgneal@107
|
420
|
bgneal@107
|
421 # It should be safe to just delete the topic now. This will
|
bgneal@107
|
422 # automatically delete all posts in the topic.
|
bgneal@107
|
423 topic.delete()
|
bgneal@108
|
424
|
bgneal@108
|
425
|
bgneal@108
|
426 @login_required
|
bgneal@108
|
427 def new_post(request, topic_id):
|
bgneal@108
|
428 """
|
bgneal@108
|
429 This function is the view for creating a normal, non-quick reply
|
bgneal@108
|
430 to a topic.
|
bgneal@108
|
431 """
|
bgneal@108
|
432 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
|
bgneal@108
|
433 can_post = _can_post_in_topic(topic, request.user)
|
bgneal@108
|
434
|
bgneal@108
|
435 if can_post:
|
bgneal@108
|
436 if request.method == 'POST':
|
bgneal@108
|
437 form = PostForm(request.POST)
|
bgneal@108
|
438 if form.is_valid():
|
bgneal@108
|
439 post = form.save(commit=False)
|
bgneal@108
|
440 post.topic = topic
|
bgneal@108
|
441 post.user = request.user
|
bgneal@108
|
442 post.user_ip = request.META.get("REMOTE_ADDR", "")
|
bgneal@108
|
443 post.save()
|
bgneal@108
|
444 _bump_post_count(request.user)
|
bgneal@113
|
445 _update_last_visit(request.user, topic)
|
bgneal@108
|
446 return HttpResponseRedirect(post.get_absolute_url())
|
bgneal@108
|
447 else:
|
bgneal@108
|
448 quote_id = request.GET.get('quote')
|
bgneal@108
|
449 if quote_id:
|
bgneal@108
|
450 quote_post = get_object_or_404(Post.objects.select_related(),
|
bgneal@108
|
451 pk=quote_id)
|
bgneal@108
|
452 form = PostForm(initial={'body': _quote_message(quote_post.user.username,
|
bgneal@108
|
453 quote_post.body)})
|
bgneal@108
|
454 else:
|
bgneal@108
|
455 form = PostForm()
|
bgneal@108
|
456 else:
|
bgneal@108
|
457 form = None
|
bgneal@108
|
458
|
bgneal@108
|
459 return render_to_response('forums/new_post.html', {
|
bgneal@108
|
460 'forum': topic.forum,
|
bgneal@108
|
461 'topic': topic,
|
bgneal@108
|
462 'form': form,
|
bgneal@108
|
463 'can_post': can_post,
|
bgneal@108
|
464 },
|
bgneal@108
|
465 context_instance=RequestContext(request))
|
bgneal@108
|
466
|
bgneal@108
|
467
|
bgneal@109
|
468 @login_required
|
bgneal@109
|
469 def mod_topic_stick(request, id):
|
bgneal@109
|
470 """
|
bgneal@109
|
471 This view function is for moderators to toggle the sticky status of a topic.
|
bgneal@109
|
472 """
|
bgneal@109
|
473 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
474 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
475 topic.sticky = not topic.sticky
|
bgneal@109
|
476 topic.save()
|
bgneal@109
|
477 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
478
|
bgneal@110
|
479 return HttpResponseForbidden()
|
bgneal@109
|
480
|
bgneal@109
|
481
|
bgneal@109
|
482 @login_required
|
bgneal@109
|
483 def mod_topic_lock(request, id):
|
bgneal@109
|
484 """
|
bgneal@109
|
485 This view function is for moderators to toggle the locked status of a topic.
|
bgneal@109
|
486 """
|
bgneal@109
|
487 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
488 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
489 topic.locked = not topic.locked
|
bgneal@109
|
490 topic.save()
|
bgneal@109
|
491 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@109
|
492
|
bgneal@110
|
493 return HttpResponseForbidden()
|
bgneal@109
|
494
|
bgneal@109
|
495
|
bgneal@109
|
496 @login_required
|
bgneal@109
|
497 def mod_topic_delete(request, id):
|
bgneal@109
|
498 """
|
bgneal@109
|
499 This view function is for moderators to delete an entire topic.
|
bgneal@109
|
500 """
|
bgneal@109
|
501 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@109
|
502 if _can_moderate(topic.forum, request.user):
|
bgneal@109
|
503 forum_url = topic.forum.get_absolute_url()
|
bgneal@109
|
504 _delete_topic(topic)
|
bgneal@109
|
505 return HttpResponseRedirect(forum_url)
|
bgneal@109
|
506
|
bgneal@110
|
507 return HttpResponseForbidden()
|
bgneal@110
|
508
|
bgneal@110
|
509
|
bgneal@110
|
510 @login_required
|
bgneal@110
|
511 def mod_topic_move(request, id):
|
bgneal@110
|
512 """
|
bgneal@110
|
513 This view function is for moderators to move a topic to a different forum.
|
bgneal@110
|
514 """
|
bgneal@110
|
515 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@110
|
516 if not _can_moderate(topic.forum, request.user):
|
bgneal@110
|
517 return HttpResponseForbidden()
|
bgneal@110
|
518
|
bgneal@110
|
519 if request.method == 'POST':
|
bgneal@110
|
520 form = MoveTopicForm(request.user, request.POST)
|
bgneal@110
|
521 if form.is_valid():
|
bgneal@110
|
522 new_forum = form.cleaned_data['forums']
|
bgneal@110
|
523 old_forum = topic.forum
|
bgneal@111
|
524 _move_topic(topic, old_forum, new_forum)
|
bgneal@110
|
525 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@110
|
526 else:
|
bgneal@110
|
527 form = MoveTopicForm(request.user)
|
bgneal@110
|
528
|
bgneal@110
|
529 return render_to_response('forums/move_topic.html', {
|
bgneal@110
|
530 'forum': topic.forum,
|
bgneal@110
|
531 'topic': topic,
|
bgneal@110
|
532 'form': form,
|
bgneal@110
|
533 },
|
bgneal@110
|
534 context_instance=RequestContext(request))
|
bgneal@109
|
535
|
bgneal@109
|
536
|
bgneal@111
|
537 @login_required
|
bgneal@111
|
538 def mod_forum(request, slug):
|
bgneal@111
|
539 """
|
bgneal@111
|
540 Displays a view to allow moderators to perform various operations
|
bgneal@111
|
541 on topics in a forum in bulk. We currently support mass locking/unlocking,
|
bgneal@111
|
542 stickying and unstickying, moving, and deleting topics.
|
bgneal@111
|
543 """
|
bgneal@111
|
544 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@111
|
545 if not _can_moderate(forum, request.user):
|
bgneal@111
|
546 return HttpResponseForbidden()
|
bgneal@111
|
547
|
bgneal@111
|
548 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
|
bgneal@111
|
549 paginator = create_topic_paginator(topics)
|
bgneal@167
|
550 page_num = get_page_num(request)
|
bgneal@111
|
551 try:
|
bgneal@111
|
552 page = paginator.page(page_num)
|
bgneal@111
|
553 except InvalidPage:
|
bgneal@111
|
554 raise Http404
|
bgneal@111
|
555
|
bgneal@111
|
556 # we do this for the template since it is rendered twice
|
bgneal@111
|
557 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@111
|
558 form = None
|
bgneal@111
|
559
|
bgneal@111
|
560 if request.method == 'POST':
|
bgneal@111
|
561 topic_ids = request.POST.getlist('topic_ids')
|
bgneal@111
|
562 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
|
bgneal@111
|
563 url += '?page=%s' % page_num
|
bgneal@111
|
564
|
bgneal@111
|
565 if len(topic_ids):
|
bgneal@111
|
566 if request.POST.get('sticky'):
|
bgneal@111
|
567 _bulk_sticky(forum, topic_ids)
|
bgneal@111
|
568 return HttpResponseRedirect(url)
|
bgneal@111
|
569 elif request.POST.get('lock'):
|
bgneal@111
|
570 _bulk_lock(forum, topic_ids)
|
bgneal@111
|
571 return HttpResponseRedirect(url)
|
bgneal@111
|
572 elif request.POST.get('delete'):
|
bgneal@111
|
573 _bulk_delete(forum, topic_ids)
|
bgneal@111
|
574 return HttpResponseRedirect(url)
|
bgneal@111
|
575 elif request.POST.get('move'):
|
bgneal@111
|
576 form = MoveTopicForm(request.user, request.POST, hide_label=True)
|
bgneal@111
|
577 if form.is_valid():
|
bgneal@111
|
578 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
|
bgneal@111
|
579 return HttpResponseRedirect(url)
|
bgneal@111
|
580
|
bgneal@111
|
581 if form is None:
|
bgneal@111
|
582 form = MoveTopicForm(request.user, hide_label=True)
|
bgneal@111
|
583
|
bgneal@111
|
584 return render_to_response('forums/mod_forum.html', {
|
bgneal@111
|
585 'forum': forum,
|
bgneal@111
|
586 'page': page,
|
bgneal@111
|
587 'page_nav': page_nav,
|
bgneal@111
|
588 'form': form,
|
bgneal@111
|
589 },
|
bgneal@111
|
590 context_instance=RequestContext(request))
|
bgneal@111
|
591
|
bgneal@111
|
592
|
bgneal@113
|
593 @login_required
|
bgneal@113
|
594 @require_POST
|
bgneal@113
|
595 def forum_catchup(request, slug):
|
bgneal@113
|
596 """
|
bgneal@113
|
597 This view marks all the topics in the forum as being read.
|
bgneal@113
|
598 """
|
bgneal@113
|
599 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
|
bgneal@113
|
600
|
bgneal@113
|
601 if not forum.category.can_access(request.user):
|
bgneal@113
|
602 return HttpResponseForbidden()
|
bgneal@113
|
603
|
bgneal@113
|
604 forum.catchup(request.user)
|
bgneal@113
|
605 return HttpResponseRedirect(forum.get_absolute_url())
|
bgneal@113
|
606
|
bgneal@113
|
607
|
bgneal@115
|
608 @login_required
|
bgneal@115
|
609 def mod_topic_split(request, id):
|
bgneal@115
|
610 """
|
bgneal@115
|
611 This view function allows moderators to split posts off to a new topic.
|
bgneal@115
|
612 """
|
bgneal@115
|
613 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
|
bgneal@115
|
614 if not _can_moderate(topic.forum, request.user):
|
bgneal@115
|
615 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
616
|
bgneal@115
|
617 if request.method == "POST":
|
bgneal@115
|
618 form = SplitTopicForm(request.user, request.POST)
|
bgneal@115
|
619 if form.is_valid():
|
bgneal@115
|
620 if form.split_at:
|
bgneal@115
|
621 _split_topic_at(topic, form.post_ids[0],
|
bgneal@115
|
622 form.cleaned_data['forums'],
|
bgneal@115
|
623 form.cleaned_data['name'])
|
bgneal@115
|
624 else:
|
bgneal@115
|
625 _split_topic(topic, form.post_ids,
|
bgneal@115
|
626 form.cleaned_data['forums'],
|
bgneal@115
|
627 form.cleaned_data['name'])
|
bgneal@115
|
628
|
bgneal@115
|
629 return HttpResponseRedirect(topic.get_absolute_url())
|
bgneal@115
|
630 else:
|
bgneal@115
|
631 form = SplitTopicForm(request.user)
|
bgneal@115
|
632
|
bgneal@115
|
633 posts = topic.posts.select_related()
|
bgneal@115
|
634
|
bgneal@115
|
635 return render_to_response('forums/mod_split_topic.html', {
|
bgneal@115
|
636 'forum': topic.forum,
|
bgneal@115
|
637 'topic': topic,
|
bgneal@115
|
638 'posts': posts,
|
bgneal@115
|
639 'form': form,
|
bgneal@115
|
640 },
|
bgneal@115
|
641 context_instance=RequestContext(request))
|
bgneal@115
|
642
|
bgneal@115
|
643
|
bgneal@167
|
644 @login_required
|
bgneal@167
|
645 def unread_topics(request):
|
bgneal@167
|
646 topics = get_unread_topics(request.user)
|
bgneal@167
|
647
|
bgneal@167
|
648 paginator = create_topic_paginator(topics)
|
bgneal@167
|
649 page_num = get_page_num(request)
|
bgneal@167
|
650 try:
|
bgneal@167
|
651 page = paginator.page(page_num)
|
bgneal@167
|
652 except InvalidPage:
|
bgneal@167
|
653 raise Http404
|
bgneal@167
|
654
|
bgneal@167
|
655 attach_topic_page_ranges(page.object_list)
|
bgneal@167
|
656
|
bgneal@167
|
657 # we do this for the template since it is rendered twice
|
bgneal@167
|
658 page_nav = render_to_string('forums/pagination.html', {'page': page})
|
bgneal@167
|
659
|
bgneal@167
|
660 return render_to_response('forums/topic_list.html', {
|
bgneal@167
|
661 'title': 'Topics With Unread Posts',
|
bgneal@167
|
662 'page': page,
|
bgneal@167
|
663 'page_nav': page_nav,
|
bgneal@167
|
664 },
|
bgneal@167
|
665 context_instance=RequestContext(request))
|
bgneal@167
|
666
|
bgneal@167
|
667
|
bgneal@109
|
668 def _can_moderate(forum, user):
|
bgneal@109
|
669 """
|
bgneal@109
|
670 Determines if a user has permission to moderate a given forum.
|
bgneal@109
|
671 """
|
bgneal@109
|
672 return user.is_authenticated() and (
|
bgneal@109
|
673 user.is_superuser or user in forum.moderators.all())
|
bgneal@109
|
674
|
bgneal@109
|
675
|
bgneal@108
|
676 def _can_post_in_topic(topic, user):
|
bgneal@108
|
677 """
|
bgneal@108
|
678 This function returns true if the given user can post in the given topic
|
bgneal@108
|
679 and false otherwise.
|
bgneal@108
|
680 """
|
bgneal@108
|
681 return (not topic.locked and topic.forum.category.can_access(user)) or \
|
bgneal@108
|
682 (user.is_superuser or user in topic.forum.moderators.all())
|
bgneal@108
|
683
|
bgneal@108
|
684
|
bgneal@108
|
685 def _bump_post_count(user):
|
bgneal@108
|
686 """
|
bgneal@108
|
687 Increments the forum_post_count for the given user.
|
bgneal@108
|
688 """
|
bgneal@108
|
689 profile = user.get_profile()
|
bgneal@108
|
690 profile.forum_post_count += 1
|
bgneal@108
|
691 profile.save()
|
bgneal@108
|
692
|
bgneal@108
|
693
|
bgneal@108
|
694 def _quote_message(who, message):
|
bgneal@111
|
695 """
|
bgneal@111
|
696 Builds a message reply by quoting the existing message in a
|
bgneal@111
|
697 typical email-like fashion. The quoting is compatible with Markdown.
|
bgneal@111
|
698 """
|
bgneal@111
|
699 header = '*%s wrote:*\n\n' % (who, )
|
bgneal@111
|
700 lines = wrap(message, 55).split('\n')
|
bgneal@111
|
701 for i, line in enumerate(lines):
|
bgneal@111
|
702 lines[i] = '> ' + line
|
bgneal@111
|
703 return header + '\n'.join(lines)
|
bgneal@111
|
704
|
bgneal@111
|
705
|
bgneal@111
|
706 def _move_topic(topic, old_forum, new_forum):
|
bgneal@111
|
707 if new_forum != old_forum:
|
bgneal@111
|
708 topic.forum = new_forum
|
bgneal@111
|
709 topic.save()
|
bgneal@111
|
710 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
|
bgneal@112
|
711 old_forum.sync()
|
bgneal@111
|
712 old_forum.save()
|
bgneal@112
|
713 new_forum.sync()
|
bgneal@111
|
714 new_forum.save()
|
bgneal@111
|
715
|
bgneal@111
|
716
|
bgneal@111
|
717 def _bulk_sticky(forum, topic_ids):
|
bgneal@111
|
718 """
|
bgneal@111
|
719 Performs a toggle on the sticky status for a given list of topic ids.
|
bgneal@111
|
720 """
|
bgneal@111
|
721 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
722 for topic in topics:
|
bgneal@111
|
723 if topic.forum == forum:
|
bgneal@111
|
724 topic.sticky = not topic.sticky
|
bgneal@111
|
725 topic.save()
|
bgneal@111
|
726
|
bgneal@111
|
727
|
bgneal@111
|
728 def _bulk_lock(forum, topic_ids):
|
bgneal@111
|
729 """
|
bgneal@111
|
730 Performs a toggle on the locked status for a given list of topic ids.
|
bgneal@111
|
731 """
|
bgneal@111
|
732 topics = Topic.objects.filter(pk__in=topic_ids)
|
bgneal@111
|
733 for topic in topics:
|
bgneal@111
|
734 if topic.forum == forum:
|
bgneal@111
|
735 topic.locked = not topic.locked
|
bgneal@111
|
736 topic.save()
|
bgneal@111
|
737
|
bgneal@111
|
738
|
bgneal@111
|
739 def _bulk_delete(forum, topic_ids):
|
bgneal@111
|
740 """
|
bgneal@111
|
741 Deletes the list of topics.
|
bgneal@111
|
742 """
|
bgneal@111
|
743 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
744 for topic in topics:
|
bgneal@111
|
745 if topic.forum == forum:
|
bgneal@111
|
746 _delete_topic(topic)
|
bgneal@111
|
747
|
bgneal@111
|
748
|
bgneal@111
|
749 def _bulk_move(topic_ids, old_forum, new_forum):
|
bgneal@111
|
750 """
|
bgneal@111
|
751 Moves the list of topics to a new forum.
|
bgneal@111
|
752 """
|
bgneal@111
|
753 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
|
bgneal@111
|
754 for topic in topics:
|
bgneal@111
|
755 if topic.forum == old_forum:
|
bgneal@111
|
756 _move_topic(topic, old_forum, new_forum)
|
bgneal@111
|
757
|
bgneal@113
|
758
|
bgneal@113
|
759 def _update_last_visit(user, topic):
|
bgneal@113
|
760 """
|
bgneal@113
|
761 Does the bookkeeping for the last visit status for the user to the
|
bgneal@113
|
762 topic/forum.
|
bgneal@113
|
763 """
|
bgneal@113
|
764 now = datetime.datetime.now()
|
bgneal@113
|
765 try:
|
bgneal@113
|
766 flv = ForumLastVisit.objects.get(user=user, forum=topic.forum)
|
bgneal@113
|
767 except ForumLastVisit.DoesNotExist:
|
bgneal@113
|
768 flv = ForumLastVisit(user=user, forum=topic.forum)
|
bgneal@113
|
769 flv.begin_date = now
|
bgneal@113
|
770
|
bgneal@113
|
771 flv.end_date = now
|
bgneal@113
|
772 flv.save()
|
bgneal@113
|
773
|
bgneal@113
|
774 if topic.update_date > flv.begin_date:
|
bgneal@113
|
775 try:
|
bgneal@113
|
776 tlv = TopicLastVisit.objects.get(user=user, topic=topic)
|
bgneal@113
|
777 except TopicLastVisit.DoesNotExist:
|
bgneal@113
|
778 tlv = TopicLastVisit(user=user, topic=topic)
|
bgneal@113
|
779
|
bgneal@113
|
780 tlv.touch()
|
bgneal@113
|
781 tlv.save()
|
bgneal@113
|
782
|
bgneal@115
|
783
|
bgneal@115
|
784 def _split_topic_at(topic, post_id, new_forum, new_name):
|
bgneal@115
|
785 """
|
bgneal@115
|
786 This function splits the post given by post_id and all posts that come
|
bgneal@115
|
787 after it in the given topic to a new topic in a new forum.
|
bgneal@115
|
788 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
789 """
|
bgneal@115
|
790 post = get_object_or_404(Post, id=post_id)
|
bgneal@115
|
791 if post.topic == topic:
|
bgneal@115
|
792 post_ids = Post.objects.filter(topic=topic,
|
bgneal@115
|
793 creation_date__gte=post.creation_date).values_list('id', flat=True)
|
bgneal@115
|
794 _split_topic(topic, post_ids, new_forum, new_name)
|
bgneal@115
|
795
|
bgneal@115
|
796
|
bgneal@115
|
797 def _split_topic(topic, post_ids, new_forum, new_name):
|
bgneal@115
|
798 """
|
bgneal@115
|
799 This function splits the posts given by the post_ids list in the
|
bgneal@115
|
800 given topic to a new topic in a new forum.
|
bgneal@115
|
801 It is assumed the caller has been checked for moderator rights.
|
bgneal@115
|
802 """
|
bgneal@115
|
803 posts = Post.objects.filter(topic=topic, id__in=post_ids)
|
bgneal@115
|
804 if len(posts) > 0:
|
bgneal@115
|
805 new_topic = Topic(forum=new_forum, name=new_name, user=posts[0].user)
|
bgneal@115
|
806 new_topic.save()
|
bgneal@115
|
807 for post in posts:
|
bgneal@115
|
808 post.topic = new_topic
|
bgneal@115
|
809 post.save()
|
bgneal@115
|
810
|
bgneal@115
|
811 topic.post_count_update()
|
bgneal@115
|
812 topic.save()
|
bgneal@115
|
813 new_topic.post_count_update()
|
bgneal@115
|
814 new_topic.save()
|
bgneal@115
|
815 topic.forum.sync()
|
bgneal@115
|
816 topic.forum.save()
|
bgneal@115
|
817 new_forum.sync()
|
bgneal@115
|
818 new_forum.save()
|