annotate gpp/forums/views.py @ 172:0fa78ef80356

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