annotate gpp/forums/views.py @ 181:500e5875a306

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