annotate gpp/forums/views.py @ 109:07be3e39e639

Forums: implemented topic level moderator controls.
author Brian Neal <bgneal@gmail.com>
date Sat, 26 Sep 2009 18:03:57 +0000
parents 80ab249d1adc
children c329bfaed4a7
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@106 25 from forums.forms import NewTopicForm, NewPostForm, PostForm
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@82 81
bgneal@82 82 return render_to_response('forums/forum_index.html', {
bgneal@82 83 'forum': forum,
bgneal@93 84 'page': page,
bgneal@97 85 'page_nav': page_nav,
bgneal@82 86 },
bgneal@82 87 context_instance=RequestContext(request))
bgneal@82 88
bgneal@82 89
bgneal@82 90 def topic_index(request, id):
bgneal@82 91 """
bgneal@82 92 Displays all the posts in a topic.
bgneal@82 93 """
bgneal@101 94 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@100 95
bgneal@100 96 if not topic.forum.category.can_access(request.user):
bgneal@100 97 return HttpResponseForbidden()
bgneal@100 98
bgneal@86 99 topic.view_count += 1
bgneal@86 100 topic.save()
bgneal@86 101
bgneal@86 102 posts = topic.posts.select_related()
bgneal@93 103 paginator = create_post_paginator(posts)
bgneal@93 104 page_num = int(request.GET.get('page', 1))
bgneal@90 105 try:
bgneal@90 106 page = paginator.page(page_num)
bgneal@90 107 except InvalidPage:
bgneal@90 108 raise Http404
bgneal@90 109
bgneal@90 110 last_page = page_num == paginator.num_pages
bgneal@86 111
bgneal@97 112 # we do this for the template since it is rendered twice
bgneal@97 113 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@97 114
bgneal@109 115 can_moderate = _can_moderate(topic.forum, request.user)
bgneal@104 116
bgneal@104 117 can_reply = request.user.is_authenticated() and (
bgneal@104 118 not topic.locked or can_moderate)
bgneal@104 119
bgneal@86 120 return render_to_response('forums/topic.html', {
bgneal@86 121 'forum': topic.forum,
bgneal@86 122 'topic': topic,
bgneal@90 123 'page': page,
bgneal@97 124 'page_nav': page_nav,
bgneal@87 125 'last_page': last_page,
bgneal@104 126 'can_moderate': can_moderate,
bgneal@104 127 'can_reply': can_reply,
bgneal@106 128 'form': NewPostForm(initial={'topic_id': topic.id}),
bgneal@86 129 },
bgneal@86 130 context_instance=RequestContext(request))
bgneal@83 131
bgneal@83 132
bgneal@83 133 @login_required
bgneal@83 134 def new_topic(request, slug):
bgneal@83 135 """
bgneal@83 136 This view handles the creation of new topics.
bgneal@83 137 """
bgneal@101 138 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@100 139
bgneal@100 140 if not forum.category.can_access(request.user):
bgneal@100 141 return HttpResponseForbidden()
bgneal@100 142
bgneal@83 143 if request.method == 'POST':
bgneal@102 144 form = NewTopicForm(request.user, forum, request.POST)
bgneal@83 145 if form.is_valid():
bgneal@102 146 topic = form.save(request.META.get("REMOTE_ADDR"))
bgneal@108 147 _bump_post_count(request.user)
bgneal@83 148 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 149 kwargs={'tid': topic.pk}))
bgneal@83 150 else:
bgneal@102 151 form = NewTopicForm(request.user, forum)
bgneal@83 152
bgneal@83 153 return render_to_response('forums/new_topic.html', {
bgneal@83 154 'forum': forum,
bgneal@83 155 'form': form,
bgneal@83 156 },
bgneal@83 157 context_instance=RequestContext(request))
bgneal@83 158
bgneal@83 159
bgneal@83 160 @login_required
bgneal@83 161 def new_topic_thanks(request, tid):
bgneal@83 162 """
bgneal@83 163 This view displays the success page for a newly created topic.
bgneal@83 164 """
bgneal@101 165 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
bgneal@83 166 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 167 'forum': topic.forum,
bgneal@83 168 'topic': topic,
bgneal@83 169 },
bgneal@83 170 context_instance=RequestContext(request))
bgneal@89 171
bgneal@89 172
bgneal@89 173 @require_POST
bgneal@89 174 def quick_reply_ajax(request):
bgneal@89 175 """
bgneal@89 176 This function handles the quick reply to a thread function. This
bgneal@89 177 function is meant to be the target of an AJAX post, and returns
bgneal@89 178 the HTML for the new post, which the client-side script appends
bgneal@89 179 to the document.
bgneal@89 180 """
bgneal@90 181 if not request.user.is_authenticated():
bgneal@108 182 return HttpResponseForbidden('Please login or register to post.')
bgneal@90 183
bgneal@106 184 form = NewPostForm(request.POST)
bgneal@89 185 if form.is_valid():
bgneal@108 186 if not _can_post_in_topic(form.topic, request.user):
bgneal@108 187 return HttpResponseForbidden("You don't have permission to post in this topic.")
bgneal@100 188
bgneal@108 189 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
bgneal@108 190 _bump_post_count(request.user)
bgneal@89 191 return render_to_response('forums/display_post.html', {
bgneal@89 192 'post': post,
bgneal@89 193 },
bgneal@89 194 context_instance=RequestContext(request))
bgneal@89 195
bgneal@108 196 return HttpResponseBadRequest("Invalid post.");
bgneal@89 197
bgneal@91 198
bgneal@91 199 def goto_post(request, post_id):
bgneal@91 200 """
bgneal@91 201 This function calculates what page a given post is on, then redirects
bgneal@91 202 to that URL. This function is the target of get_absolute_url() for
bgneal@91 203 Post objects.
bgneal@91 204 """
bgneal@101 205 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
bgneal@91 206 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
bgneal@91 207 page = count / POSTS_PER_PAGE + 1
bgneal@91 208 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
bgneal@91 209 '?page=%s#p%s' % (page, post.id)
bgneal@91 210 return HttpResponseRedirect(url)
bgneal@91 211
bgneal@98 212
bgneal@98 213 @require_POST
bgneal@98 214 def flag_post(request):
bgneal@98 215 """
bgneal@98 216 This function handles the flagging of posts by users. This function should
bgneal@98 217 be the target of an AJAX post.
bgneal@98 218 """
bgneal@98 219 if not request.user.is_authenticated():
bgneal@99 220 return HttpResponseForbidden('Please login or register to flag a post.')
bgneal@98 221
bgneal@98 222 id = request.POST.get('id')
bgneal@98 223 if id is None:
bgneal@98 224 return HttpResponseBadRequest('No post id')
bgneal@98 225
bgneal@98 226 try:
bgneal@98 227 post = Post.objects.get(pk=id)
bgneal@98 228 except Post.DoesNotExist:
bgneal@98 229 return HttpResponseBadRequest('No post with id %s' % id)
bgneal@98 230
bgneal@98 231 flag = FlaggedPost(user=request.user, post=post)
bgneal@98 232 flag.save()
bgneal@98 233 email_admins('A Post Has Been Flagged', """Hello,
bgneal@98 234
bgneal@98 235 A user has flagged a forum post for review.
bgneal@98 236 """)
bgneal@98 237 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
bgneal@98 238 'Thanks for helping to improve the discussions on this site.')
bgneal@106 239
bgneal@106 240
bgneal@106 241 @login_required
bgneal@106 242 def edit_post(request, id):
bgneal@106 243 """
bgneal@106 244 This view function allows authorized users to edit posts.
bgneal@106 245 The superuser, forum moderators, and original author can edit posts.
bgneal@106 246 """
bgneal@106 247 post = get_object_or_404(Post.objects.select_related(), pk=id)
bgneal@108 248
bgneal@109 249 can_moderate = _can_moderate(post.topic.forum, request.user)
bgneal@108 250 can_edit = can_moderate or request.user == post.user
bgneal@106 251
bgneal@106 252 if not can_edit:
bgneal@106 253 return HttpResponseForbidden("You don't have permission to edit that post.")
bgneal@106 254
bgneal@106 255 if request.method == "POST":
bgneal@106 256 form = PostForm(request.POST, instance=post)
bgneal@106 257 if form.is_valid():
bgneal@106 258 form.save()
bgneal@106 259 return HttpResponseRedirect(post.get_absolute_url())
bgneal@106 260 else:
bgneal@106 261 form = PostForm(instance=post)
bgneal@106 262
bgneal@106 263 return render_to_response('forums/edit_post.html', {
bgneal@106 264 'forum': post.topic.forum,
bgneal@106 265 'topic': post.topic,
bgneal@106 266 'post': post,
bgneal@106 267 'form': form,
bgneal@108 268 'can_moderate': can_moderate,
bgneal@106 269 },
bgneal@106 270 context_instance=RequestContext(request))
bgneal@107 271
bgneal@107 272
bgneal@107 273 @require_POST
bgneal@107 274 def delete_post(request):
bgneal@107 275 """
bgneal@107 276 This view function allows superusers and forum moderators to delete posts.
bgneal@107 277 This function is the target of AJAX calls from the client.
bgneal@107 278 """
bgneal@107 279 if not request.user.is_authenticated():
bgneal@107 280 return HttpResponseForbidden('Please login to delete a post.')
bgneal@107 281
bgneal@107 282 id = request.POST.get('id')
bgneal@107 283 if id is None:
bgneal@107 284 return HttpResponseBadRequest('No post id')
bgneal@107 285
bgneal@107 286 post = get_object_or_404(Post.objects.select_related(), pk=id)
bgneal@107 287
bgneal@107 288 can_delete = request.user.is_superuser or \
bgneal@107 289 request.user in post.topic.forum.moderators.all()
bgneal@107 290
bgneal@107 291 if not can_delete:
bgneal@107 292 return HttpResponseForbidden("You don't have permission to delete that post.")
bgneal@107 293
bgneal@107 294 if post.topic.post_count == 1 and post == post.topic.last_post:
bgneal@107 295 _delete_topic(post.topic)
bgneal@107 296 else:
bgneal@107 297 _delete_post(post)
bgneal@107 298
bgneal@107 299 return HttpResponse("The post has been deleted.")
bgneal@107 300
bgneal@107 301
bgneal@107 302 def _delete_post(post):
bgneal@107 303 """
bgneal@107 304 Internal function to delete a single post object.
bgneal@107 305 Decrements the post author's post count.
bgneal@107 306 Adjusts the parent topic and forum's last_post as needed.
bgneal@107 307 """
bgneal@107 308 # Adjust post creator's post count
bgneal@107 309 profile = post.user.get_profile()
bgneal@107 310 if profile.forum_post_count > 0:
bgneal@107 311 profile.forum_post_count -= 1
bgneal@107 312 profile.save()
bgneal@107 313
bgneal@107 314 # If this post is the last_post in a topic, we need to update
bgneal@107 315 # both the topic and parent forum's last post fields. If we don't
bgneal@107 316 # the cascading delete will delete them also!
bgneal@107 317
bgneal@107 318 topic = post.topic
bgneal@107 319 if topic.last_post == post:
bgneal@107 320 topic.last_post_pre_delete()
bgneal@107 321 topic.save()
bgneal@107 322
bgneal@107 323 forum = topic.forum
bgneal@107 324 if forum.last_post == post:
bgneal@107 325 forum.last_post_pre_delete()
bgneal@107 326 forum.save()
bgneal@107 327
bgneal@107 328 # Should be safe to delete the post now:
bgneal@107 329 post.delete()
bgneal@107 330
bgneal@107 331
bgneal@107 332 def _delete_topic(topic):
bgneal@107 333 """
bgneal@107 334 Internal function to delete an entire topic.
bgneal@107 335 Deletes the topic and all posts contained within.
bgneal@107 336 Adjusts the parent forum's last_post as needed.
bgneal@107 337 Note that we don't bother adjusting all the users'
bgneal@107 338 post counts as that doesn't seem to be worth the effort.
bgneal@107 339 """
bgneal@107 340 if topic.forum.last_post.topic == topic:
bgneal@107 341 topic.forum.last_post_pre_delete()
bgneal@107 342 topic.forum.save()
bgneal@107 343
bgneal@107 344 # It should be safe to just delete the topic now. This will
bgneal@107 345 # automatically delete all posts in the topic.
bgneal@107 346 topic.delete()
bgneal@108 347
bgneal@108 348
bgneal@108 349 @login_required
bgneal@108 350 def new_post(request, topic_id):
bgneal@108 351 """
bgneal@108 352 This function is the view for creating a normal, non-quick reply
bgneal@108 353 to a topic.
bgneal@108 354 """
bgneal@108 355 topic = get_object_or_404(Topic.objects.select_related(), pk=topic_id)
bgneal@108 356 can_post = _can_post_in_topic(topic, request.user)
bgneal@108 357
bgneal@108 358 if can_post:
bgneal@108 359 if request.method == 'POST':
bgneal@108 360 form = PostForm(request.POST)
bgneal@108 361 if form.is_valid():
bgneal@108 362 post = form.save(commit=False)
bgneal@108 363 post.topic = topic
bgneal@108 364 post.user = request.user
bgneal@108 365 post.user_ip = request.META.get("REMOTE_ADDR", "")
bgneal@108 366 post.save()
bgneal@108 367 _bump_post_count(request.user)
bgneal@108 368 return HttpResponseRedirect(post.get_absolute_url())
bgneal@108 369 else:
bgneal@108 370 quote_id = request.GET.get('quote')
bgneal@108 371 if quote_id:
bgneal@108 372 quote_post = get_object_or_404(Post.objects.select_related(),
bgneal@108 373 pk=quote_id)
bgneal@108 374 form = PostForm(initial={'body': _quote_message(quote_post.user.username,
bgneal@108 375 quote_post.body)})
bgneal@108 376 else:
bgneal@108 377 form = PostForm()
bgneal@108 378 else:
bgneal@108 379 form = None
bgneal@108 380
bgneal@108 381 return render_to_response('forums/new_post.html', {
bgneal@108 382 'forum': topic.forum,
bgneal@108 383 'topic': topic,
bgneal@108 384 'form': form,
bgneal@108 385 'can_post': can_post,
bgneal@108 386 },
bgneal@108 387 context_instance=RequestContext(request))
bgneal@108 388
bgneal@108 389
bgneal@109 390 @login_required
bgneal@109 391 def mod_topic_stick(request, id):
bgneal@109 392 """
bgneal@109 393 This view function is for moderators to toggle the sticky status of a topic.
bgneal@109 394 """
bgneal@109 395 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 396 if _can_moderate(topic.forum, request.user):
bgneal@109 397 topic.sticky = not topic.sticky
bgneal@109 398 topic.save()
bgneal@109 399 return HttpResponseRedirect(topic.get_absolute_url())
bgneal@109 400
bgneal@109 401 return HttpResponseForbidden()
bgneal@109 402
bgneal@109 403
bgneal@109 404 @login_required
bgneal@109 405 def mod_topic_lock(request, id):
bgneal@109 406 """
bgneal@109 407 This view function is for moderators to toggle the locked status of a topic.
bgneal@109 408 """
bgneal@109 409 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 410 if _can_moderate(topic.forum, request.user):
bgneal@109 411 topic.locked = not topic.locked
bgneal@109 412 topic.save()
bgneal@109 413 return HttpResponseRedirect(topic.get_absolute_url())
bgneal@109 414
bgneal@109 415 return HttpResponseForbidden()
bgneal@109 416
bgneal@109 417
bgneal@109 418 @login_required
bgneal@109 419 def mod_topic_delete(request, id):
bgneal@109 420 """
bgneal@109 421 This view function is for moderators to delete an entire topic.
bgneal@109 422 """
bgneal@109 423 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@109 424 if _can_moderate(topic.forum, request.user):
bgneal@109 425 forum_url = topic.forum.get_absolute_url()
bgneal@109 426 _delete_topic(topic)
bgneal@109 427 return HttpResponseRedirect(forum_url)
bgneal@109 428
bgneal@109 429 return HttpResponseForbidden()
bgneal@109 430
bgneal@109 431
bgneal@109 432 def _can_moderate(forum, user):
bgneal@109 433 """
bgneal@109 434 Determines if a user has permission to moderate a given forum.
bgneal@109 435 """
bgneal@109 436 return user.is_authenticated() and (
bgneal@109 437 user.is_superuser or user in forum.moderators.all())
bgneal@109 438
bgneal@109 439
bgneal@108 440 def _can_post_in_topic(topic, user):
bgneal@108 441 """
bgneal@108 442 This function returns true if the given user can post in the given topic
bgneal@108 443 and false otherwise.
bgneal@108 444 """
bgneal@108 445 return (not topic.locked and topic.forum.category.can_access(user)) or \
bgneal@108 446 (user.is_superuser or user in topic.forum.moderators.all())
bgneal@108 447
bgneal@108 448
bgneal@108 449 def _bump_post_count(user):
bgneal@108 450 """
bgneal@108 451 Increments the forum_post_count for the given user.
bgneal@108 452 """
bgneal@108 453 profile = user.get_profile()
bgneal@108 454 profile.forum_post_count += 1
bgneal@108 455 profile.save()
bgneal@108 456
bgneal@108 457
bgneal@108 458 def _quote_message(who, message):
bgneal@108 459 """
bgneal@108 460 Builds a message reply by quoting the existing message in a
bgneal@108 461 typical email-like fashion. The quoting is compatible with Markdown.
bgneal@108 462 """
bgneal@108 463 header = '*%s wrote:*\n\n' % (who, )
bgneal@108 464 lines = wrap(message, 55).split('\n')
bgneal@108 465 for i, line in enumerate(lines):
bgneal@108 466 lines[i] = '> ' + line
bgneal@108 467 return header + '\n'.join(lines)