annotate smiley/models.py @ 757:f36864bc919e

Remove NSSR banner from base template.
author Brian Neal <bgneal@gmail.com>
date Sun, 05 Jan 2014 13:06:53 -0600
parents 0729c73d5761
children 7429c98c8ece
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the smiley application.
gremmie@1 3 """
bgneal@211 4 import re
bgneal@211 5
gremmie@1 6 from django.db import models
bgneal@123 7 from django.core.cache import cache
bgneal@613 8 from django.contrib.sites.models import Site
bgneal@123 9
bgneal@128 10 CACHE_TIMEOUT = 60 * 60 # seconds
gremmie@1 11
gremmie@1 12
gremmie@1 13 class SmileyManager(models.Manager):
gremmie@1 14
gremmie@1 15 def get_smiley_map(self):
bgneal@128 16 """
bgneal@211 17 Returns a dictionary, the keys are smiley codes.
bgneal@211 18 The values are the HTML representations of the keys.
bgneal@128 19 The dictionary is cached.
bgneal@128 20 """
bgneal@123 21 map = cache.get('smiley_map')
bgneal@123 22 if map:
bgneal@123 23 return map
gremmie@1 24
bgneal@211 25 map = dict((s.code, s.html()) for s in self.all())
bgneal@123 26 cache.set('smiley_map', map, CACHE_TIMEOUT)
bgneal@123 27 return map
gremmie@1 28
bgneal@123 29 def get_smilies(self, extra=False):
bgneal@211 30 """
bgneal@211 31 Returns smiley model instances filtered by the extra flag.
bgneal@211 32 """
bgneal@123 33 key = 'smileys' if not extra else 'smileys_extra'
bgneal@123 34 smilies = cache.get(key)
bgneal@123 35 if smilies:
bgneal@123 36 return smilies
bgneal@123 37
bgneal@123 38 smilies = self.filter(is_extra=extra)
bgneal@123 39 cache.set(key, smilies, CACHE_TIMEOUT)
bgneal@123 40 return smilies
gremmie@1 41
bgneal@211 42 def get_smiley_regexes(self):
bgneal@211 43 """
bgneal@211 44 Returns a list of 2-tuples of the form: (regex, repl)
bgneal@211 45 where regex is a regular expression for a smiley and
bgneal@211 46 repl is the replacement image in Markdown format.
bgneal@211 47 """
bgneal@211 48 regexes = cache.get('smiley_regexes')
bgneal@211 49 if regexes:
bgneal@211 50 return regexes
bgneal@211 51
bgneal@211 52 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)),
bgneal@211 53 r"\1%s\2" % s.markdown()) for s in self.all()]
bgneal@211 54 cache.set('smiley_regexes', regexes, CACHE_TIMEOUT)
bgneal@211 55 return regexes
bgneal@211 56
gremmie@1 57
gremmie@1 58 class Smiley(models.Model):
gremmie@1 59 image = models.ImageField(upload_to='smiley/images/')
gremmie@1 60 title = models.CharField(max_length=32)
gremmie@1 61 code = models.CharField(max_length=32)
bgneal@123 62 is_extra = models.BooleanField()
gremmie@1 63
gremmie@1 64 objects = SmileyManager()
gremmie@1 65
gremmie@1 66 class Meta:
gremmie@1 67 verbose_name_plural = 'Smilies'
gremmie@1 68 ordering = ('title', )
gremmie@1 69
gremmie@1 70 def __unicode__(self):
gremmie@1 71 return self.title
gremmie@1 72
gremmie@1 73 def get_absolute_url(self):
gremmie@1 74 return self.image.url
gremmie@1 75
gremmie@1 76 def html(self):
bgneal@128 77 """Returns a HTML img tag representation of the smiley."""
gremmie@1 78 if self.image:
bgneal@613 79 site = Site.objects.get_current()
bgneal@613 80 return (u'<img src="http://%s%s" alt="%s" title="%s" />' %
bgneal@613 81 (site.domain, self.get_absolute_url(), self.title, self.title))
gremmie@1 82 return u''
gremmie@1 83 html.allow_tags = True
gremmie@1 84
bgneal@128 85 def markdown(self):
bgneal@128 86 """Returns a markdown representation of the smiley."""
bgneal@128 87 if self.image:
bgneal@613 88 site = Site.objects.get_current()
bgneal@613 89 return (u'![%s](http://%s%s "%s")' %
bgneal@613 90 (self.title, site.domain, self.get_absolute_url(), self.title))
bgneal@128 91 return u''