annotate gpp/forums/views.py @ 112:d1b0b86441c0

Forums: added a sync() function on the forum model. Created javascript for the moderate forum function to drive the master topic select checkbox.
author Brian Neal <bgneal@gmail.com>
date Wed, 30 Sep 2009 00:42:13 +0000
parents e5faf9f0c11a
children d97ceb95ce02
rev   line source
bgneal@81 1 """
bgneal@81 2 Views for the forums application.
bgneal@81 3 """
bgneal@83 4 from django.contrib.auth.decorators import login_required
bgneal@82 5 from django.http import Http404
bgneal@98 6 from django.http import HttpResponse
bgneal@89 7 from django.http import HttpResponseBadRequest
bgneal@90 8 from django.http import HttpResponseForbidden
bgneal@83 9 from django.http import HttpResponseRedirect
bgneal@83 10 from django.core.urlresolvers import reverse
bgneal@91 11 from django.core.paginator import InvalidPage
bgneal@82 12 from django.shortcuts import get_object_or_404
bgneal@81 13 from django.shortcuts import render_to_response
bgneal@97 14 from django.template.loader import render_to_string
bgneal@81 15 from django.template import RequestContext
bgneal@89 16 from django.views.decorators.http import require_POST
bgneal@108 17 from django.utils.text import wrap
bgneal@81 18
bgneal@90 19 from core.paginator import DiggPaginator
bgneal@98 20 from core.functions import email_admins
bgneal@81 21 from forums.models import Forum
bgneal@83 22 from forums.models import Topic
bgneal@91 23 from forums.models import Post
bgneal@98 24 from forums.models import FlaggedPost
bgneal@110 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm
bgneal@81 26
bgneal@90 27 #######################################################################
bgneal@90 28
bgneal@93 29 TOPICS_PER_PAGE = 50
bgneal@90 30 POSTS_PER_PAGE = 2
bgneal@90 31
bgneal@93 32 def create_topic_paginator(topics):
bgneal@93 33 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
bgneal@93 34
bgneal@93 35 def create_post_paginator(posts):
bgneal@93 36 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
bgneal@90 37
bgneal@90 38 #######################################################################
bgneal@81 39
bgneal@81 40 def index(request):
bgneal@82 41 """
bgneal@82 42 This view displays all the forums available, ordered in each category.
bgneal@82 43 """
bgneal@100 44 forums = Forum.objects.forums_for_user(request.user)
bgneal@81 45 cats = {}
bgneal@81 46 for forum in forums:
bgneal@81 47 cat = cats.setdefault(forum.category.id, {
bgneal@81 48 'cat': forum.category,
bgneal@81 49 'forums': [],
bgneal@81 50 })
bgneal@81 51 cat['forums'].append(forum)
bgneal@81 52
bgneal@81 53 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 54 cats = sorted(cats.values(), cmpdef)
bgneal@81 55
bgneal@81 56 return render_to_response('forums/index.html', {
bgneal@81 57 'cats': cats,
bgneal@81 58 },
bgneal@81 59 context_instance=RequestContext(request))
bgneal@81 60
bgneal@82 61
bgneal@81 62 def forum_index(request, slug):
bgneal@82 63 """
bgneal@82 64 Displays all the topics in a forum.
bgneal@82 65 """
bgneal@101 66 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@100 67
bgneal@100 68 if not forum.category.can_access(request.user):
bgneal@100 69 return HttpResponseForbidden()
bgneal@100 70
bgneal@107 71 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
bgneal@93 72 paginator = create_topic_paginator(topics)
bgneal@93 73 page_num = int(request.GET.get('page', 1))
bgneal@93 74 try:
bgneal@93 75 page = paginator.page(page_num)
bgneal@93 76 except InvalidPage:
bgneal@93 77 raise Http404
bgneal@97 78
bgneal@97 79 # we do this for the template since it is rendered twice
bgneal@97 80 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@111 81
bgneal@111 82 can_moderate = _can_moderate(forum, request.user)
bgneal@82 83
bgneal@82 84 return render_to_response('forums/forum_index.html', {
bgneal@82 85 'forum': forum,
bgneal@93 86 'page': page,
bgneal@97 87 'page_nav': page_nav,
bgneal@111 88 'can_moderate': can_moderate,
bgneal@82 89 },
bgneal@82 90 context_instance=RequestContext(request))
bgneal@82 91
bgneal@82 92
bgneal@82 93 def topic_index(request, id):
bgneal@82 94 """
bgneal@82 95 Displays all the posts in a topic.
bgneal@82 96 """
bgneal@101 97 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@100 98
bgneal@100 99 if not topic.forum.category.can_access(request.user):
bgneal@100 100 return HttpResponseForbidden()
bgneal@100 101
bgneal@86 102 topic.view_count += 1
bgneal@86 103 topic.save()
bgneal@86 104
bgneal@86 105 posts = topic.posts.select_related()
bgneal@93 106 paginator = create_post_paginator(posts)
bgneal@93 107 page_num = int(request.GET.get('page', 1))
bgneal@90 108 try:
bgneal@90 109 page = paginator.page(page_num)
bgneal@90 110 except InvalidPage:
bgneal@90 111 raise Http404
bgneal@90 112
bgneal@90 113 last_page = page_num == paginator.num_pages
bgneal@86 114
bgneal@97 115 # we do this for the template since it is rendered twice
bgneal@97 116 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@97 117
bgneal@109 118 can_moderate = _can_moderate(topic.forum, request.user)
bgneal@104 119
bgneal@104 120 can_reply = request.user.is_authenticated() and (
bgneal@104 121 not topic.locked or can_moderate)
bgneal@104 122
bgneal@86 123 return render_to_response('forums/topic.html', {
bgneal@86 124 'forum': topic.forum,
bgneal@86 125 'topic': topic,
bgneal@90 126 'page': page,
bgneal@97 127 'page_nav': page_nav,
bgneal@87 128 'last_page': last_page,
bgneal@104 129 'can_moderate': can_moderate,
bgneal@104 130 'can_reply': can_reply,
bgneal@106 131 'form': NewPostForm(initial={'topic_id': topic.id}),
bgneal@86 132 },
bgneal@86 133 context_instance=RequestContext(request))
bgneal@83 134
bgneal@83 135
bgneal@83 136 @login_required
bgneal@83 137 def new_topic(request, slug):
bgneal@83 138 """
bgneal@83 139 This view handles the creation of new topics.
bgneal@83 140 """
bgneal@101 141 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@100 142
bgneal@100 143 if not forum.category.can_access(request.user):
bgneal@100 144 return HttpResponseForbidden()
bgneal@100 145
bgneal@83 146 if request.method == 'POST':
bgneal@102 147 form = NewTopicForm(request.user, forum, request.POST)
bgneal@83 148 if form.is_valid():
bgneal@102 149 topic = form.save(request.META.get("REMOTE_ADDR"))
bgneal@108 150 _bump_post_count(request.user)
bgneal@83 151 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 152 kwargs={'tid': topic.pk}))
bgneal@83 153 else:
bgneal@102 154 form = NewTopicForm(request.user, forum)
bgneal@83 155
bgneal@83 156 return render_to_response('forums/new_topic.html', {
bgneal@83 157 'forum': forum,
bgneal@83 158 'form': form,
bgneal@83 159 },
bgneal@83 160 context_instance=RequestContext(request))
bgneal@83 161
bgneal@83 162
bgneal@83 163 @login_required
bgneal@83 164 def new_topic_thanks(request, tid):
bgneal@83 165 """
bgneal@83 166 This view displays the success page for a newly created topic.
bgneal@83 167 """
bgneal@101 168 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
bgneal@83 169 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 170 'forum': topic.forum,
bgneal@83 171 'topic': topic,
bgneal@83 172 },
bgneal@83 173 context_instance=RequestContext(request))
bgneal@89 174
bgneal@89 175
bgneal@89 176 @require_POST
bgneal@89 177 def quick_reply_ajax(request):
bgneal@89 178 """
bgneal@89 179 This function handles the quick reply to a thread function. This
bgneal@89 180 function is meant to be the target of an AJAX post, and returns
bgneal@89 181 the HTML for the new post, which the client-side script appends
bgneal@89 182 to the document.
bgneal@89 183 """
bgneal@90 184 if not request.user.is_authenticated():
bgneal@108 185 return HttpResponseForbidden('Please login or register to post.')
bgneal@90 186
bgneal@106 187 form = NewPostForm(request.POST)
bgneal@89 188 if form.is_valid():
bgneal@108 189 if not _can_post_in_topic(form.topic, request.user):
bgneal@108 190 return HttpResponseForbidden("You don't have permission to post in this topic.")
bgneal@100 191
bgneal@108 192 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
bgneal@108 193 _bump_post_count(request.user)
bgneal@89 194 return render_to_response('forums/display_post.html', {
bgneal@89 195 'post': post,
bgneal@89 196 },
bgneal@89 197 context_instance=RequestContext(request))
bgneal@89 198
bgneal@108 199 return HttpResponseBadRequest("Invalid post.");
bgneal@89 200
bgneal@91 201
bgneal@91 202 def goto_post(request, post_id):
bgneal@91 203 """
bgneal@91 204 This function calculates what page a given post is on, then redirects
bgneal@91 205 to that URL. This function is the target of get_absolute_url() for
bgneal@91 206 Post objects.
bgneal@91 207 """
bgneal@101 208 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
bgneal@91 209 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
bgneal@91 210 page = count / POSTS_PER_PAGE + 1
bgneal@91 211 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
bgneal@91 212 '?page=%s#p%s' % (page, post.id)
bgneal@91 213 return HttpResponseRedirect(url)
bgneal@91 214
bgneal@98 215
bgneal@98 216 @require_POST
bgneal@98 217 def flag_post(request):
bgneal@98 218 """
bgneal@98 219 This function handles the flagging of posts by users. This function should
bgneal@98 220 be the target of an AJAX post.
bgneal@98 221 """
bgneal@98 222 if not request.user.is_authenticated():
bgneal@99 223 return HttpResponseForbidden('Please login or register to flag a post.')
bgneal@98 224
bgneal@98 225 id = request.POST.get('id')
bgneal@98 226 if id is None:
bgneal@98 227 return HttpResponseBadRequest('No post id')
bgneal@98 228
bgneal@98 229 try:
bgneal@98 230 post = Post.objects.get(pk=id)
bgneal@98 231 except Post.DoesNotExist:
bgneal@98 232 return HttpResponseBadRequest('No post with id %s' % id)
bgneal@98 233
bgneal@98 234 flag = FlaggedPost(user=request.user, post=post)
bgneal@98 235 flag.save()
bgneal@98 236 email_admins('A Post Has Been Flagged', """Hello,
bgneal@98 237
bgneal@98 238 A user has flagged a forum post for review.
bgneal@98 239 """)
bgneal@98 240 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
bgneal@98 241 'Thanks for helping to improve the discussions on this site.')
bgneal@106 242
bgneal@106 243
bgneal@106 244 @login_required
bgneal@106 245 def edit_post(request, id):
bgneal@106 246 """
bgneal@106 247 This view function allows authorized users to edit posts.
bgneal@106 248 The superuser, forum moderators, and original author can edit posts.
bgneal@106 249 """
bgneal@106 250 post = get_object_or_404(Post.objects.select_related(), pk=id)
bgneal@108 251
bgneal@109 252 can_moderate = _can_moderate(post.topic.forum, request.user)
bgneal@108 253 can_edit = can_moderate or request.user == post.user
bgneal@106 254
bgneal@106 255 if not can_edit:
bgneal@106 256 return HttpResponseForbidden("You don't have permission to edit that post.")
bgneal@106 257
bgneal@106 258 if request.method == "POST":
bgneal@106 259 form = PostForm(request.POST, instance=post)
bgneal@106 260 if form.is_valid():
bgneal@106 261 form.save()
bgneal@106 262 return HttpResponseRedirect(post.get_absolute_url())
bgneal@106 263 else:
bgneal@106 264 form = PostForm(instance=post)
bgneal@106 265
bgneal@106 266 return render_to_response('forums/edit_post.html', {
bgneal@106 267 'forum': post.topic.forum,
bgneal@106 268 'topic': post.topic,
bgneal@106 269 'post': post,
bgneal@106 270 'form': form,
bgneal@108 271 'can_moderate': can_moderate,
bgneal@106 272 },
bgneal@106 273 context_instance=RequestContext(request))
bgneal@107 274
bgneal@107 275
bgneal@107 276 @require_POST
bgneal@107 277 def delete_post(request):
bgneal@107 278 """
bgneal@107 279 This view function allows superusers and forum moderators to delete posts.
bgneal@107 280 This function is the target of AJAX calls from the client.
bgneal@107 281 """
bgneal@107 282 if not request.user.is_authenticated():
bgneal@107 283 return HttpResponseForbidden('Please login to delete a post.')
bgneal@107 284
bgneal@107 285 id = request.POST.get('id')
bgneal@107 286 if id is None:
bgneal@107 287 return HttpResponseBadRequest('No post id')
bgneal@107 288
bgneal@107 289 post = get_object_or_404(Post.objects.select_related(), pk=id)
bgneal@107 290
bgneal@107 291 can_delete = request.user.is_superuser or \
bgneal@107 292 request.user in post.topic.forum.moderators.all()
bgneal@107 293
bgneal@107 294 if not can_delete:
bgneal@107 295 return HttpResponseForbidden("You don't have permission to delete that post.")
bgneal@107 296
bgneal@107 297 if post.topic.post_count == 1 and post == post.topic.last_post:
bgneal@107 298 _delete_topic(post.topic)
bgneal@107 299 else:
bgneal@107 300 _delete_post(post)
bgneal@107 301
bgneal@107 302 return HttpResponse("The post has been deleted.")
bgneal@107 303
bgneal@107 304
bgneal@107 305 def _delete_post(post):
bgneal@107 306 """
bgneal@107 307 Internal function to delete a single post object.
bgneal@107 308 Decrements the post author's post count.
bgneal@107 309 Adjusts the parent topic and forum's last_post as needed.
bgneal@107 310 """
bgneal@107 311 # Adjust post creator's post count
bgneal@107 312 profile = post.user.get_profile()
bgneal@107 313 if profile.forum_post_count > 0:
bgneal@107 314 profile.forum_post_count -= 1
bgneal@107 315 profile.save()
bgneal@107 316
bgneal@107 317 # If this post is the last_post in a topic, we need to update
bgneal@107 318 # both the topic and parent forum's last post fields. If we don't
bgneal@107 319 # the cascading delete will delete them also!
bgneal@107 320
bgneal@107 321 topic = post.topic
bgneal@107 322 if topic.last_post == post:
bgneal@107 323 topic.last_post_pre_delete()
bgneal@107 324 topic.save()
bgneal@107 325
bgneal@107 326 forum = topic.forum
bgneal@107 327 if forum.last_post == post:
bgneal@107 328 forum.last_post_pre_delete()
bgneal@107 329 forum.save()
bgneal@107 330
bgneal@107 331 # Should be safe to delete the post now:
bgneal@107 332 post.delete()
bgneal@107 333
bgneal@107 334
bgneal@107 335 def _delete_topic(topic):
bgneal@107 336 """
bgneal@107 337 Internal function to delete an entire topic.
bgneal@107 338 Deletes the topic and all posts contained within.
bgneal@107 339 Adjusts the parent forum's last_post as needed.
bgneal@107 340 Note that we don't bother adjusting all the users'
bgneal@107 341 post counts as that doesn't seem to be worth the effort.
bgneal@107 342 """
bgneal@107 343 if topic.forum.last_post.topic == topic:
bgneal@107 344 topic.forum.last_post_pre_delete()
bgneal@107 345 topic.forum.save()
bgneal@107 346
bgneal@107 347 # It should be safe to just delete the topic now. This will
bgneal@107 348 # automatically delete all posts in the topic.
bgneal@107 349 topic.delete()
bgneal@108 350
bgneal@108 351
bgneal@108 352 @login_required
bgneal@108 353 def new_post(request, topic_id):
bgneal@108 354 """
bgneal@108 355 This function is the view for creating a normal, non-quick reply
bgneal@108 356 to a topic.
bgneal@108 357 """
bgneal@108 358 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
bgneal@108 359 can_post = _can_post_in_topic(topic, request.user)
bgneal@108 360
bgneal@108 361 if can_post:
bgneal@108 362 if request.method == 'POST':
bgneal@108 363 form = PostForm(request.POST)
bgneal@108 364 if form.is_valid():
bgneal@108 365 post = form.save(commit=False)
bgneal@108 366 post.topic = topic
bgneal@108 367 post.user = request.user
bgneal@108 368 post.user_ip = request.META.get("REMOTE_ADDR", "")
bgneal@108 369 post.save()
bgneal@108 370 _bump_post_count(request.user)
bgneal@108 371 return HttpResponseRedirect(post.get_absolute_url())
bgneal@108 372 else:
bgneal@108 373 quote_id = request.GET.get('quote')
bgneal@108 374 if quote_id:
bgneal@108 375 quote_post = get_object_or_404(Post.objects.select_related(),
bgneal@108 376 pk=quote_id)
bgneal@108 377 form = PostForm(initial={'body': _quote_message(quote_post.user.username,
bgneal@108 378 quote_post.body)})
bgneal@108 379 else:
bgneal@108 380 form = PostForm()
bgneal@108 381 else:
bgneal@108 382 form = None
bgneal@108 383
bgneal@108 384 return render_to_response('forums/new_post.html', {
bgneal@108 385 'forum': topic.forum,
bgneal@108 386 'topic': topic,
bgneal@108 387 'form': form,
bgneal@108 388 'can_post': can_post,
bgneal@108 389 },
bgneal@108 390 context_instance=RequestContext(request))
bgneal@108 391
bgneal@108 392
bgneal@109 393 @login_required
bgneal@109 394 def mod_topic_stick(request, id):
bgneal@109 395 """
bgneal@109 396 This view function is for moderators to toggle the sticky status of a topic.
bgneal@109 397 """
bgneal@109 398 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 399 if _can_moderate(topic.forum, request.user):
bgneal@109 400 topic.sticky = not topic.sticky
bgneal@109 401 topic.save()
bgneal@109 402 return HttpResponseRedirect(topic.get_absolute_url())
bgneal@109 403
bgneal@110 404 return HttpResponseForbidden()
bgneal@109 405
bgneal@109 406
bgneal@109 407 @login_required
bgneal@109 408 def mod_topic_lock(request, id):
bgneal@109 409 """
bgneal@109 410 This view function is for moderators to toggle the locked status of a topic.
bgneal@109 411 """
bgneal@109 412 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 413 if _can_moderate(topic.forum, request.user):
bgneal@109 414 topic.locked = not topic.locked
bgneal@109 415 topic.save()
bgneal@109 416 return HttpResponseRedirect(topic.get_absolute_url())
bgneal@109 417
bgneal@110 418 return HttpResponseForbidden()
bgneal@109 419
bgneal@109 420
bgneal@109 421 @login_required
bgneal@109 422 def mod_topic_delete(request, id):
bgneal@109 423 """
bgneal@109 424 This view function is for moderators to delete an entire topic.
bgneal@109 425 """
bgneal@109 426 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 427 if _can_moderate(topic.forum, request.user):
bgneal@109 428 forum_url = topic.forum.get_absolute_url()
bgneal@109 429 _delete_topic(topic)
bgneal@109 430 return HttpResponseRedirect(forum_url)
bgneal@109 431
bgneal@110 432 return HttpResponseForbidden()
bgneal@110 433
bgneal@110 434
bgneal@110 435 @login_required
bgneal@110 436 def mod_topic_move(request, id):
bgneal@110 437 """
bgneal@110 438 This view function is for moderators to move a topic to a different forum.
bgneal@110 439 """
bgneal@110 440 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@110 441 if not _can_moderate(topic.forum, request.user):
bgneal@110 442 return HttpResponseForbidden()
bgneal@110 443
bgneal@110 444 if request.method == 'POST':
bgneal@110 445 form = MoveTopicForm(request.user, request.POST)
bgneal@110 446 if form.is_valid():
bgneal@110 447 new_forum = form.cleaned_data['forums']
bgneal@110 448 old_forum = topic.forum
bgneal@111 449 _move_topic(topic, old_forum, new_forum)
bgneal@110 450 return HttpResponseRedirect(topic.get_absolute_url())
bgneal@110 451 else:
bgneal@110 452 form = MoveTopicForm(request.user)
bgneal@110 453
bgneal@110 454 return render_to_response('forums/move_topic.html', {
bgneal@110 455 'forum': topic.forum,
bgneal@110 456 'topic': topic,
bgneal@110 457 'form': form,
bgneal@110 458 },
bgneal@110 459 context_instance=RequestContext(request))
bgneal@109 460
bgneal@109 461
bgneal@111 462 @login_required
bgneal@111 463 def mod_forum(request, slug):
bgneal@111 464 """
bgneal@111 465 Displays a view to allow moderators to perform various operations
bgneal@111 466 on topics in a forum in bulk. We currently support mass locking/unlocking,
bgneal@111 467 stickying and unstickying, moving, and deleting topics.
bgneal@111 468 """
bgneal@111 469 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@111 470 if not _can_moderate(forum, request.user):
bgneal@111 471 return HttpResponseForbidden()
bgneal@111 472
bgneal@111 473 topics = forum.topics.select_related('user', 'last_post', 'last_post__user')
bgneal@111 474 paginator = create_topic_paginator(topics)
bgneal@111 475 page_num = int(request.REQUEST.get('page', 1))
bgneal@111 476 try:
bgneal@111 477 page = paginator.page(page_num)
bgneal@111 478 except InvalidPage:
bgneal@111 479 raise Http404
bgneal@111 480
bgneal@111 481 # we do this for the template since it is rendered twice
bgneal@111 482 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@111 483 form = None
bgneal@111 484
bgneal@111 485 if request.method == 'POST':
bgneal@111 486 topic_ids = request.POST.getlist('topic_ids')
bgneal@111 487 url = reverse('forums-mod_forum', kwargs={'slug':forum.slug})
bgneal@111 488 url += '?page=%s' % page_num
bgneal@111 489
bgneal@111 490 if len(topic_ids):
bgneal@111 491 if request.POST.get('sticky'):
bgneal@111 492 _bulk_sticky(forum, topic_ids)
bgneal@111 493 return HttpResponseRedirect(url)
bgneal@111 494 elif request.POST.get('lock'):
bgneal@111 495 _bulk_lock(forum, topic_ids)
bgneal@111 496 return HttpResponseRedirect(url)
bgneal@111 497 elif request.POST.get('delete'):
bgneal@111 498 _bulk_delete(forum, topic_ids)
bgneal@111 499 return HttpResponseRedirect(url)
bgneal@111 500 elif request.POST.get('move'):
bgneal@111 501 form = MoveTopicForm(request.user, request.POST, hide_label=True)
bgneal@111 502 if form.is_valid():
bgneal@111 503 _bulk_move(topic_ids, forum, form.cleaned_data['forums'])
bgneal@111 504 return HttpResponseRedirect(url)
bgneal@111 505
bgneal@111 506 if form is None:
bgneal@111 507 form = MoveTopicForm(request.user, hide_label=True)
bgneal@111 508
bgneal@111 509 return render_to_response('forums/mod_forum.html', {
bgneal@111 510 'forum': forum,
bgneal@111 511 'page': page,
bgneal@111 512 'page_nav': page_nav,
bgneal@111 513 'form': form,
bgneal@111 514 },
bgneal@111 515 context_instance=RequestContext(request))
bgneal@111 516
bgneal@111 517
bgneal@109 518 def _can_moderate(forum, user):
bgneal@109 519 """
bgneal@109 520 Determines if a user has permission to moderate a given forum.
bgneal@109 521 """
bgneal@109 522 return user.is_authenticated() and (
bgneal@109 523 user.is_superuser or user in forum.moderators.all())
bgneal@109 524
bgneal@109 525
bgneal@108 526 def _can_post_in_topic(topic, user):
bgneal@108 527 """
bgneal@108 528 This function returns true if the given user can post in the given topic
bgneal@108 529 and false otherwise.
bgneal@108 530 """
bgneal@108 531 return (not topic.locked and topic.forum.category.can_access(user)) or \
bgneal@108 532 (user.is_superuser or user in topic.forum.moderators.all())
bgneal@108 533
bgneal@108 534
bgneal@108 535 def _bump_post_count(user):
bgneal@108 536 """
bgneal@108 537 Increments the forum_post_count for the given user.
bgneal@108 538 """
bgneal@108 539 profile = user.get_profile()
bgneal@108 540 profile.forum_post_count += 1
bgneal@108 541 profile.save()
bgneal@108 542
bgneal@108 543
bgneal@108 544 def _quote_message(who, message):
bgneal@111 545 """
bgneal@111 546 Builds a message reply by quoting the existing message in a
bgneal@111 547 typical email-like fashion. The quoting is compatible with Markdown.
bgneal@111 548 """
bgneal@111 549 header = '*%s wrote:*\n\n' % (who, )
bgneal@111 550 lines = wrap(message, 55).split('\n')
bgneal@111 551 for i, line in enumerate(lines):
bgneal@111 552 lines[i] = '> ' + line
bgneal@111 553 return header + '\n'.join(lines)
bgneal@111 554
bgneal@111 555
bgneal@111 556 def _move_topic(topic, old_forum, new_forum):
bgneal@111 557 if new_forum != old_forum:
bgneal@111 558 topic.forum = new_forum
bgneal@111 559 topic.save()
bgneal@111 560 # Have to adjust foreign keys to last_post, denormalized counts, etc.:
bgneal@112 561 old_forum.sync()
bgneal@111 562 old_forum.save()
bgneal@112 563 new_forum.sync()
bgneal@111 564 new_forum.save()
bgneal@111 565
bgneal@111 566
bgneal@111 567 def _bulk_sticky(forum, topic_ids):
bgneal@111 568 """
bgneal@111 569 Performs a toggle on the sticky status for a given list of topic ids.
bgneal@111 570 """
bgneal@111 571 topics = Topic.objects.filter(pk__in=topic_ids)
bgneal@111 572 for topic in topics:
bgneal@111 573 if topic.forum == forum:
bgneal@111 574 topic.sticky = not topic.sticky
bgneal@111 575 topic.save()
bgneal@111 576
bgneal@111 577
bgneal@111 578 def _bulk_lock(forum, topic_ids):
bgneal@111 579 """
bgneal@111 580 Performs a toggle on the locked status for a given list of topic ids.
bgneal@111 581 """
bgneal@111 582 topics = Topic.objects.filter(pk__in=topic_ids)
bgneal@111 583 for topic in topics:
bgneal@111 584 if topic.forum == forum:
bgneal@111 585 topic.locked = not topic.locked
bgneal@111 586 topic.save()
bgneal@111 587
bgneal@111 588
bgneal@111 589 def _bulk_delete(forum, topic_ids):
bgneal@111 590 """
bgneal@111 591 Deletes the list of topics.
bgneal@111 592 """
bgneal@111 593 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
bgneal@111 594 for topic in topics:
bgneal@111 595 if topic.forum == forum:
bgneal@111 596 _delete_topic(topic)
bgneal@111 597
bgneal@111 598
bgneal@111 599 def _bulk_move(topic_ids, old_forum, new_forum):
bgneal@111 600 """
bgneal@111 601 Moves the list of topics to a new forum.
bgneal@111 602 """
bgneal@111 603 topics = Topic.objects.filter(pk__in=topic_ids).select_related()
bgneal@111 604 for topic in topics:
bgneal@111 605 if topic.forum == old_forum:
bgneal@111 606 _move_topic(topic, old_forum, new_forum)
bgneal@111 607