gremmie@1
|
1 """
|
gremmie@1
|
2 Models for the smiley application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4 from django.db import models
|
bgneal@123
|
5 from django.core.cache import cache
|
bgneal@123
|
6
|
bgneal@123
|
7 CACHE_TIMEOUT = 60 * 5 # seconds
|
gremmie@1
|
8
|
gremmie@1
|
9
|
gremmie@1
|
10 class SmileyManager(models.Manager):
|
gremmie@1
|
11
|
gremmie@1
|
12 def get_smiley_map(self):
|
bgneal@123
|
13 map = cache.get('smiley_map')
|
bgneal@123
|
14 if map:
|
bgneal@123
|
15 return map
|
gremmie@1
|
16
|
bgneal@123
|
17 map = dict((s.code, s.html()) for s in self.all())
|
bgneal@123
|
18 cache.set('smiley_map', map, CACHE_TIMEOUT)
|
bgneal@123
|
19 return map
|
gremmie@1
|
20
|
bgneal@123
|
21 def get_smilies(self, extra=False):
|
bgneal@123
|
22 key = 'smileys' if not extra else 'smileys_extra'
|
bgneal@123
|
23 smilies = cache.get(key)
|
bgneal@123
|
24 if smilies:
|
bgneal@123
|
25 return smilies
|
bgneal@123
|
26
|
bgneal@123
|
27 smilies = self.filter(is_extra=extra)
|
bgneal@123
|
28 cache.set(key, smilies, CACHE_TIMEOUT)
|
bgneal@123
|
29 return smilies
|
gremmie@1
|
30
|
gremmie@1
|
31
|
gremmie@1
|
32 class Smiley(models.Model):
|
gremmie@1
|
33 image = models.ImageField(upload_to='smiley/images/')
|
gremmie@1
|
34 title = models.CharField(max_length=32)
|
gremmie@1
|
35 code = models.CharField(max_length=32)
|
bgneal@123
|
36 is_extra = models.BooleanField()
|
gremmie@1
|
37
|
gremmie@1
|
38 objects = SmileyManager()
|
gremmie@1
|
39
|
gremmie@1
|
40 class Meta:
|
gremmie@1
|
41 verbose_name_plural = 'Smilies'
|
gremmie@1
|
42 ordering = ('title', )
|
gremmie@1
|
43
|
gremmie@1
|
44 def __unicode__(self):
|
gremmie@1
|
45 return self.title
|
gremmie@1
|
46
|
gremmie@1
|
47 def get_absolute_url(self):
|
gremmie@1
|
48 return self.image.url
|
gremmie@1
|
49
|
gremmie@1
|
50 def html(self):
|
gremmie@1
|
51 if self.image:
|
bgneal@123
|
52 return (u'<img src="%s" alt="%s" title="%s" />' %
|
bgneal@123
|
53 (self.get_absolute_url(), self.title, self.title))
|
gremmie@1
|
54 return u''
|
gremmie@1
|
55 html.allow_tags = True
|
gremmie@1
|
56
|