view gpp/smiley/models.py @ 467:b910cc1460c8

Add the ability to conditionally add model instances to the search index on update. This is not perfect, as some instances should be deleted from the index if they are updated such that they should not be in the index anymore. Will think about and address that later.
author Brian Neal <bgneal@gmail.com>
date Sun, 24 Jul 2011 18:12:20 +0000
parents 3a626c48e9ae
children
line wrap: on
line source
"""
Models for the smiley application.
"""
import re

from django.db import models
from django.core.cache import cache

CACHE_TIMEOUT = 60 * 60      # seconds


class SmileyManager(models.Manager):

    def get_smiley_map(self):
        """
        Returns a dictionary, the keys are smiley codes.
        The values are the HTML representations of the keys.
        The dictionary is cached.
        """
        map = cache.get('smiley_map')
        if map:
            return map

        map = dict((s.code, s.html()) for s in self.all())
        cache.set('smiley_map', map, CACHE_TIMEOUT)
        return map

    def get_smilies(self, extra=False):
        """
        Returns smiley model instances filtered by the extra flag.
        """
        key = 'smileys' if not extra else 'smileys_extra'
        smilies = cache.get(key)
        if smilies:
            return smilies

        smilies = self.filter(is_extra=extra)
        cache.set(key, smilies, CACHE_TIMEOUT)
        return smilies

    def get_smiley_regexes(self):
        """
        Returns a list of 2-tuples of the form: (regex, repl)
        where regex is a regular expression for a smiley and
        repl is the replacement image in Markdown format.
        """
        regexes = cache.get('smiley_regexes')
        if regexes:
            return regexes

        regexes = [(re.compile(r"(^|\s|(?<=\s))%s(\s|$)" % re.escape(s.code)),
            r"\1%s\2" % s.markdown()) for s in self.all()]
        cache.set('smiley_regexes', regexes, CACHE_TIMEOUT)
        return regexes


class Smiley(models.Model):
    image = models.ImageField(upload_to='smiley/images/')
    title = models.CharField(max_length=32)
    code = models.CharField(max_length=32)
    is_extra = models.BooleanField()

    objects = SmileyManager()

    class Meta:
        verbose_name_plural = 'Smilies'
        ordering = ('title', )

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return self.image.url

    def html(self):
        """Returns a HTML img tag representation of the smiley."""
        if self.image:
            return (u'<img src="%s" alt="%s" title="%s" />' %
                    (self.get_absolute_url(), self.title, self.title))
        return u''
    html.allow_tags = True

    def markdown(self):
        """Returns a markdown representation of the smiley."""
        if self.image:
            return (u'![%s](%s "%s")' % 
                    (self.title, self.get_absolute_url(), self.title))
        return u''