Mercurial > public > sg101
comparison gpp/potd/models.py @ 330:3c951521e0ec
Fixing #152; POTD was saving thumbnail every night and making tons of thumbnail directories all over. Also added thumbnail images in the admin.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 24 Feb 2011 03:45:50 +0000 |
parents | 5c889b587416 |
children | 7dbdbb08e68c |
comparison
equal
deleted
inserted
replaced
329:000c006fee97 | 330:3c951521e0ec |
---|---|
43 @models.permalink | 43 @models.permalink |
44 def get_absolute_url(self): | 44 def get_absolute_url(self): |
45 return ('potd-archive', [str(self.id)]) | 45 return ('potd-archive', [str(self.id)]) |
46 | 46 |
47 def save(self, *args, **kwargs): | 47 def save(self, *args, **kwargs): |
48 if not self.pk: | |
49 self.generate_thumb() | |
48 | 50 |
51 super(Photo, self).save(*args, **kwargs) | |
52 Sequence.objects.insert_photo(self.pk) | |
53 | |
54 def delete(self): | |
55 Sequence.objects.remove_photo(self.pk) | |
56 super(Photo, self).delete() | |
57 | |
58 def can_comment_on(self): | |
59 return Current.objects.get_current_id() == self.id | |
60 | |
61 def generate_thumb(self): | |
49 if self.thumb: | 62 if self.thumb: |
50 self.thumb.delete(save=False) | 63 self.thumb.delete(save=False) |
51 | 64 |
52 parser = ImageFile.Parser() | 65 parser = ImageFile.Parser() |
53 for chunk in self.photo.chunks(): | 66 for chunk in self.photo.chunks(): |
57 image = scale_image(image) | 70 image = scale_image(image) |
58 s = StringIO() | 71 s = StringIO() |
59 image.save(s, format) | 72 image.save(s, format) |
60 thumb_name = os.path.basename(self.photo.path) | 73 thumb_name = os.path.basename(self.photo.path) |
61 self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False) | 74 self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False) |
62 | |
63 super(Photo, self).save(*args, **kwargs) | |
64 Sequence.objects.insert_photo(self.pk) | |
65 | |
66 def delete(self): | |
67 Sequence.objects.remove_photo(self.pk) | |
68 super(Photo, self).delete() | |
69 | |
70 def can_comment_on(self): | |
71 return Current.objects.get_current_id() == self.id | |
72 | 75 |
73 | 76 |
74 class CurrentManager(models.Manager): | 77 class CurrentManager(models.Manager): |
75 def get_current_photo(self): | 78 def get_current_photo(self): |
76 try: | 79 try: |
101 | 104 |
102 class SequenceManager(models.Manager): | 105 class SequenceManager(models.Manager): |
103 def insert_photo(self, photo_id): | 106 def insert_photo(self, photo_id): |
104 current = Current.objects.get_current_id() | 107 current = Current.objects.get_current_id() |
105 if current is not None: | 108 if current is not None: |
106 try: | 109 s = self.get(pk=1) |
107 s = self.get(pk=1) | 110 seq = [int(x) for x in s.seq.split(',')] |
108 seq = [int(x) for x in s.seq.split(',')] | 111 if photo_id not in seq: |
109 if photo_id not in seq: | 112 i = seq.index(current) |
110 i = seq.index(current) | 113 seq.insert(i + 1, photo_id) |
111 seq.insert(i + 1, photo_id) | 114 s.seq = ','.join([str(x) for x in seq]) |
112 s.seq = ','.join([str(x) for x in seq]) | 115 s.save() |
113 s.save() | |
114 except: | |
115 pass | |
116 | 116 |
117 def remove_photo(self, photo_id): | 117 def remove_photo(self, photo_id): |
118 try: | 118 try: |
119 s = self.get(pk=1) | 119 s = self.get(pk=1) |
120 except Sequence.DoesNotExist: | |
121 pass | |
122 else: | |
120 seq = [int(x) for x in s.seq.split(',')] | 123 seq = [int(x) for x in s.seq.split(',')] |
121 if photo_id in seq: | 124 if photo_id in seq: |
122 seq.remove(photo_id) | 125 seq.remove(photo_id) |
123 s.seq = ','.join([str(x) for x in seq]) | 126 s.seq = ','.join([str(x) for x in seq]) |
124 s.save() | 127 s.save() |
125 except: | |
126 pass | |
127 | 128 |
128 | 129 |
129 class Sequence(models.Model): | 130 class Sequence(models.Model): |
130 """This model stores the sequence of photos for the POTD.""" | 131 """This model stores the sequence of photos for the POTD.""" |
131 seq = models.CommaSeparatedIntegerField(max_length=4096) | 132 seq = models.CommaSeparatedIntegerField(max_length=4096) |