changeset 118:a20b2c492d55

Reduced some sql queries by adding a select_related and monkey patching user profiles onto a user list in shoutbox and the bio/member's list, respectively.
author Brian Neal <bgneal@gmail.com>
date Sat, 24 Oct 2009 02:39:19 +0000
parents a3633f39f3ce
children b8f1dcc9fae4
files gpp/bio/views.py gpp/shoutbox/views.py gpp/templates/bio/members.html
diffstat 3 files changed, 14 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/bio/views.py	Fri Oct 23 03:09:33 2009 +0000
+++ b/gpp/bio/views.py	Sat Oct 24 02:39:19 2009 +0000
@@ -35,6 +35,16 @@
     except InvalidPage:
         raise Http404
 
+    # Attach user profiles to each post to avoid using get_user_profile() in
+    # the template.
+    users = set(user.id for user in the_page.object_list)
+
+    profiles = UserProfile.objects.filter(user__id__in=users).select_related()
+    user_profiles = dict((profile.user.id, profile) for profile in profiles)
+
+    for user in the_page.object_list:
+        user.user_profile = user_profiles[user.id]
+
     return render_to_response('bio/members.html', {
         'page': the_page,
         'type': type,
--- a/gpp/shoutbox/views.py	Fri Oct 23 03:09:33 2009 +0000
+++ b/gpp/shoutbox/views.py	Sat Oct 24 02:39:19 2009 +0000
@@ -52,7 +52,8 @@
 
 def view_history(request, page=1):
     """This view allows one to view the shoutbox history."""
-    paginator = DiggPaginator(Shout.objects.all(), SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
+    paginator = DiggPaginator(Shout.objects.all().select_related(), 
+            SHOUTS_PER_PAGE, body=5, tail=3, margin=3, padding=2)
     try:
         the_page = paginator.page(int(page))
     except InvalidPage:
--- a/gpp/templates/bio/members.html	Fri Oct 23 03:09:33 2009 +0000
+++ b/gpp/templates/bio/members.html	Sat Oct 24 02:39:19 2009 +0000
@@ -32,12 +32,12 @@
    <td><a href="{% url bio-view_profile username=u.username %}">{% avatar u %}</a></td>
    <td><a href="{% url bio-view_profile username=u.username %}" title="View profile for {{ u.username }}">{{ u.username }}</a></td>
    <td>{{ u.get_full_name }}</td>
-   <td>{{ u.get_profile.location }}</td>
+   <td>{{ u.user_profile.location }}</td>
    <td>{{ u.date_joined|date:"M. d, Y" }}</td>
    <td>
       {% ifnotequal user u %}<a href="{% url messages-compose_to u.username %}">
          <img src="{{ MEDIA_URL }}icons/note.png" alt="PM" title="Send private message" /></a>{% endifnotequal %}
-      {% if not u.get_profile.hide_email %}<a href="mailto:{{ u.email }}">
+      {% if not u.user_profile.hide_email %}<a href="mailto:{{ u.email }}">
          <img src="{{ MEDIA_URL }}icons/email.png" alt="Email" title="Send Email" /></a>{% endif %}
    </td>
 </tr>