annotate gpp/smiley/models.py @ 335:46d7f69b61db

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