diff membermap/views.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 21c592cac71c
line wrap: on
line diff
--- a/membermap/views.py	Fri May 23 15:39:14 2014 -0500
+++ b/membermap/views.py	Fri May 23 21:52:41 2014 -0500
@@ -57,9 +57,10 @@
         return HttpResponse(s, content_type='application/json')
 
     # Compute JSON for the map
-    entries = MapEntry.objects.all().select_related().order_by('user__username')
+    entries = MapEntry.objects.select_related('user', 'user__profile').\
+            order_by('user__username')
     users = []
-    user_ids = []
+    avatar_urls = []
     recent = []
     for entry in entries.iterator():
         users.append(dict(name=entry.user.username,
@@ -67,20 +68,17 @@
             lon=entry.lon,
             message=entry.html,
             ))
-        user_ids.append(entry.user.id)
+        avatar = entry.user.profile.avatar
+        if avatar and avatar.url:
+            avatar_urls.append(avatar.url)
+        else:
+            avatar_urls.append(None)
         recent.append((entry.date_updated, entry.user.username))
 
-    # Get avatars for all users
-    profiles = UserProfile.objects.filter(user__in=user_ids).select_related()
-    avatars = {}
-    for profile in profiles.iterator():
-        if profile.avatar and profile.avatar.url:
-            avatars[profile.user.username] = profile.avatar.url
-
     # Render the messages that go in the balloons
-    for user in users:
+    for user, avatar_url in zip(users, avatar_urls):
         user['message'] = render_to_string('membermap/balloon.html',
-                dict(user=user, avatar_url=avatars.get(user['name'])))
+                dict(user=user, avatar_url=avatar_url))
 
     # Produce the list of recent updates
     recent.sort(reverse=True)