Mercurial > public > sg101
diff gpp/potd/models.py @ 1:dbd703f7d63a
Initial import of sg101 stuff from private repository.
author | gremmie |
---|---|
date | Mon, 06 Apr 2009 02:43:12 +0000 |
parents | |
children | 7b6540b185d9 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/potd/models.py Mon Apr 06 02:43:12 2009 +0000 @@ -0,0 +1,136 @@ +""" +Models for the Photo Of The Day (POTD) application. +""" +import os +from PIL import ImageFile +from PIL import Image +try: + from cStringIO import StringIO +except: + from StringIO import StringIO + +from django.db import models +from django.contrib.auth.models import User +from django.core.files.base import ContentFile + +POTD_THUMB_WIDTH = 120 + +def scale_image(image): + (w, h) = image.size + if w <= POTD_THUMB_WIDTH: + return image + scale_factor = float(POTD_THUMB_WIDTH) / w + new_height = int(scale_factor * h) + return image.resize((POTD_THUMB_WIDTH, new_height), Image.ANTIALIAS) + + +class Photo(models.Model): + """Model to represent a POTD""" + photo = models.ImageField(upload_to='potd/%Y/%m/%d') + thumb = models.ImageField(upload_to='potd/%Y/%m/%d/thumbs', blank=True, null=True) + caption = models.CharField(max_length=128) + description = models.TextField() + user = models.ForeignKey(User) + date_added = models.DateField(auto_now_add=True) + potd_count = models.IntegerField(default=0) + + def __unicode__(self): + return u'%s (%s)' % (self.caption, self.pk) + + class Meta: + ordering = ('-date_added', '-caption') + + def save(self, force_insert=False, force_update=False): + + if self.thumb: + self.thumb.delete(save=False) + + parser = ImageFile.Parser() + for chunk in self.photo.chunks(): + parser.feed(chunk) + image = parser.close() + format = image.format + image = scale_image(image) + s = StringIO() + image.save(s, format) + thumb_name = os.path.basename(self.photo.path) + self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False) + + super(Photo, self).save(force_insert, force_update) + Sequence.objects.insert_photo(self.pk) + + def delete(self): + Sequence.objects.remove_photo(self.pk) + super(Photo, self).delete() + + def can_comment_on(self): + return Current.objects.get_current_id() == self.id + + +class CurrentManager(models.Manager): + def get_current_photo(self): + try: + c = self.get(pk=1) + return c.potd + except Current.DoesNotExist: + return None + + def get_current_id(self): + potd = self.get_current_photo() + if potd is not None: + return potd.pk + return None + + +class Current(models.Model): + """This model simply stores the current POTD.""" + potd = models.ForeignKey(Photo) + + objects = CurrentManager() + + def __unicode__(self): + return self.potd.__unicode__() + + class Meta: + verbose_name_plural = 'Current' + + +class SequenceManager(models.Manager): + def insert_photo(self, photo_id): + current = Current.objects.get_current_id() + if current is not None: + try: + s = self.get(pk=1) + seq = [int(x) for x in s.seq.split(',')] + if photo_id not in seq: + i = seq.index(current) + seq.insert(i + 1, photo_id) + s.seq = ','.join([str(x) for x in seq]) + s.save() + except: + pass + + def remove_photo(self, photo_id): + try: + s = self.get(pk=1) + seq = [int(x) for x in s.seq.split(',')] + if photo_id in seq: + seq.remove(photo_id) + s.seq = ','.join([str(x) for x in seq]) + s.save() + except: + pass + + +class Sequence(models.Model): + """This model stores the sequence of photos for the POTD.""" + seq = models.CommaSeparatedIntegerField(max_length=4096) + + objects = SequenceManager() + + def __unicode__(self): + return self.seq + + class Meta: + verbose_name_plural = 'Sequence' +