annotate gpp/smiley/models.py @ 140:91a01b8b5885

Comments: in order to show a green board concept while looking at the comments in the admin, added a 'not removed' boolean function.
author Brian Neal <bgneal@gmail.com>
date Mon, 30 Nov 2009 03:20:24 +0000
parents 48621ba5c385
children 3a626c48e9ae
rev   line source
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@128 7 CACHE_TIMEOUT = 60 * 60 # 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@128 13 """
bgneal@128 14 Returns a dictionary of 2-tuples, indexed by smiley codes.
bgneal@128 15 Element 0 of the tuple is the HTML representation of the smiley,
bgneal@128 16 and element 1 is the markdown version.
bgneal@128 17 The dictionary is cached.
bgneal@128 18 """
bgneal@123 19 map = cache.get('smiley_map')
bgneal@123 20 if map:
bgneal@123 21 return map
gremmie@1 22
bgneal@128 23 map = dict((s.code, (s.html(), s.markdown())) for s in self.all())
bgneal@123 24 cache.set('smiley_map', map, CACHE_TIMEOUT)
bgneal@123 25 return map
gremmie@1 26
bgneal@123 27 def get_smilies(self, extra=False):
bgneal@123 28 key = 'smileys' if not extra else 'smileys_extra'
bgneal@123 29 smilies = cache.get(key)
bgneal@123 30 if smilies:
bgneal@123 31 return smilies
bgneal@123 32
bgneal@123 33 smilies = self.filter(is_extra=extra)
bgneal@123 34 cache.set(key, smilies, CACHE_TIMEOUT)
bgneal@123 35 return smilies
gremmie@1 36
gremmie@1 37
gremmie@1 38 class Smiley(models.Model):
gremmie@1 39 image = models.ImageField(upload_to='smiley/images/')
gremmie@1 40 title = models.CharField(max_length=32)
gremmie@1 41 code = models.CharField(max_length=32)
bgneal@123 42 is_extra = models.BooleanField()
gremmie@1 43
gremmie@1 44 objects = SmileyManager()
gremmie@1 45
gremmie@1 46 class Meta:
gremmie@1 47 verbose_name_plural = 'Smilies'
gremmie@1 48 ordering = ('title', )
gremmie@1 49
gremmie@1 50 def __unicode__(self):
gremmie@1 51 return self.title
gremmie@1 52
gremmie@1 53 def get_absolute_url(self):
gremmie@1 54 return self.image.url
gremmie@1 55
gremmie@1 56 def html(self):
bgneal@128 57 """Returns a HTML img tag representation of the smiley."""
gremmie@1 58 if self.image:
bgneal@123 59 return (u'<img src="%s" alt="%s" title="%s" />' %
bgneal@123 60 (self.get_absolute_url(), self.title, self.title))
gremmie@1 61 return u''
gremmie@1 62 html.allow_tags = True
gremmie@1 63
bgneal@128 64 def markdown(self):
bgneal@128 65 """Returns a markdown representation of the smiley."""
bgneal@128 66 if self.image:
bgneal@128 67 return (u'![%s](%s "%s")' %
bgneal@128 68 (self.title, self.get_absolute_url(), self.title))
bgneal@128 69 return u''