comparison 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
comparison
equal deleted inserted replaced
100:eb9f99382476 101:4bbb6a9aa317
61 61
62 def forum_index(request, slug): 62 def forum_index(request, slug):
63 """ 63 """
64 Displays all the topics in a forum. 64 Displays all the topics in a forum.
65 """ 65 """
66 # TODO: use select_related to save queries 66 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
67 forum = get_object_or_404(Forum, slug=slug)
68 67
69 if not forum.category.can_access(request.user): 68 if not forum.category.can_access(request.user):
70 return HttpResponseForbidden() 69 return HttpResponseForbidden()
71 70
72 topics = forum.topics.select_related('last_post', 'last_post__user') 71 topics = forum.topics.select_related('last_post', 'last_post__user')
90 89
91 def topic_index(request, id): 90 def topic_index(request, id):
92 """ 91 """
93 Displays all the posts in a topic. 92 Displays all the posts in a topic.
94 """ 93 """
95 #topic = get_object_or_404(Topic, pk=id) 94 topic = get_object_or_404(Topic.objects.select_related(), pk=id)
96 #TODO: optimize this or package it up somehow
97 # this saves 2 queries vs the get_object_or_404
98 qs = Topic.objects.filter(pk=id).select_related()
99 try:
100 topic = qs[0]
101 except Topic.DoesNotExist:
102 raise Http404
103 95
104 if not topic.forum.category.can_access(request.user): 96 if not topic.forum.category.can_access(request.user):
105 return HttpResponseForbidden() 97 return HttpResponseForbidden()
106 98
107 topic.view_count += 1 99 topic.view_count += 1
134 @login_required 126 @login_required
135 def new_topic(request, slug): 127 def new_topic(request, slug):
136 """ 128 """
137 This view handles the creation of new topics. 129 This view handles the creation of new topics.
138 """ 130 """
139 forum = get_object_or_404(Forum, slug=slug) 131 forum = get_object_or_404(Forum.objects.select_related(), slug=slug)
140 132
141 if not forum.category.can_access(request.user): 133 if not forum.category.can_access(request.user):
142 return HttpResponseForbidden() 134 return HttpResponseForbidden()
143 135
144 if request.method == 'POST': 136 if request.method == 'POST':
160 @login_required 152 @login_required
161 def new_topic_thanks(request, tid): 153 def new_topic_thanks(request, tid):
162 """ 154 """
163 This view displays the success page for a newly created topic. 155 This view displays the success page for a newly created topic.
164 """ 156 """
165 topic = get_object_or_404(Topic, pk=tid) 157 topic = get_object_or_404(Topic.objects.select_related(), pk=tid)
166 return render_to_response('forums/new_topic_thanks.html', { 158 return render_to_response('forums/new_topic_thanks.html', {
167 'forum': topic.forum, 159 'forum': topic.forum,
168 'topic': topic, 160 'topic': topic,
169 }, 161 },
170 context_instance=RequestContext(request)) 162 context_instance=RequestContext(request))
199 """ 191 """
200 This function calculates what page a given post is on, then redirects 192 This function calculates what page a given post is on, then redirects
201 to that URL. This function is the target of get_absolute_url() for 193 to that URL. This function is the target of get_absolute_url() for
202 Post objects. 194 Post objects.
203 """ 195 """
204 post = get_object_or_404(Post, pk=post_id) 196 post = get_object_or_404(Post.objects.select_related(), pk=post_id)
205 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count() 197 count = post.topic.posts.filter(creation_date__lt=post.creation_date).count()
206 page = count / POSTS_PER_PAGE + 1 198 page = count / POSTS_PER_PAGE + 1
207 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \ 199 url = reverse('forums-topic_index', kwargs={'id': post.topic.id}) + \
208 '?page=%s#p%s' % (page, post.id) 200 '?page=%s#p%s' % (page, post.id)
209 return HttpResponseRedirect(url) 201 return HttpResponseRedirect(url)