changeset 43:2763977301c2

Added support for caching. The avatar template tag now uses the cache.
author Brian Neal <bgneal@gmail.com>
date Sun, 14 Jun 2009 20:25:22 +0000
parents 48b221d304c6
children 08cd19c1ee50
files gpp/bio/templatetags/avatar_tags.py gpp/settings.py gpp/templates/base.html gpp/templates/home.html
diffstat 4 files changed, 33 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/gpp/bio/templatetags/avatar_tags.py	Fri Jun 12 02:37:28 2009 +0000
+++ b/gpp/bio/templatetags/avatar_tags.py	Sun Jun 14 20:25:22 2009 +0000
@@ -3,30 +3,38 @@
 """
 from django import template
 from django.conf import settings
+from django.core.cache import cache
 
 register = template.Library()
 
 
 @register.simple_tag
 def avatar(user, align='bottom'):
-   alt = user.username
-   title = alt
-   try:
+    cache_key = 'avatar_' + user.username
+    img_tag = cache.get(cache_key)
+    if img_tag:
+        return img_tag
+
+    alt = user.username
+    title = alt
+    try:
       profile = user.get_profile()
-   except:
+    except:
       profile = None
-   if profile is None or profile.avatar.name == '':
+    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:
+    else:
       url = profile.avatar.url
       width = profile.avatar.width
       height = profile.avatar.height
 
-   style = ''
-   if align == 'left':
+    style = ''
+    if align == 'left':
       style = 'style="float:left;margin-right:3px;"'
 
-   return u"""<img src="%s" alt="%s" title="%s" width="%s" height="%s" border="0" class="avatar" %s />""" % (
+    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)
+    return img_tag
--- a/gpp/settings.py	Fri Jun 12 02:37:28 2009 +0000
+++ b/gpp/settings.py	Sun Jun 14 20:25:22 2009 +0000
@@ -134,6 +134,15 @@
 FILE_UPLOAD_PERMISSIONS = 0640
 
 #######################################################################
+# Caching
+#######################################################################
+if local_settings.USE_CACHE:
+    CACHE_BACKEND = local_settings.CACHE_BACKEND
+    #CACHE_MIDDLEWARE_SECONDS = local_settings.CACHE_MIDDLEWARE_SECONDS
+    #CACHE_MIDDLEWARE_KEY_PREFIX = local_settings.CACHE_MIDDLEWARE_KEY_PREFIX
+    #CACHE_MIDDLEWARE_ANONYMOUS_ONLY = local_settings.CACHE_MIDDLEWARE_ANONYMOUS_ONLY
+
+#######################################################################
 # Tagging Specific Settings
 #######################################################################
 FORCE_LOWERCASE_TAGS = True
--- a/gpp/templates/base.html	Fri Jun 12 02:37:28 2009 +0000
+++ b/gpp/templates/base.html	Sun Jun 14 20:25:22 2009 +0000
@@ -7,6 +7,7 @@
 {% load potd_tags %}
 {% load messages_tags %}
 {% load script_tags %}
+{% load cache %}
 <head><title>SurfGuitar101.com | {% block title %}{% endblock %}</title>
 <meta http-equiv="Content-Type" content="text/html" />
 <meta http-equiv="Content-Language" content="en-US" />
@@ -63,7 +64,9 @@
       <li><a href="{% url weblinks-main %}">Links</a></li>
       <li><a href="{% url downloads-index %}">Downloads</a></li>
    </ul>
-   {% photo_of_the_day %}
+   {% cache 300 potd_block %}
+      {% photo_of_the_day %}
+   {% endcache %}
    {% shoutbox %}
    {% irc_status %}
 </div>
--- a/gpp/templates/home.html	Fri Jun 12 02:37:28 2009 +0000
+++ b/gpp/templates/home.html	Sun Jun 14 20:25:22 2009 +0000
@@ -3,6 +3,7 @@
 {% load news_tags %}
 {% load weblinks_tags %}
 {% load downloads_tags %}
+{% load cache %}
 {% block title %}Home{% endblock %}
 {% block custom_css %}
 <link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/news.css" />
@@ -35,6 +36,7 @@
 permanent. The site (content, user accounts, etc.) may be wiped at any time while we fix bugs and add features. 
 Also, I hope that the look and feel of this site can be greatly improved to something really cool! I'll need
 lots of help for this aspect, as I'm more of a coder than a designer.</p>
+{% cache 600 home_content %}
 {% current_bulletins %}
 {% current_news %}
 <div class="span-9 append-1">
@@ -43,4 +45,5 @@
 <div class="span-9 last">
    {% latest_downloads %} 
 </div>
+{% endcache %}
 {% endblock %}