changeset 234:f53eb0aae7a9

Fix #89; avatar caching was broken.
author Brian Neal <bgneal@gmail.com>
date Sun, 01 Aug 2010 22:14:01 +0000 (2010-08-01)
parents 6dde069debd4
children d302c498560e
files gpp/bio/templatetags/avatar_tags.py
diffstat 1 files changed, 22 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/bio/templatetags/avatar_tags.py	Sun Aug 01 21:30:22 2010 +0000
+++ b/gpp/bio/templatetags/avatar_tags.py	Sun Aug 01 22:14:01 2010 +0000
@@ -10,31 +10,35 @@
 
 @register.simple_tag
 def avatar(user, align='bottom'):
+    """Returns the HTML for a user's avatar image."""
+
+    # In the cache is stored a tuple containing info about the 
+    # avatar: (url, width, height)
     cache_key = 'avatar_' + user.username
-    img_tag = cache.get(cache_key)
-    if img_tag:
-        return img_tag
+    img_info = cache.get(cache_key)
+    if img_info is None:
+        try:
+            profile = user.get_profile()
+        except:
+            profile = None
+        if profile is None or profile.avatar.name == '':
+            img_info = (settings.AVATAR_DEFAULT_URL, 
+                settings.MAX_AVATAR_SIZE_PIXELS, 
+                settings.MAX_AVATAR_SIZE_PIXELS)
+        else:
+            img_info = (profile.avatar.url, profile.avatar.width,
+                profile.avatar.height)
+        cache.set(cache_key, img_info)
 
     alt = user.username
     title = alt
-    try:
-      profile = user.get_profile()
-    except:
-      profile = None
-    if profile is None or profile.avatar.name == '':
-      url = settings.AVATAR_DEFAULT_URL
-      width = settings.MAX_AVATAR_SIZE_PIXELS
-      height = settings.MAX_AVATAR_SIZE_PIXELS
-    else:
-      url = profile.avatar.url
-      width = profile.avatar.width
-      height = profile.avatar.height
 
     style = ''
     if align == 'left':
       style = 'style="float:left;margin-right:3px;"'
+    # other styles not supported
 
-    img_tag = u"""<img src="%s" alt="%s" title="%s" width="%s" height="%s" border="0" class="avatar" %s />""" % (
-         url, alt, title, width, height, style)
-    cache.set(cache_key, img_tag)
+    img_tag = (u'<img src="%s" alt="%s" title="%s" width="%s" height="%s" '
+                'border="0" class="avatar" %s />') % (
+         img_info[0], alt, title, img_info[1], img_info[2], style)
     return img_tag