view gpp/smiley/models.py @ 197:2baadae33f2e

Got autocomplete working for the member search. Updated django and ran into a bug where url tags with comma separated kwargs starting consuming tons of CPU throughput. The work-around is to cut over to using spaces between arguments. This is now allowed to be consistent with other tags. Did some query optimization for the news app.
author Brian Neal <bgneal@gmail.com>
date Sat, 10 Apr 2010 04:32:24 +0000 (2010-04-10)
parents 48621ba5c385
children 3a626c48e9ae
line wrap: on
line source
"""
Models for the smiley application.
"""
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 of 2-tuples, indexed by smiley codes.
        Element 0 of the tuple is the HTML representation of the smiley,
        and element 1 is the markdown version.
        The dictionary is cached.
        """
        map = cache.get('smiley_map')
        if map:
            return map

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

    def get_smilies(self, extra=False):
        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


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''