Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
127:2d299909e074 | 128:48621ba5c385 |
---|---|
2 Models for the smiley application. | 2 Models for the smiley application. |
3 """ | 3 """ |
4 from django.db import models | 4 from django.db import models |
5 from django.core.cache import cache | 5 from django.core.cache import cache |
6 | 6 |
7 CACHE_TIMEOUT = 60 * 5 # seconds | 7 CACHE_TIMEOUT = 60 * 60 # seconds |
8 | 8 |
9 | 9 |
10 class SmileyManager(models.Manager): | 10 class SmileyManager(models.Manager): |
11 | 11 |
12 def get_smiley_map(self): | 12 def get_smiley_map(self): |
13 """ | |
14 Returns a dictionary of 2-tuples, indexed by smiley codes. | |
15 Element 0 of the tuple is the HTML representation of the smiley, | |
16 and element 1 is the markdown version. | |
17 The dictionary is cached. | |
18 """ | |
13 map = cache.get('smiley_map') | 19 map = cache.get('smiley_map') |
14 if map: | 20 if map: |
15 return map | 21 return map |
16 | 22 |
17 map = dict((s.code, s.html()) for s in self.all()) | 23 map = dict((s.code, (s.html(), s.markdown())) for s in self.all()) |
18 cache.set('smiley_map', map, CACHE_TIMEOUT) | 24 cache.set('smiley_map', map, CACHE_TIMEOUT) |
19 return map | 25 return map |
20 | 26 |
21 def get_smilies(self, extra=False): | 27 def get_smilies(self, extra=False): |
22 key = 'smileys' if not extra else 'smileys_extra' | 28 key = 'smileys' if not extra else 'smileys_extra' |
46 | 52 |
47 def get_absolute_url(self): | 53 def get_absolute_url(self): |
48 return self.image.url | 54 return self.image.url |
49 | 55 |
50 def html(self): | 56 def html(self): |
57 """Returns a HTML img tag representation of the smiley.""" | |
51 if self.image: | 58 if self.image: |
52 return (u'<img src="%s" alt="%s" title="%s" />' % | 59 return (u'<img src="%s" alt="%s" title="%s" />' % |
53 (self.get_absolute_url(), self.title, self.title)) | 60 (self.get_absolute_url(), self.title, self.title)) |
54 return u'' | 61 return u'' |
55 html.allow_tags = True | 62 html.allow_tags = True |
56 | 63 |
64 def markdown(self): | |
65 """Returns a markdown representation of the smiley.""" | |
66 if self.image: | |
67 return (u'![%s](%s "%s")' % | |
68 (self.title, self.get_absolute_url(), self.title)) | |
69 return u'' |