Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:900ba3c7b765 | 1:dbd703f7d63a |
---|---|
1 """ | |
2 Models for the Photo Of The Day (POTD) application. | |
3 """ | |
4 import os | |
5 from PIL import ImageFile | |
6 from PIL import Image | |
7 try: | |
8 from cStringIO import StringIO | |
9 except: | |
10 from StringIO import StringIO | |
11 | |
12 from django.db import models | |
13 from django.contrib.auth.models import User | |
14 from django.core.files.base import ContentFile | |
15 | |
16 POTD_THUMB_WIDTH = 120 | |
17 | |
18 def scale_image(image): | |
19 (w, h) = image.size | |
20 if w <= POTD_THUMB_WIDTH: | |
21 return image | |
22 scale_factor = float(POTD_THUMB_WIDTH) / w | |
23 new_height = int(scale_factor * h) | |
24 return image.resize((POTD_THUMB_WIDTH, new_height), Image.ANTIALIAS) | |
25 | |
26 | |
27 class Photo(models.Model): | |
28 """Model to represent a POTD""" | |
29 photo = models.ImageField(upload_to='potd/%Y/%m/%d') | |
30 thumb = models.ImageField(upload_to='potd/%Y/%m/%d/thumbs', blank=True, null=True) | |
31 caption = models.CharField(max_length=128) | |
32 description = models.TextField() | |
33 user = models.ForeignKey(User) | |
34 date_added = models.DateField(auto_now_add=True) | |
35 potd_count = models.IntegerField(default=0) | |
36 | |
37 def __unicode__(self): | |
38 return u'%s (%s)' % (self.caption, self.pk) | |
39 | |
40 class Meta: | |
41 ordering = ('-date_added', '-caption') | |
42 | |
43 def save(self, force_insert=False, force_update=False): | |
44 | |
45 if self.thumb: | |
46 self.thumb.delete(save=False) | |
47 | |
48 parser = ImageFile.Parser() | |
49 for chunk in self.photo.chunks(): | |
50 parser.feed(chunk) | |
51 image = parser.close() | |
52 format = image.format | |
53 image = scale_image(image) | |
54 s = StringIO() | |
55 image.save(s, format) | |
56 thumb_name = os.path.basename(self.photo.path) | |
57 self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False) | |
58 | |
59 super(Photo, self).save(force_insert, force_update) | |
60 Sequence.objects.insert_photo(self.pk) | |
61 | |
62 def delete(self): | |
63 Sequence.objects.remove_photo(self.pk) | |
64 super(Photo, self).delete() | |
65 | |
66 def can_comment_on(self): | |
67 return Current.objects.get_current_id() == self.id | |
68 | |
69 | |
70 class CurrentManager(models.Manager): | |
71 def get_current_photo(self): | |
72 try: | |
73 c = self.get(pk=1) | |
74 return c.potd | |
75 except Current.DoesNotExist: | |
76 return None | |
77 | |
78 def get_current_id(self): | |
79 potd = self.get_current_photo() | |
80 if potd is not None: | |
81 return potd.pk | |
82 return None | |
83 | |
84 | |
85 class Current(models.Model): | |
86 """This model simply stores the current POTD.""" | |
87 potd = models.ForeignKey(Photo) | |
88 | |
89 objects = CurrentManager() | |
90 | |
91 def __unicode__(self): | |
92 return self.potd.__unicode__() | |
93 | |
94 class Meta: | |
95 verbose_name_plural = 'Current' | |
96 | |
97 | |
98 class SequenceManager(models.Manager): | |
99 def insert_photo(self, photo_id): | |
100 current = Current.objects.get_current_id() | |
101 if current is not None: | |
102 try: | |
103 s = self.get(pk=1) | |
104 seq = [int(x) for x in s.seq.split(',')] | |
105 if photo_id not in seq: | |
106 i = seq.index(current) | |
107 seq.insert(i + 1, photo_id) | |
108 s.seq = ','.join([str(x) for x in seq]) | |
109 s.save() | |
110 except: | |
111 pass | |
112 | |
113 def remove_photo(self, photo_id): | |
114 try: | |
115 s = self.get(pk=1) | |
116 seq = [int(x) for x in s.seq.split(',')] | |
117 if photo_id in seq: | |
118 seq.remove(photo_id) | |
119 s.seq = ','.join([str(x) for x in seq]) | |
120 s.save() | |
121 except: | |
122 pass | |
123 | |
124 | |
125 class Sequence(models.Model): | |
126 """This model stores the sequence of photos for the POTD.""" | |
127 seq = models.CommaSeparatedIntegerField(max_length=4096) | |
128 | |
129 objects = SequenceManager() | |
130 | |
131 def __unicode__(self): | |
132 return self.seq | |
133 | |
134 class Meta: | |
135 verbose_name_plural = 'Sequence' | |
136 |