comparison gpp/forums/views/main.py @ 329:000c006fee97

Various small changes to reduce database hits.
author Brian Neal <bgneal@gmail.com>
date Wed, 23 Feb 2011 03:40:18 +0000
parents 3f9b9fd54b01
children a43add8af83d
comparison
equal deleted inserted replaced
328:3f9b9fd54b01 329:000c006fee97
1 """ 1 """
2 Views for the forums application. 2 Views for the forums application.
3 """ 3 """
4 import collections
4 import datetime 5 import datetime
5 6
6 from django.contrib.auth.decorators import login_required 7 from django.contrib.auth.decorators import login_required
7 from django.contrib.auth.models import User 8 from django.contrib.auth.models import User
8 from django.http import Http404 9 from django.http import Http404
27 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ 28 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \
28 SplitTopicForm 29 SplitTopicForm
29 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ 30 from forums.unread import get_forum_unread_status, get_topic_unread_status, \
30 get_post_unread_status, get_unread_topics 31 get_post_unread_status, get_unread_topics
31 32
32 from bio.models import UserProfile 33 from bio.models import UserProfile, BadgeOwnership
33 import antispam 34 import antispam
34 import antispam.utils 35 import antispam.utils
35 from forums.attachments import AttachmentProcessor 36 from forums.attachments import AttachmentProcessor
36 37
37 ####################################################################### 38 #######################################################################
173 page = paginator.page(page_num) 174 page = paginator.page(page_num)
174 except InvalidPage: 175 except InvalidPage:
175 raise Http404 176 raise Http404
176 get_post_unread_status(topic, page.object_list, request.user) 177 get_post_unread_status(topic, page.object_list, request.user)
177 178
178 # Attach user profiles to each post to avoid using get_user_profile() in 179 # Attach user profiles to each post's user to avoid using
179 # the template. 180 # get_user_profile() in the template.
180 users = set(post.user.id for post in page.object_list) 181 users = set(post.user.id for post in page.object_list)
181 182
182 profiles = UserProfile.objects.filter(user__id__in=users).select_related() 183 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
183 user_profiles = dict((profile.user.id, profile) for profile in profiles) 184 user_profiles = dict((profile.user.id, profile) for profile in profiles)
184 185
185 for post in page.object_list: 186 for post in page.object_list:
186 post.user_profile = user_profiles[post.user.id] 187 post.user.user_profile = user_profiles[post.user.id]
187 post.attach_list = [] 188 post.attach_list = []
189
190 # Attach badge ownership info to the user profiles to avoid lots
191 # of database hits in the template:
192 bos_qs = BadgeOwnership.objects.filter(
193 profile__id__in=user_profiles.keys()).select_related()
194 bos = collections.defaultdict(list)
195 for bo in bos_qs:
196 bos[bo.profile.id].append(bo)
197
198 for pk, profile in user_profiles.iteritems():
199 profile.badge_ownership = bos[pk]
188 200
189 # Attach any attachments 201 # Attach any attachments
190 post_ids = [post.pk for post in page.object_list] 202 post_ids = [post.pk for post in page.object_list]
191 attachments = Attachment.objects.filter(post__in=post_ids).select_related( 203 attachments = Attachment.objects.filter(post__in=post_ids).select_related(
192 'embed').order_by('order') 204 'embed').order_by('order')
290 if antispam.utils.spam_check(request, form.cleaned_data['body']): 302 if antispam.utils.spam_check(request, form.cleaned_data['body']):
291 return HttpResponseForbidden(antispam.BUSTED_MESSAGE) 303 return HttpResponseForbidden(antispam.BUSTED_MESSAGE)
292 304
293 post = form.save(request.user, request.META.get("REMOTE_ADDR", "")) 305 post = form.save(request.user, request.META.get("REMOTE_ADDR", ""))
294 post.unread = True 306 post.unread = True
295 post.user_profile = request.user.get_profile() 307 post.user.user_profile = request.user.get_profile()
296 post.attach_list = post.attachments.all() 308 post.attach_list = post.attachments.all()
297 _bump_post_count(request.user) 309 _bump_post_count(request.user)
298 _update_last_visit(request.user, form.topic) 310 _update_last_visit(request.user, form.topic)
299 311
300 return render_to_response('forums/display_post.html', { 312 return render_to_response('forums/display_post.html', {
386 398
387 return HttpResponseRedirect(post.get_absolute_url()) 399 return HttpResponseRedirect(post.get_absolute_url())
388 else: 400 else:
389 form = PostForm(instance=post, topic_name=topic_name) 401 form = PostForm(instance=post, topic_name=topic_name)
390 402
391 post.user_profile = post.user.get_profile() 403 post.user.user_profile = post.user.get_profile()
392 404
393 return render_to_response('forums/edit_post.html', { 405 return render_to_response('forums/edit_post.html', {
394 'forum': post.topic.forum, 406 'forum': post.topic.forum,
395 'topic': post.topic, 407 'topic': post.topic,
396 'post': post, 408 'post': post,