gremmie@1: """ gremmie@1: Models for the smiley application. gremmie@1: """ bgneal@211: import re bgneal@211: gremmie@1: from django.db import models bgneal@123: from django.core.cache import cache bgneal@123: bgneal@128: CACHE_TIMEOUT = 60 * 60 # seconds gremmie@1: gremmie@1: gremmie@1: class SmileyManager(models.Manager): gremmie@1: gremmie@1: def get_smiley_map(self): bgneal@128: """ bgneal@211: Returns a dictionary, the keys are smiley codes. bgneal@211: The values are the HTML representations of the keys. bgneal@128: The dictionary is cached. bgneal@128: """ bgneal@123: map = cache.get('smiley_map') bgneal@123: if map: bgneal@123: return map gremmie@1: bgneal@211: map = dict((s.code, s.html()) for s in self.all()) bgneal@123: cache.set('smiley_map', map, CACHE_TIMEOUT) bgneal@123: return map gremmie@1: bgneal@123: def get_smilies(self, extra=False): bgneal@211: """ bgneal@211: Returns smiley model instances filtered by the extra flag. bgneal@211: """ bgneal@123: key = 'smileys' if not extra else 'smileys_extra' bgneal@123: smilies = cache.get(key) bgneal@123: if smilies: bgneal@123: return smilies bgneal@123: bgneal@123: smilies = self.filter(is_extra=extra) bgneal@123: cache.set(key, smilies, CACHE_TIMEOUT) bgneal@123: return smilies gremmie@1: bgneal@211: def get_smiley_regexes(self): bgneal@211: """ bgneal@211: Returns a list of 2-tuples of the form: (regex, repl) bgneal@211: where regex is a regular expression for a smiley and bgneal@211: repl is the replacement image in Markdown format. bgneal@211: """ bgneal@211: regexes = cache.get('smiley_regexes') bgneal@211: if regexes: bgneal@211: return regexes bgneal@211: bgneal@211: regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)), bgneal@211: r"\1%s\2" % s.markdown()) for s in self.all()] bgneal@211: cache.set('smiley_regexes', regexes, CACHE_TIMEOUT) bgneal@211: return regexes bgneal@211: gremmie@1: gremmie@1: class Smiley(models.Model): gremmie@1: image = models.ImageField(upload_to='smiley/images/') gremmie@1: title = models.CharField(max_length=32) gremmie@1: code = models.CharField(max_length=32) bgneal@123: is_extra = models.BooleanField() gremmie@1: gremmie@1: objects = SmileyManager() gremmie@1: gremmie@1: class Meta: gremmie@1: verbose_name_plural = 'Smilies' gremmie@1: ordering = ('title', ) gremmie@1: gremmie@1: def __unicode__(self): gremmie@1: return self.title gremmie@1: gremmie@1: def get_absolute_url(self): gremmie@1: return self.image.url gremmie@1: gremmie@1: def html(self): bgneal@128: """Returns a HTML img tag representation of the smiley.""" gremmie@1: if self.image: bgneal@123: return (u'%s' % bgneal@123: (self.get_absolute_url(), self.title, self.title)) gremmie@1: return u'' gremmie@1: html.allow_tags = True gremmie@1: bgneal@128: def markdown(self): bgneal@128: """Returns a markdown representation of the smiley.""" bgneal@128: if self.image: bgneal@128: return (u'![%s](%s "%s")' % bgneal@128: (self.title, self.get_absolute_url(), self.title)) bgneal@128: return u''