diff gpp/smiley/models.py @ 128:48621ba5c385

Fixing #36, Smilify doesn't work when a smiley appears first before other text. Refactored the smiley system to produce markdown as well as HTML.
author Brian Neal <bgneal@gmail.com>
date Fri, 20 Nov 2009 01:43:32 +0000
parents 3ae999b0c53b
children 3a626c48e9ae
line wrap: on
line diff
--- a/gpp/smiley/models.py	Mon Nov 16 01:00:28 2009 +0000
+++ b/gpp/smiley/models.py	Fri Nov 20 01:43:32 2009 +0000
@@ -4,17 +4,23 @@
 from django.db import models
 from django.core.cache import cache
 
-CACHE_TIMEOUT = 60 * 5      # seconds
+CACHE_TIMEOUT = 60 * 60      # seconds
 
 
 class SmileyManager(models.Manager):
 
     def get_smiley_map(self):
+        """
+        Returns a dictionary of 2-tuples, indexed by smiley codes.
+        Element 0 of the tuple is the HTML representation of the smiley,
+        and element 1 is the markdown version.
+        The dictionary is cached.
+        """
         map = cache.get('smiley_map')
         if map:
             return map
 
-        map = dict((s.code, s.html()) for s in self.all())
+        map = dict((s.code, (s.html(), s.markdown())) for s in self.all())
         cache.set('smiley_map', map, CACHE_TIMEOUT)
         return map
 
@@ -48,9 +54,16 @@
         return self.image.url
 
     def html(self):
+        """Returns a HTML img tag representation of the smiley."""
         if self.image:
             return (u'<img src="%s" alt="%s" title="%s" />' %
                     (self.get_absolute_url(), self.title, self.title))
         return u''
     html.allow_tags = True
 
+    def markdown(self):
+        """Returns a markdown representation of the smiley."""
+        if self.image:
+            return (u'![%s](%s "%s")' % 
+                    (self.title, self.get_absolute_url(), self.title))
+        return u''