diff smiley/models.py @ 792:7429c98c8ece

Issue #71: use relative URLs for smileys on the web and absolute for RSS.
author Brian Neal <bgneal@gmail.com>
date Mon, 26 May 2014 14:59:55 -0500
parents 0729c73d5761
children e14f54f16dbc
line wrap: on
line diff
--- a/smiley/models.py	Fri May 23 21:52:41 2014 -0500
+++ b/smiley/models.py	Mon May 26 14:59:55 2014 -0500
@@ -39,19 +39,24 @@
         cache.set(key, smilies, CACHE_TIMEOUT)
         return smilies
 
-    def get_smiley_regexes(self):
+    def get_smiley_regexes(self, relative_urls=True):
         """
         Returns a list of 2-tuples of the form: (regex, repl)
         where regex is a regular expression for a smiley and
         repl is the replacement image in Markdown format.
+
+        If relative_urls is true, the smiley images will use relative URLs. If
+        False, absolute URLs will be used.
+
         """
-        regexes = cache.get('smiley_regexes')
+        key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs'
+        regexes = cache.get(key)
         if regexes:
             return regexes
 
         regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)),
-            r"\1%s\2" % s.markdown()) for s in self.all()]
-        cache.set('smiley_regexes', regexes, CACHE_TIMEOUT)
+            r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()]
+        cache.set(key, regexes, CACHE_TIMEOUT)
         return regexes
 
 
@@ -82,10 +87,19 @@
         return u''
     html.allow_tags = True
 
-    def markdown(self):
-        """Returns a markdown representation of the smiley."""
+    def markdown(self, relative_urls=True):
+        """Returns a markdown representation of the smiley.
+
+        If relative_urls is True, relative URLs will be generated. If False,
+        absolute URLs will be used.
+
+        """
         if self.image:
-            site = Site.objects.get_current()
-            return (u'![%s](http://%s%s "%s")' % 
-                    (self.title, site.domain, self.get_absolute_url(), self.title))
+            if relative_urls:
+                return u'![%s](%s "%s")' % (self.title, self.image.url,
+                        self.title)
+            else:
+                site = Site.objects.get_current()
+                return (u'![%s](http://%s%s "%s")' %
+                    (self.title, site.domain, self.image.url, self.title))
         return u''