Mercurial > public > sg101
comparison gpp/forums/views.py @ 117:a3633f39f3ce
Forums: pre-select all the user profiles when displaying a topic. As it was, the template was calling get_user_profile which was causing a db hit on every post in the topic.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 23 Oct 2009 03:09:33 +0000 |
parents | 0ce0104c7df3 |
children | f8f4514b806a |
comparison
equal
deleted
inserted
replaced
116:19b64e8f02a2 | 117:a3633f39f3ce |
---|---|
25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ | 25 from forums.forms import NewTopicForm, NewPostForm, PostForm, MoveTopicForm, \ |
26 SplitTopicForm | 26 SplitTopicForm |
27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ | 27 from forums.unread import get_forum_unread_status, get_topic_unread_status, \ |
28 get_post_unread_status | 28 get_post_unread_status |
29 | 29 |
30 from bio.models import UserProfile | |
30 ####################################################################### | 31 ####################################################################### |
31 | 32 |
32 TOPICS_PER_PAGE = 50 | 33 TOPICS_PER_PAGE = 50 |
33 POSTS_PER_PAGE = 20 | 34 POSTS_PER_PAGE = 20 |
34 | 35 |
107 | 108 |
108 topic.view_count += 1 | 109 topic.view_count += 1 |
109 topic.save() | 110 topic.save() |
110 | 111 |
111 posts = topic.posts.select_related() | 112 posts = topic.posts.select_related() |
112 get_post_unread_status(topic, posts, request.user) | |
113 | 113 |
114 paginator = create_post_paginator(posts) | 114 paginator = create_post_paginator(posts) |
115 page_num = int(request.GET.get('page', 1)) | 115 page_num = int(request.GET.get('page', 1)) |
116 try: | 116 try: |
117 page = paginator.page(page_num) | 117 page = paginator.page(page_num) |
118 except InvalidPage: | 118 except InvalidPage: |
119 raise Http404 | 119 raise Http404 |
120 get_post_unread_status(topic, page.object_list, request.user) | |
121 | |
122 # Attach user profiles to each post to avoid using get_user_profile() in | |
123 # the template. | |
124 users = set(post.user.id for post in page.object_list) | |
125 | |
126 profiles = UserProfile.objects.filter(user__id__in=users).select_related() | |
127 user_profiles = dict((profile.user.id, profile) for profile in profiles) | |
128 | |
129 for post in page.object_list: | |
130 post.user_profile = user_profiles[post.user.id] | |
120 | 131 |
121 last_page = page_num == paginator.num_pages | 132 last_page = page_num == paginator.num_pages |
122 | 133 |
123 if request.user.is_authenticated() and last_page: | 134 if request.user.is_authenticated() and last_page: |
124 _update_last_visit(request.user, topic) | 135 _update_last_visit(request.user, topic) |