annotate gpp/forums/views.py @ 215:8c1832b9d815

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