Mercurial > public > sg101
comparison gpp/smiley/models.py @ 211:3a626c48e9ae
Fix #81: could not get paragraphs in Markdown due to the interaction between smiley and Markdown. Refactored the smilify code to use a series of regular expressions over the text when working with markdown.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 08 May 2010 23:44:59 +0000 |
parents | 48621ba5c385 |
children |
comparison
equal
deleted
inserted
replaced
210:77b606e0ad5f | 211:3a626c48e9ae |
---|---|
1 """ | 1 """ |
2 Models for the smiley application. | 2 Models for the smiley application. |
3 """ | 3 """ |
4 import re | |
5 | |
4 from django.db import models | 6 from django.db import models |
5 from django.core.cache import cache | 7 from django.core.cache import cache |
6 | 8 |
7 CACHE_TIMEOUT = 60 * 60 # seconds | 9 CACHE_TIMEOUT = 60 * 60 # seconds |
8 | 10 |
9 | 11 |
10 class SmileyManager(models.Manager): | 12 class SmileyManager(models.Manager): |
11 | 13 |
12 def get_smiley_map(self): | 14 def get_smiley_map(self): |
13 """ | 15 """ |
14 Returns a dictionary of 2-tuples, indexed by smiley codes. | 16 Returns a dictionary, the keys are smiley codes. |
15 Element 0 of the tuple is the HTML representation of the smiley, | 17 The values are the HTML representations of the keys. |
16 and element 1 is the markdown version. | |
17 The dictionary is cached. | 18 The dictionary is cached. |
18 """ | 19 """ |
19 map = cache.get('smiley_map') | 20 map = cache.get('smiley_map') |
20 if map: | 21 if map: |
21 return map | 22 return map |
22 | 23 |
23 map = dict((s.code, (s.html(), s.markdown())) for s in self.all()) | 24 map = dict((s.code, s.html()) for s in self.all()) |
24 cache.set('smiley_map', map, CACHE_TIMEOUT) | 25 cache.set('smiley_map', map, CACHE_TIMEOUT) |
25 return map | 26 return map |
26 | 27 |
27 def get_smilies(self, extra=False): | 28 def get_smilies(self, extra=False): |
29 """ | |
30 Returns smiley model instances filtered by the extra flag. | |
31 """ | |
28 key = 'smileys' if not extra else 'smileys_extra' | 32 key = 'smileys' if not extra else 'smileys_extra' |
29 smilies = cache.get(key) | 33 smilies = cache.get(key) |
30 if smilies: | 34 if smilies: |
31 return smilies | 35 return smilies |
32 | 36 |
33 smilies = self.filter(is_extra=extra) | 37 smilies = self.filter(is_extra=extra) |
34 cache.set(key, smilies, CACHE_TIMEOUT) | 38 cache.set(key, smilies, CACHE_TIMEOUT) |
35 return smilies | 39 return smilies |
40 | |
41 def get_smiley_regexes(self): | |
42 """ | |
43 Returns a list of 2-tuples of the form: (regex, repl) | |
44 where regex is a regular expression for a smiley and | |
45 repl is the replacement image in Markdown format. | |
46 """ | |
47 regexes = cache.get('smiley_regexes') | |
48 if regexes: | |
49 return regexes | |
50 | |
51 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)), | |
52 r"\1%s\2" % s.markdown()) for s in self.all()] | |
53 cache.set('smiley_regexes', regexes, CACHE_TIMEOUT) | |
54 return regexes | |
36 | 55 |
37 | 56 |
38 class Smiley(models.Model): | 57 class Smiley(models.Model): |
39 image = models.ImageField(upload_to='smiley/images/') | 58 image = models.ImageField(upload_to='smiley/images/') |
40 title = models.CharField(max_length=32) | 59 title = models.CharField(max_length=32) |