Mercurial > public > sg101
diff forums/views/main.py @ 791:0ca691cccf8d
Utilize select_related() for user & user profiles.
This commit also removes the caching of the avatar URL in the
avatar template tag. This is because we are now using select_related,
so we already have the profile & avatar when we get to the tag.
Thus we don't need to waste time querying the cache.
Removed an apparently unused member map template as well.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 23 May 2014 21:52:41 -0500 |
parents | 9e803323a0d0 |
children | 71a671dab55d 4619290d171d |
line wrap: on
line diff
--- a/forums/views/main.py Fri May 23 15:39:14 2014 -0500 +++ b/forums/views/main.py Fri May 23 21:52:41 2014 -0500 @@ -23,12 +23,12 @@ import antispam import antispam.utils -from bio.models import UserProfile, BadgeOwnership +from bio.models import BadgeOwnership from core.paginator import DiggPaginator from core.functions import email_admins, quote_message from forums.models import (Forum, Topic, Post, FlaggedPost, TopicLastVisit, - ForumLastVisit, Attachment) + ForumLastVisit) from forums.forms import (NewTopicForm, NewPostForm, PostForm, MoveTopicForm, SplitTopicForm) from forums.unread import (get_forum_unread_status, get_topic_unread_status, @@ -170,7 +170,9 @@ topic.view_count = F('view_count') + 1 topic.save(force_update=True) - posts = topic.posts.select_related('user') + posts = topic.posts.\ + select_related('user', 'user__profile').\ + prefetch_related('attachments') paginator = create_post_paginator(posts) page_num = get_page_num(request) @@ -180,39 +182,23 @@ raise Http404 get_post_unread_status(topic, page.object_list, request.user) - # Attach user profiles to each post's user to avoid using - # get_user_profile() in the template. - users = set(post.user.id for post in page.object_list) - - profiles = UserProfile.objects.filter(user__id__in=users).select_related() - profile_keys = [profile.id for profile in profiles] - user_profiles = dict((profile.user.id, profile) for profile in profiles) + # Get the BadgeOwnership & Badges for each user who has posted in the + # thread. This is done to save SQL queries in the template. last_post_on_page = None + profile_ids = [] for post in page.object_list: - post.user.user_profile = user_profiles[post.user.id] - post.attach_list = [] last_post_on_page = post + profile_ids.append(post.user.profile.pk) - # Attach badge ownership info to the user profiles to avoid lots - # of database hits in the template: - bos_qs = BadgeOwnership.objects.filter( - profile__id__in=profile_keys).select_related() + bo_qs = BadgeOwnership.objects.filter(profile__in=profile_ids).\ + select_related() bos = collections.defaultdict(list) - for bo in bos_qs: - bos[bo.profile.id].append(bo) + for bo in bo_qs: + bos[bo.profile.pk].append(bo) - for user_id, profile in user_profiles.iteritems(): - profile.badge_ownership = bos[profile.id] - - # Attach any attachments - post_ids = [post.pk for post in page.object_list] - attachments = Attachment.objects.filter(post__in=post_ids).select_related( - 'embed').order_by('order') - - post_dict = dict((post.pk, post) for post in page.object_list) - for item in attachments: - post_dict[item.post.id].attach_list.append(item.embed) + for post in page.object_list: + post.user.profile.badge_ownership = bos[post.user.profile.pk] last_page = page_num == paginator.num_pages @@ -468,8 +454,6 @@ else: form = PostForm(instance=post, topic_name=topic_name) - post.user.user_profile = post.user.profile - return render_to_response('forums/edit_post.html', { 'forum': post.topic.forum, 'topic': post.topic,