comparison gpp/forums/views/main.py @ 349:a43add8af83d

Fix #171; forums weren't assigning badge ownership correctly when pre-fetching data.
author Brian Neal <bgneal@gmail.com>
date Wed, 02 Mar 2011 02:34:57 +0000
parents 000c006fee97
children dd673fae508d
comparison
equal deleted inserted replaced
348:d1b11096595b 349:a43add8af83d
179 # Attach user profiles to each post's user to avoid using 179 # Attach user profiles to each post's user to avoid using
180 # get_user_profile() in the template. 180 # get_user_profile() in the template.
181 users = set(post.user.id for post in page.object_list) 181 users = set(post.user.id for post in page.object_list)
182 182
183 profiles = UserProfile.objects.filter(user__id__in=users).select_related() 183 profiles = UserProfile.objects.filter(user__id__in=users).select_related()
184 profile_keys = [profile.id for profile in profiles]
184 user_profiles = dict((profile.user.id, profile) for profile in profiles) 185 user_profiles = dict((profile.user.id, profile) for profile in profiles)
185 186
186 for post in page.object_list: 187 for post in page.object_list:
187 post.user.user_profile = user_profiles[post.user.id] 188 post.user.user_profile = user_profiles[post.user.id]
188 post.attach_list = [] 189 post.attach_list = []
189 190
190 # Attach badge ownership info to the user profiles to avoid lots 191 # Attach badge ownership info to the user profiles to avoid lots
191 # of database hits in the template: 192 # of database hits in the template:
192 bos_qs = BadgeOwnership.objects.filter( 193 bos_qs = BadgeOwnership.objects.filter(
193 profile__id__in=user_profiles.keys()).select_related() 194 profile__id__in=profile_keys).select_related()
194 bos = collections.defaultdict(list) 195 bos = collections.defaultdict(list)
195 for bo in bos_qs: 196 for bo in bos_qs:
196 bos[bo.profile.id].append(bo) 197 bos[bo.profile.id].append(bo)
197 198
198 for pk, profile in user_profiles.iteritems(): 199 for user_id, profile in user_profiles.iteritems():
199 profile.badge_ownership = bos[pk] 200 profile.badge_ownership = bos[profile.id]
200 201
201 # Attach any attachments 202 # Attach any attachments
202 post_ids = [post.pk for post in page.object_list] 203 post_ids = [post.pk for post in page.object_list]
203 attachments = Attachment.objects.filter(post__in=post_ids).select_related( 204 attachments = Attachment.objects.filter(post__in=post_ids).select_related(
204 'embed').order_by('order') 205 'embed').order_by('order')