annotate gpp/forums/views.py @ 101:4bbb6a9aa317

Forums: use select_related() with get_object_or_404() to reduce queries.
author Brian Neal <bgneal@gmail.com>
date Wed, 16 Sep 2009 00:39:27 +0000
parents eb9f99382476
children e67c4dd98db5
rev   line source
bgneal@81 1 """
bgneal@81 2 Views for the forums application.
bgneal@81 3 """
bgneal@83 4 from django.contrib.auth.decorators import login_required
bgneal@82 5 from django.http import Http404
bgneal@98 6 from django.http import HttpResponse
bgneal@89 7 from django.http import HttpResponseBadRequest
bgneal@90 8 from django.http import HttpResponseForbidden
bgneal@83 9 from django.http import HttpResponseRedirect
bgneal@83 10 from django.core.urlresolvers import reverse
bgneal@91 11 from django.core.paginator import InvalidPage
bgneal@82 12 from django.shortcuts import get_object_or_404
bgneal@81 13 from django.shortcuts import render_to_response
bgneal@97 14 from django.template.loader import render_to_string
bgneal@81 15 from django.template import RequestContext
bgneal@89 16 from django.views.decorators.http import require_POST
bgneal@81 17
bgneal@90 18 from core.paginator import DiggPaginator
bgneal@98 19 from core.functions import email_admins
bgneal@81 20 from forums.models import Forum
bgneal@83 21 from forums.models import Topic
bgneal@91 22 from forums.models import Post
bgneal@98 23 from forums.models import FlaggedPost
bgneal@83 24 from forums.forms import NewTopicForm
bgneal@86 25 from forums.forms import PostForm
bgneal@81 26
bgneal@90 27 #######################################################################
bgneal@90 28
bgneal@93 29 TOPICS_PER_PAGE = 50
bgneal@90 30 POSTS_PER_PAGE = 2
bgneal@90 31
bgneal@93 32 def create_topic_paginator(topics):
bgneal@93 33 return DiggPaginator(topics, TOPICS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
bgneal@93 34
bgneal@93 35 def create_post_paginator(posts):
bgneal@93 36 return DiggPaginator(posts, POSTS_PER_PAGE, body=5, tail=2, margin=3, padding=2)
bgneal@90 37
bgneal@90 38 #######################################################################
bgneal@81 39
bgneal@81 40 def index(request):
bgneal@82 41 """
bgneal@82 42 This view displays all the forums available, ordered in each category.
bgneal@82 43 """
bgneal@100 44 forums = Forum.objects.forums_for_user(request.user)
bgneal@81 45 cats = {}
bgneal@81 46 for forum in forums:
bgneal@81 47 cat = cats.setdefault(forum.category.id, {
bgneal@81 48 'cat': forum.category,
bgneal@81 49 'forums': [],
bgneal@81 50 })
bgneal@81 51 cat['forums'].append(forum)
bgneal@81 52
bgneal@81 53 cmpdef = lambda a, b: cmp(a['cat'].position, b['cat'].position)
bgneal@81 54 cats = sorted(cats.values(), cmpdef)
bgneal@81 55
bgneal@81 56 return render_to_response('forums/index.html', {
bgneal@81 57 'cats': cats,
bgneal@81 58 },
bgneal@81 59 context_instance=RequestContext(request))
bgneal@81 60
bgneal@82 61
bgneal@81 62 def forum_index(request, slug):
bgneal@82 63 """
bgneal@82 64 Displays all the topics in a forum.
bgneal@82 65 """
bgneal@101 66 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@100 67
bgneal@100 68 if not forum.category.can_access(request.user):
bgneal@100 69 return HttpResponseForbidden()
bgneal@100 70
bgneal@100 71 topics = forum.topics.select_related('last_post', 'last_post__user')
bgneal@93 72 paginator = create_topic_paginator(topics)
bgneal@93 73 page_num = int(request.GET.get('page', 1))
bgneal@93 74 try:
bgneal@93 75 page = paginator.page(page_num)
bgneal@93 76 except InvalidPage:
bgneal@93 77 raise Http404
bgneal@97 78
bgneal@97 79 # we do this for the template since it is rendered twice
bgneal@97 80 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@82 81
bgneal@82 82 return render_to_response('forums/forum_index.html', {
bgneal@82 83 'forum': forum,
bgneal@93 84 'page': page,
bgneal@97 85 'page_nav': page_nav,
bgneal@82 86 },
bgneal@82 87 context_instance=RequestContext(request))
bgneal@82 88
bgneal@82 89
bgneal@82 90 def topic_index(request, id):
bgneal@82 91 """
bgneal@82 92 Displays all the posts in a topic.
bgneal@82 93 """
bgneal@101 94 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
bgneal@100 95
bgneal@100 96 if not topic.forum.category.can_access(request.user):
bgneal@100 97 return HttpResponseForbidden()
bgneal@100 98
bgneal@86 99 topic.view_count += 1
bgneal@86 100 topic.save()
bgneal@86 101
bgneal@86 102 posts = topic.posts.select_related()
bgneal@93 103 paginator = create_post_paginator(posts)
bgneal@93 104 page_num = int(request.GET.get('page', 1))
bgneal@90 105 try:
bgneal@90 106 page = paginator.page(page_num)
bgneal@90 107 except InvalidPage:
bgneal@90 108 raise Http404
bgneal@90 109
bgneal@90 110 last_page = page_num == paginator.num_pages
bgneal@86 111
bgneal@97 112 # we do this for the template since it is rendered twice
bgneal@97 113 page_nav = render_to_string('forums/pagination.html', {'page': page})
bgneal@97 114
bgneal@86 115 return render_to_response('forums/topic.html', {
bgneal@86 116 'forum': topic.forum,
bgneal@86 117 'topic': topic,
bgneal@90 118 'page': page,
bgneal@97 119 'page_nav': page_nav,
bgneal@87 120 'last_page': last_page,
bgneal@89 121 'form': PostForm(initial={'topic_id': topic.id}),
bgneal@86 122 },
bgneal@86 123 context_instance=RequestContext(request))
bgneal@83 124
bgneal@83 125
bgneal@83 126 @login_required
bgneal@83 127 def new_topic(request, slug):
bgneal@83 128 """
bgneal@83 129 This view handles the creation of new topics.
bgneal@83 130 """
bgneal@101 131 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
bgneal@100 132
bgneal@100 133 if not forum.category.can_access(request.user):
bgneal@100 134 return HttpResponseForbidden()
bgneal@100 135
bgneal@83 136 if request.method == 'POST':
bgneal@83 137 form = NewTopicForm(request.POST)
bgneal@83 138 if form.is_valid():
bgneal@83 139 topic = form.save(forum, request.user, request.META.get("REMOTE_ADDR"))
bgneal@83 140 return HttpResponseRedirect(reverse('forums-new_topic_thanks',
bgneal@83 141 kwargs={'tid': topic.pk}))
bgneal@83 142 else:
bgneal@83 143 form = NewTopicForm()
bgneal@83 144
bgneal@83 145 return render_to_response('forums/new_topic.html', {
bgneal@83 146 'forum': forum,
bgneal@83 147 'form': form,
bgneal@83 148 },
bgneal@83 149 context_instance=RequestContext(request))
bgneal@83 150
bgneal@83 151
bgneal@83 152 @login_required
bgneal@83 153 def new_topic_thanks(request, tid):
bgneal@83 154 """
bgneal@83 155 This view displays the success page for a newly created topic.
bgneal@83 156 """
bgneal@101 157 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
bgneal@83 158 return render_to_response('forums/new_topic_thanks.html', {
bgneal@83 159 'forum': topic.forum,
bgneal@83 160 'topic': topic,
bgneal@83 161 },
bgneal@83 162 context_instance=RequestContext(request))
bgneal@89 163
bgneal@89 164
bgneal@89 165 @require_POST
bgneal@89 166 def quick_reply_ajax(request):
bgneal@89 167 """
bgneal@89 168 This function handles the quick reply to a thread function. This
bgneal@89 169 function is meant to be the target of an AJAX post, and returns
bgneal@89 170 the HTML for the new post, which the client-side script appends
bgneal@89 171 to the document.
bgneal@89 172 """
bgneal@90 173 if not request.user.is_authenticated():
bgneal@90 174 return HttpResponseForbidden()
bgneal@90 175
bgneal@89 176 form = PostForm(request.POST)
bgneal@89 177 if form.is_valid():
bgneal@100 178 if not form.topic.forum.category.can_access(request.user):
bgneal@100 179 return HttpResponseForbidden()
bgneal@100 180
bgneal@89 181 post = form.save(request.user, request.META.get("REMOTE_ADDR"))
bgneal@89 182 return render_to_response('forums/display_post.html', {
bgneal@89 183 'post': post,
bgneal@89 184 },
bgneal@89 185 context_instance=RequestContext(request))
bgneal@89 186
bgneal@89 187 return HttpResponseBadRequest();
bgneal@89 188
bgneal@91 189
bgneal@91 190 def goto_post(request, post_id):
bgneal@91 191 """
bgneal@91 192 This function calculates what page a given post is on, then redirects
bgneal@91 193 to that URL. This function is the target of get_absolute_url() for
bgneal@91 194 Post objects.
bgneal@91 195 """
bgneal@101 196 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
bgneal@91 197 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
bgneal@91 198 page = count / POSTS_PER_PAGE + 1
bgneal@91 199 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
bgneal@91 200 '?page=%s#p%s' % (page, post.id)
bgneal@91 201 return HttpResponseRedirect(url)
bgneal@91 202
bgneal@98 203
bgneal@98 204 @require_POST
bgneal@98 205 def flag_post(request):
bgneal@98 206 """
bgneal@98 207 This function handles the flagging of posts by users. This function should
bgneal@98 208 be the target of an AJAX post.
bgneal@98 209 """
bgneal@98 210 if not request.user.is_authenticated():
bgneal@99 211 return HttpResponseForbidden('Please login or register to flag a post.')
bgneal@98 212
bgneal@98 213 id = request.POST.get('id')
bgneal@98 214 if id is None:
bgneal@98 215 return HttpResponseBadRequest('No post id')
bgneal@98 216
bgneal@98 217 try:
bgneal@98 218 post = Post.objects.get(pk=id)
bgneal@98 219 except Post.DoesNotExist:
bgneal@98 220 return HttpResponseBadRequest('No post with id %s' % id)
bgneal@98 221
bgneal@98 222 flag = FlaggedPost(user=request.user, post=post)
bgneal@98 223 flag.save()
bgneal@98 224 email_admins('A Post Has Been Flagged', """Hello,
bgneal@98 225
bgneal@98 226 A user has flagged a forum post for review.
bgneal@98 227 """)
bgneal@98 228 return HttpResponse('The post was flagged. A moderator will review the post shortly. ' \
bgneal@98 229 'Thanks for helping to improve the discussions on this site.')