annotate gpp/potd/models.py @ 518:5171a5e9353b

For #194, add a celery task for purging deleted private messages.
author Brian Neal <bgneal@gmail.com>
date Fri, 16 Dec 2011 01:17:35 +0000
parents ae89ba801e8b
children ff67946fd4b0
rev   line source
gremmie@1 1 """
gremmie@1 2 Models for the Photo Of The Day (POTD) application.
bgneal@515 3
gremmie@1 4 """
gremmie@1 5 import os
gremmie@1 6 from PIL import ImageFile
gremmie@1 7 from PIL import Image
gremmie@1 8 try:
gremmie@1 9 from cStringIO import StringIO
gremmie@1 10 except:
gremmie@1 11 from StringIO import StringIO
gremmie@1 12
gremmie@1 13 from django.db import models
gremmie@1 14 from django.contrib.auth.models import User
gremmie@1 15 from django.core.files.base import ContentFile
gremmie@1 16
gremmie@1 17 POTD_THUMB_WIDTH = 120
gremmie@1 18
gremmie@1 19 def scale_image(image):
gremmie@1 20 (w, h) = image.size
gremmie@1 21 if w <= POTD_THUMB_WIDTH:
gremmie@1 22 return image
gremmie@1 23 scale_factor = float(POTD_THUMB_WIDTH) / w
gremmie@1 24 new_height = int(scale_factor * h)
gremmie@1 25 return image.resize((POTD_THUMB_WIDTH, new_height), Image.ANTIALIAS)
gremmie@1 26
gremmie@1 27
gremmie@1 28 class Photo(models.Model):
gremmie@1 29 """Model to represent a POTD"""
gremmie@1 30 photo = models.ImageField(upload_to='potd/%Y/%m/%d')
gremmie@1 31 thumb = models.ImageField(upload_to='potd/%Y/%m/%d/thumbs', blank=True, null=True)
gremmie@1 32 caption = models.CharField(max_length=128)
gremmie@1 33 description = models.TextField()
gremmie@1 34 user = models.ForeignKey(User)
gremmie@1 35 date_added = models.DateField(auto_now_add=True)
gremmie@1 36 potd_count = models.IntegerField(default=0)
gremmie@1 37
bgneal@14 38 class Meta:
bgneal@14 39 ordering = ('-date_added', '-caption')
bgneal@14 40
gremmie@1 41 def __unicode__(self):
gremmie@1 42 return u'%s (%s)' % (self.caption, self.pk)
gremmie@1 43
bgneal@14 44 @models.permalink
bgneal@14 45 def get_absolute_url(self):
bgneal@14 46 return ('potd-archive', [str(self.id)])
gremmie@1 47
bgneal@182 48 def save(self, *args, **kwargs):
bgneal@330 49 if not self.pk:
bgneal@330 50 self.generate_thumb()
gremmie@1 51
bgneal@330 52 super(Photo, self).save(*args, **kwargs)
bgneal@330 53 Sequence.objects.insert_photo(self.pk)
bgneal@330 54
bgneal@330 55 def delete(self):
bgneal@330 56 Sequence.objects.remove_photo(self.pk)
bgneal@330 57 super(Photo, self).delete()
bgneal@330 58
bgneal@330 59 def can_comment_on(self):
bgneal@330 60 return Current.objects.get_current_id() == self.id
bgneal@330 61
bgneal@330 62 def generate_thumb(self):
gremmie@1 63 if self.thumb:
gremmie@1 64 self.thumb.delete(save=False)
gremmie@1 65
gremmie@1 66 parser = ImageFile.Parser()
gremmie@1 67 for chunk in self.photo.chunks():
gremmie@1 68 parser.feed(chunk)
gremmie@1 69 image = parser.close()
gremmie@1 70 format = image.format
gremmie@1 71 image = scale_image(image)
gremmie@1 72 s = StringIO()
gremmie@1 73 image.save(s, format)
gremmie@1 74 thumb_name = os.path.basename(self.photo.path)
gremmie@1 75 self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False)
gremmie@1 76
bgneal@491 77 def ogp_tags(self):
bgneal@491 78 """
bgneal@491 79 Returns a dict of Open Graph Protocol meta tags.
bgneal@491 80
bgneal@491 81 """
bgneal@491 82 desc = "Photo of the day: %s." % self.caption
bgneal@491 83 return {
bgneal@491 84 'og:title': self.caption,
bgneal@491 85 'og:type': 'article',
bgneal@491 86 'og:url': self.get_absolute_url(),
bgneal@491 87 'og:image': self.photo.url,
bgneal@491 88 'og:description': desc,
bgneal@491 89 }
bgneal@491 90
gremmie@1 91
gremmie@1 92 class CurrentManager(models.Manager):
bgneal@515 93 """
bgneal@515 94 Manager for the Current model.
bgneal@515 95
bgneal@515 96 """
gremmie@1 97 def get_current_photo(self):
bgneal@515 98 """
bgneal@515 99 Retrieves the current photo object from the current record.
bgneal@515 100
bgneal@515 101 """
gremmie@1 102 try:
gremmie@1 103 c = self.get(pk=1)
gremmie@1 104 return c.potd
gremmie@1 105 except Current.DoesNotExist:
gremmie@1 106 return None
gremmie@1 107
gremmie@1 108 def get_current_id(self):
bgneal@515 109 """
bgneal@515 110 Returns the ID of the current POTD from the current record.
bgneal@515 111
bgneal@515 112 """
gremmie@1 113 potd = self.get_current_photo()
gremmie@1 114 if potd is not None:
gremmie@1 115 return potd.pk
gremmie@1 116 return None
gremmie@1 117
gremmie@1 118
gremmie@1 119 class Current(models.Model):
gremmie@1 120 """This model simply stores the current POTD."""
gremmie@1 121 potd = models.ForeignKey(Photo)
gremmie@1 122
gremmie@1 123 objects = CurrentManager()
gremmie@1 124
gremmie@1 125 def __unicode__(self):
gremmie@1 126 return self.potd.__unicode__()
gremmie@1 127
gremmie@1 128 class Meta:
gremmie@1 129 verbose_name_plural = 'Current'
gremmie@1 130
gremmie@1 131
gremmie@1 132 class SequenceManager(models.Manager):
bgneal@515 133 """
bgneal@515 134 Manager for the Sequence model.
bgneal@515 135
bgneal@515 136 """
gremmie@1 137 def insert_photo(self, photo_id):
bgneal@515 138 """
bgneal@515 139 Inserts the given photo_id just after the current photo so it
bgneal@515 140 will appear as tomorrow's POTD.
bgneal@515 141
bgneal@515 142 """
gremmie@1 143 current = Current.objects.get_current_id()
gremmie@1 144 if current is not None:
bgneal@330 145 s = self.get(pk=1)
bgneal@330 146 seq = [int(x) for x in s.seq.split(',')]
bgneal@330 147 if photo_id not in seq:
bgneal@330 148 i = seq.index(current)
bgneal@330 149 seq.insert(i + 1, photo_id)
bgneal@330 150 s.seq = ','.join([str(x) for x in seq])
bgneal@330 151 s.save()
gremmie@1 152
gremmie@1 153 def remove_photo(self, photo_id):
bgneal@515 154 """
bgneal@515 155 Removes a given photo id from the sequence of photos.
bgneal@515 156
bgneal@515 157 """
gremmie@1 158 try:
gremmie@1 159 s = self.get(pk=1)
bgneal@330 160 except Sequence.DoesNotExist:
bgneal@330 161 pass
bgneal@330 162 else:
gremmie@1 163 seq = [int(x) for x in s.seq.split(',')]
gremmie@1 164 if photo_id in seq:
gremmie@1 165 seq.remove(photo_id)
gremmie@1 166 s.seq = ','.join([str(x) for x in seq])
gremmie@1 167 s.save()
gremmie@1 168
gremmie@1 169
gremmie@1 170 class Sequence(models.Model):
gremmie@1 171 """This model stores the sequence of photos for the POTD."""
gremmie@1 172 seq = models.CommaSeparatedIntegerField(max_length=4096)
gremmie@1 173
gremmie@1 174 objects = SequenceManager()
gremmie@1 175
gremmie@1 176 def __unicode__(self):
gremmie@1 177 return self.seq
gremmie@1 178
gremmie@1 179 class Meta:
gremmie@1 180 verbose_name_plural = 'Sequence'
gremmie@1 181