annotate gpp/forums/views.py @ 165:952e05cb3d80

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