comparison gpp/smiley/models.py @ 123:3ae999b0c53b

Forums: added a jquery ui dialog of extra smileys.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Nov 2009 21:15:31 +0000
parents 8eb9b8d014a2
children 48621ba5c385
comparison
equal deleted inserted replaced
122:b28d30848c57 123:3ae999b0c53b
1 """ 1 """
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
6
7 CACHE_TIMEOUT = 60 * 5 # seconds
5 8
6 9
7 class SmileyManager(models.Manager): 10 class SmileyManager(models.Manager):
8 smiley_map = None
9 smilies = None
10 11
11 def get_smiley_map(self): 12 def get_smiley_map(self):
12 if self.smiley_map is None: 13 map = cache.get('smiley_map')
13 smilies = self.all() 14 if map:
14 self.smiley_map = {} 15 return map
15 for s in smilies:
16 self.smiley_map[s.code] = s.html()
17 return self.smiley_map
18 16
19 def get_smilies(self): 17 map = dict((s.code, s.html()) for s in self.all())
20 if self.smilies is None: 18 cache.set('smiley_map', map, CACHE_TIMEOUT)
21 self.smilies = self.all() 19 return map
22 return self.smilies
23 20
24 def clear_cache(self): 21 def get_smilies(self, extra=False):
25 self.smiley_map = None 22 key = 'smileys' if not extra else 'smileys_extra'
26 self.smilies = None 23 smilies = cache.get(key)
24 if smilies:
25 return smilies
26
27 smilies = self.filter(is_extra=extra)
28 cache.set(key, smilies, CACHE_TIMEOUT)
29 return smilies
27 30
28 31
29 class Smiley(models.Model): 32 class Smiley(models.Model):
30 image = models.ImageField(upload_to='smiley/images/') 33 image = models.ImageField(upload_to='smiley/images/')
31 title = models.CharField(max_length=32) 34 title = models.CharField(max_length=32)
32 code = models.CharField(max_length=32) 35 code = models.CharField(max_length=32)
36 is_extra = models.BooleanField()
33 37
34 objects = SmileyManager() 38 objects = SmileyManager()
35 39
36 class Meta: 40 class Meta:
37 verbose_name_plural = 'Smilies' 41 verbose_name_plural = 'Smilies'
43 def get_absolute_url(self): 47 def get_absolute_url(self):
44 return self.image.url 48 return self.image.url
45 49
46 def html(self): 50 def html(self):
47 if self.image: 51 if self.image:
48 return u'<img src="%s" alt="%s" title="%s" />' % \ 52 return (u'<img src="%s" alt="%s" title="%s" />' %
49 (self.get_absolute_url(), self.title, self.title) 53 (self.get_absolute_url(), self.title, self.title))
50 return u'' 54 return u''
51 html.allow_tags = True 55 html.allow_tags = True
52 56