annotate smiley/models.py @ 867:21946dc3662d

Add can_comment_on() check for contests.
author Brian Neal <bgneal@gmail.com>
date Mon, 15 Dec 2014 18:48:45 -0600
parents 7429c98c8ece
children e14f54f16dbc
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@792 42 def get_smiley_regexes(self, relative_urls=True):
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@792 47
bgneal@792 48 If relative_urls is true, the smiley images will use relative URLs. If
bgneal@792 49 False, absolute URLs will be used.
bgneal@792 50
bgneal@211 51 """
bgneal@792 52 key = 'smiley_regexes_rel' if relative_urls else 'smiley_regexes_abs'
bgneal@792 53 regexes = cache.get(key)
bgneal@211 54 if regexes:
bgneal@211 55 return regexes
bgneal@211 56
bgneal@211 57 regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)),
bgneal@792 58 r"\1%s\2" % s.markdown(relative_urls=relative_urls)) for s in self.all()]
bgneal@792 59 cache.set(key, regexes, CACHE_TIMEOUT)
bgneal@211 60 return regexes
bgneal@211 61
gremmie@1 62
gremmie@1 63 class Smiley(models.Model):
gremmie@1 64 image = models.ImageField(upload_to='smiley/images/')
gremmie@1 65 title = models.CharField(max_length=32)
gremmie@1 66 code = models.CharField(max_length=32)
bgneal@123 67 is_extra = models.BooleanField()
gremmie@1 68
gremmie@1 69 objects = SmileyManager()
gremmie@1 70
gremmie@1 71 class Meta:
gremmie@1 72 verbose_name_plural = 'Smilies'
gremmie@1 73 ordering = ('title', )
gremmie@1 74
gremmie@1 75 def __unicode__(self):
gremmie@1 76 return self.title
gremmie@1 77
gremmie@1 78 def get_absolute_url(self):
gremmie@1 79 return self.image.url
gremmie@1 80
gremmie@1 81 def html(self):
bgneal@128 82 """Returns a HTML img tag representation of the smiley."""
gremmie@1 83 if self.image:
bgneal@613 84 site = Site.objects.get_current()
bgneal@613 85 return (u'<img src="http://%s%s" alt="%s" title="%s" />' %
bgneal@613 86 (site.domain, 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@792 103 return (u'![%s](http://%s%s "%s")' %
bgneal@792 104 (self.title, site.domain, self.image.url, self.title))
bgneal@128 105 return u''