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@991: from django.conf import settings bgneal@123: from django.core.cache import cache bgneal@613: from django.contrib.sites.models import Site 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@792: def get_smiley_regexes(self, relative_urls=True): 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@792: bgneal@792: If relative_urls is true, the smiley images will use relative URLs. If bgneal@792: False, absolute URLs will be used. bgneal@792: bgneal@211: """ bgneal@792: key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs' bgneal@792: regexes = cache.get(key) 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@792: r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()] bgneal@792: cache.set(key, 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@929: is_extra = models.BooleanField(default=False) 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@989: return (u'%s' % bgneal@989: (self.get_absolute_url(), self.title, self.title)) gremmie@1: return u'' gremmie@1: html.allow_tags = True gremmie@1: bgneal@792: def markdown(self, relative_urls=True): bgneal@792: """Returns a markdown representation of the smiley. bgneal@792: bgneal@792: If relative_urls is True, relative URLs will be generated. If False, bgneal@792: absolute URLs will be used. bgneal@792: bgneal@792: """ bgneal@128: if self.image: bgneal@792: if relative_urls: bgneal@792: return u'![%s](%s "%s")' % (self.title, self.image.url, bgneal@792: self.title) bgneal@792: else: bgneal@792: site = Site.objects.get_current() bgneal@991: return (u'![%s](%s://%s%s "%s")' % ( bgneal@991: self.title, settings.SITE_SCHEME, site.domain, self.image.url, self.title)) bgneal@128: return u''