annotate gpp/smiley/models.py @ 6:b6263ac72052

Use DRY principle to manage third party javascript libraries. Created script_tags template tags to generate the correct link and script tags for 3rd party libraries. The settings.py file is the only place where the full path name is specified.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 Apr 2009 22:50:56 +0000
parents dbd703f7d63a
children 8eb9b8d014a2
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the smiley application.
gremmie@1 3 """
gremmie@1 4 from django.db import models
gremmie@1 5
gremmie@1 6
gremmie@1 7 class SmileyManager(models.Manager):
gremmie@1 8 smiley_map = None
gremmie@1 9 smilies = None
gremmie@1 10
gremmie@1 11 def get_smiley_map(self):
gremmie@1 12 if self.smiley_map is None:
gremmie@1 13 smilies = self.all()
gremmie@1 14 self.smiley_map = {}
gremmie@1 15 for s in smilies:
gremmie@1 16 self.smiley_map[s.code] = s.html()
gremmie@1 17 return self.smiley_map
gremmie@1 18
gremmie@1 19 def get_smilies(self):
gremmie@1 20 if self.smilies is None:
gremmie@1 21 self.smilies = self.all()
gremmie@1 22 return self.smilies
gremmie@1 23
gremmie@1 24 def clear_cache(self):
gremmie@1 25 self.smiley_map = None
gremmie@1 26 self.smileis = None
gremmie@1 27
gremmie@1 28
gremmie@1 29 class Smiley(models.Model):
gremmie@1 30 image = models.ImageField(upload_to='smiley/images/')
gremmie@1 31 title = models.CharField(max_length=32)
gremmie@1 32 code = models.CharField(max_length=32)
gremmie@1 33
gremmie@1 34 objects = SmileyManager()
gremmie@1 35
gremmie@1 36 class Meta:
gremmie@1 37 verbose_name_plural = 'Smilies'
gremmie@1 38 ordering = ('title', )
gremmie@1 39
gremmie@1 40 def __unicode__(self):
gremmie@1 41 return self.title
gremmie@1 42
gremmie@1 43 def get_absolute_url(self):
gremmie@1 44 return self.image.url
gremmie@1 45
gremmie@1 46 def html(self):
gremmie@1 47 if self.image:
gremmie@1 48 return u'<img src="%s" alt="%s" title="%s" />' % \
gremmie@1 49 (self.get_absolute_url(), self.title, self.title)
gremmie@1 50 return u''
gremmie@1 51 html.allow_tags = True
gremmie@1 52