Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
791:0ca691cccf8d | 792:7429c98c8ece |
---|---|
37 | 37 |
38 smilies = self.filter(is_extra=extra) | 38 smilies = self.filter(is_extra=extra) |
39 cache.set(key, smilies, CACHE_TIMEOUT) | 39 cache.set(key, smilies, CACHE_TIMEOUT) |
40 return smilies | 40 return smilies |
41 | 41 |
42 def get_smiley_regexes(self): | 42 def get_smiley_regexes(self, relative_urls=True): |
43 """ | 43 """ |
44 Returns a list of 2-tuples of the form: (regex, repl) | 44 Returns a list of 2-tuples of the form: (regex, repl) |
45 where regex is a regular expression for a smiley and | 45 where regex is a regular expression for a smiley and |
46 repl is the replacement image in Markdown format. | 46 repl is the replacement image in Markdown format. |
47 | |
48 If relative_urls is true, the smiley images will use relative URLs. If | |
49 False, absolute URLs will be used. | |
50 | |
47 """ | 51 """ |
48 regexes = cache.get('smiley_regexes') | 52 key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs' |
53 regexes = cache.get(key) | |
49 if regexes: | 54 if regexes: |
50 return regexes | 55 return regexes |
51 | 56 |
52 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)), | 57 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)), |
53 r"\1%s\2" % s.markdown()) for s in self.all()] | 58 r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()] |
54 cache.set('smiley_regexes', regexes, CACHE_TIMEOUT) | 59 cache.set(key, regexes, CACHE_TIMEOUT) |
55 return regexes | 60 return regexes |
56 | 61 |
57 | 62 |
58 class Smiley(models.Model): | 63 class Smiley(models.Model): |
59 image = models.ImageField(upload_to='smiley/images/') | 64 image = models.ImageField(upload_to='smiley/images/') |
80 return (u'<img src="http://%s%s" alt="%s" title="%s" />' % | 85 return (u'<img src="http://%s%s" alt="%s" title="%s" />' % |
81 (site.domain, self.get_absolute_url(), self.title, self.title)) | 86 (site.domain, self.get_absolute_url(), self.title, self.title)) |
82 return u'' | 87 return u'' |
83 html.allow_tags = True | 88 html.allow_tags = True |
84 | 89 |
85 def markdown(self): | 90 def markdown(self, relative_urls=True): |
86 """Returns a markdown representation of the smiley.""" | 91 """Returns a markdown representation of the smiley. |
92 | |
93 If relative_urls is True, relative URLs will be generated. If False, | |
94 absolute URLs will be used. | |
95 | |
96 """ | |
87 if self.image: | 97 if self.image: |
88 site = Site.objects.get_current() | 98 if relative_urls: |
89 return (u'![%s](http://%s%s "%s")' % | 99 return u'![%s](%s "%s")' % (self.title, self.image.url, |
90 (self.title, site.domain, self.get_absolute_url(), self.title)) | 100 self.title) |
101 else: | |
102 site = Site.objects.get_current() | |
103 return (u'![%s](http://%s%s "%s")' % | |
104 (self.title, site.domain, self.image.url, self.title)) | |
91 return u'' | 105 return u'' |