annotate smiley/models.py @ 1103:babb1f44dda5

Use V3 post box for poll comments.
author Brian Neal <bgneal@gmail.com>
date Wed, 06 Jul 2016 20:27:34 -0500
parents 4aadaf3bc234
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@991 7 from django.conf import settings
bgneal@123 8 from django.core.cache import cache
bgneal@613 9 from django.contrib.sites.models import Site
bgneal@123 10
bgneal@128 11 CACHE_TIMEOUT = 60 * 60 # seconds
gremmie@1 12
gremmie@1 13
gremmie@1 14 class SmileyManager(models.Manager):
gremmie@1 15
gremmie@1 16 def get_smiley_map(self):
bgneal@128 17 """
bgneal@211 18 Returns a dictionary, the keys are smiley codes.
bgneal@211 19 The values are the HTML representations of the keys.
bgneal@128 20 The dictionary is cached.
bgneal@128 21 """
bgneal@123 22 map = cache.get('smiley_map')
bgneal@123 23 if map:
bgneal@123 24 return map
gremmie@1 25
bgneal@211 26 map = dict((s.code, s.html()) for s in self.all())
bgneal@123 27 cache.set('smiley_map', map, CACHE_TIMEOUT)
bgneal@123 28 return map
gremmie@1 29
bgneal@123 30 def get_smilies(self, extra=False):
bgneal@211 31 """
bgneal@211 32 Returns smiley model instances filtered by the extra flag.
bgneal@211 33 """
bgneal@123 34 key = 'smileys' if not extra else 'smileys_extra'
bgneal@123 35 smilies = cache.get(key)
bgneal@123 36 if smilies:
bgneal@123 37 return smilies
bgneal@123 38
bgneal@123 39 smilies = self.filter(is_extra=extra)
bgneal@123 40 cache.set(key, smilies, CACHE_TIMEOUT)
bgneal@123 41 return smilies
gremmie@1 42
bgneal@792 43 def get_smiley_regexes(self, relative_urls=True):
bgneal@211 44 """
bgneal@211 45 Returns a list of 2-tuples of the form: (regex, repl)
bgneal@211 46 where regex is a regular expression for a smiley and
bgneal@211 47 repl is the replacement image in Markdown format.
bgneal@792 48
bgneal@792 49 If relative_urls is true, the smiley images will use relative URLs. If
bgneal@792 50 False, absolute URLs will be used.
bgneal@792 51
bgneal@211 52 """
bgneal@792 53 key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs'
bgneal@792 54 regexes = cache.get(key)
bgneal@211 55 if regexes:
bgneal@211 56 return regexes
bgneal@211 57
bgneal@211 58 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)),
bgneal@792 59 r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()]
bgneal@792 60 cache.set(key, regexes, CACHE_TIMEOUT)
bgneal@211 61 return regexes
bgneal@211 62
gremmie@1 63
gremmie@1 64 class Smiley(models.Model):
gremmie@1 65 image = models.ImageField(upload_to='smiley/images/')
gremmie@1 66 title = models.CharField(max_length=32)
gremmie@1 67 code = models.CharField(max_length=32)
bgneal@929 68 is_extra = models.BooleanField(default=False)
gremmie@1 69
gremmie@1 70 objects = SmileyManager()
gremmie@1 71
gremmie@1 72 class Meta:
gremmie@1 73 verbose_name_plural = 'Smilies'
gremmie@1 74 ordering = ('title', )
gremmie@1 75
gremmie@1 76 def __unicode__(self):
gremmie@1 77 return self.title
gremmie@1 78
gremmie@1 79 def get_absolute_url(self):
gremmie@1 80 return self.image.url
gremmie@1 81
gremmie@1 82 def html(self):
bgneal@128 83 """Returns a HTML img tag representation of the smiley."""
gremmie@1 84 if self.image:
bgneal@989 85 return (u'<img src="%s" alt="%s" title="%s" />' %
bgneal@989 86 (self.get_absolute_url(), self.title, self.title))
gremmie@1 87 return u''
gremmie@1 88 html.allow_tags = True
gremmie@1 89
bgneal@792 90 def markdown(self, relative_urls=True):
bgneal@792 91 """Returns a markdown representation of the smiley.
bgneal@792 92
bgneal@792 93 If relative_urls is True, relative URLs will be generated. If False,
bgneal@792 94 absolute URLs will be used.
bgneal@792 95
bgneal@792 96 """
bgneal@128 97 if self.image:
bgneal@792 98 if relative_urls:
bgneal@792 99 return u'![%s](%s "%s")' % (self.title, self.image.url,
bgneal@792 100 self.title)
bgneal@792 101 else:
bgneal@792 102 site = Site.objects.get_current()
bgneal@991 103 return (u'![%s](%s://%s%s "%s")' % (
bgneal@991 104 self.title, settings.SITE_SCHEME, site.domain, self.image.url, self.title))
bgneal@128 105 return u''