comparison 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
comparison
equal deleted inserted replaced
790:6a06080e7ca8 791:0ca691cccf8d
55 s = cache.get(CACHE_KEY) 55 s = cache.get(CACHE_KEY)
56 if s: 56 if s:
57 return HttpResponse(s, content_type='application/json') 57 return HttpResponse(s, content_type='application/json')
58 58
59 # Compute JSON for the map 59 # Compute JSON for the map
60 entries = MapEntry.objects.all().select_related().order_by('user__username') 60 entries = MapEntry.objects.select_related('user', 'user__profile').\
61 order_by('user__username')
61 users = [] 62 users = []
62 user_ids = [] 63 avatar_urls = []
63 recent = [] 64 recent = []
64 for entry in entries.iterator(): 65 for entry in entries.iterator():
65 users.append(dict(name=entry.user.username, 66 users.append(dict(name=entry.user.username,
66 lat=entry.lat, 67 lat=entry.lat,
67 lon=entry.lon, 68 lon=entry.lon,
68 message=entry.html, 69 message=entry.html,
69 )) 70 ))
70 user_ids.append(entry.user.id) 71 avatar = entry.user.profile.avatar
72 if avatar and avatar.url:
73 avatar_urls.append(avatar.url)
74 else:
75 avatar_urls.append(None)
71 recent.append((entry.date_updated, entry.user.username)) 76 recent.append((entry.date_updated, entry.user.username))
72 77
73 # Get avatars for all users
74 profiles = UserProfile.objects.filter(user__in=user_ids).select_related()
75 avatars = {}
76 for profile in profiles.iterator():
77 if profile.avatar and profile.avatar.url:
78 avatars[profile.user.username] = profile.avatar.url
79
80 # Render the messages that go in the balloons 78 # Render the messages that go in the balloons
81 for user in users: 79 for user, avatar_url in zip(users, avatar_urls):
82 user['message'] = render_to_string('membermap/balloon.html', 80 user['message'] = render_to_string('membermap/balloon.html',
83 dict(user=user, avatar_url=avatars.get(user['name']))) 81 dict(user=user, avatar_url=avatar_url))
84 82
85 # Produce the list of recent updates 83 # Produce the list of recent updates
86 recent.sort(reverse=True) 84 recent.sort(reverse=True)
87 del recent[10:] 85 del recent[10:]
88 recent = [entry[1] for entry in recent] 86 recent = [entry[1] for entry in recent]