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
|
bgneal@330
|
54 def can_comment_on(self):
|
bgneal@330
|
55 return Current.objects.get_current_id() == self.id
|
bgneal@330
|
56
|
bgneal@330
|
57 def generate_thumb(self):
|
gremmie@1
|
58 if self.thumb:
|
gremmie@1
|
59 self.thumb.delete(save=False)
|
gremmie@1
|
60
|
gremmie@1
|
61 parser = ImageFile.Parser()
|
gremmie@1
|
62 for chunk in self.photo.chunks():
|
gremmie@1
|
63 parser.feed(chunk)
|
gremmie@1
|
64 image = parser.close()
|
gremmie@1
|
65 format = image.format
|
gremmie@1
|
66 image = scale_image(image)
|
gremmie@1
|
67 s = StringIO()
|
gremmie@1
|
68 image.save(s, format)
|
gremmie@1
|
69 thumb_name = os.path.basename(self.photo.path)
|
gremmie@1
|
70 self.thumb.save(thumb_name, ContentFile(s.getvalue()), save=False)
|
gremmie@1
|
71
|
bgneal@491
|
72 def ogp_tags(self):
|
bgneal@491
|
73 """
|
bgneal@491
|
74 Returns a dict of Open Graph Protocol meta tags.
|
bgneal@491
|
75
|
bgneal@491
|
76 """
|
bgneal@491
|
77 desc = "Photo of the day: %s." % self.caption
|
bgneal@491
|
78 return {
|
bgneal@491
|
79 'og:title': self.caption,
|
bgneal@491
|
80 'og:type': 'article',
|
bgneal@491
|
81 'og:url': self.get_absolute_url(),
|
bgneal@491
|
82 'og:image': self.photo.url,
|
bgneal@491
|
83 'og:description': desc,
|
bgneal@491
|
84 }
|
bgneal@491
|
85
|
gremmie@1
|
86
|
gremmie@1
|
87 class CurrentManager(models.Manager):
|
bgneal@515
|
88 """
|
bgneal@515
|
89 Manager for the Current model.
|
bgneal@515
|
90
|
bgneal@515
|
91 """
|
gremmie@1
|
92 def get_current_photo(self):
|
bgneal@515
|
93 """
|
bgneal@515
|
94 Retrieves the current photo object from the current record.
|
bgneal@515
|
95
|
bgneal@515
|
96 """
|
gremmie@1
|
97 try:
|
gremmie@1
|
98 c = self.get(pk=1)
|
gremmie@1
|
99 return c.potd
|
gremmie@1
|
100 except Current.DoesNotExist:
|
gremmie@1
|
101 return None
|
gremmie@1
|
102
|
gremmie@1
|
103 def get_current_id(self):
|
bgneal@515
|
104 """
|
bgneal@515
|
105 Returns the ID of the current POTD from the current record.
|
bgneal@515
|
106
|
bgneal@515
|
107 """
|
gremmie@1
|
108 potd = self.get_current_photo()
|
gremmie@1
|
109 if potd is not None:
|
gremmie@1
|
110 return potd.pk
|
gremmie@1
|
111 return None
|
gremmie@1
|
112
|
gremmie@1
|
113
|
gremmie@1
|
114 class Current(models.Model):
|
gremmie@1
|
115 """This model simply stores the current POTD."""
|
gremmie@1
|
116 potd = models.ForeignKey(Photo)
|
gremmie@1
|
117
|
gremmie@1
|
118 objects = CurrentManager()
|
gremmie@1
|
119
|
gremmie@1
|
120 def __unicode__(self):
|
gremmie@1
|
121 return self.potd.__unicode__()
|
gremmie@1
|
122
|
gremmie@1
|
123 class Meta:
|
gremmie@1
|
124 verbose_name_plural = 'Current'
|
gremmie@1
|
125
|
gremmie@1
|
126
|
gremmie@1
|
127 class SequenceManager(models.Manager):
|
bgneal@515
|
128 """
|
bgneal@515
|
129 Manager for the Sequence model.
|
bgneal@515
|
130
|
bgneal@515
|
131 """
|
gremmie@1
|
132 def insert_photo(self, photo_id):
|
bgneal@515
|
133 """
|
bgneal@515
|
134 Inserts the given photo_id just after the current photo so it
|
bgneal@515
|
135 will appear as tomorrow's POTD.
|
bgneal@515
|
136
|
bgneal@515
|
137 """
|
gremmie@1
|
138 current = Current.objects.get_current_id()
|
gremmie@1
|
139 if current is not None:
|
bgneal@330
|
140 s = self.get(pk=1)
|
bgneal@330
|
141 seq = [int(x) for x in s.seq.split(',')]
|
bgneal@330
|
142 if photo_id not in seq:
|
bgneal@330
|
143 i = seq.index(current)
|
bgneal@330
|
144 seq.insert(i + 1, photo_id)
|
bgneal@330
|
145 s.seq = ','.join([str(x) for x in seq])
|
bgneal@330
|
146 s.save()
|
gremmie@1
|
147
|
gremmie@1
|
148 def remove_photo(self, photo_id):
|
bgneal@515
|
149 """
|
bgneal@515
|
150 Removes a given photo id from the sequence of photos.
|
bgneal@515
|
151
|
bgneal@515
|
152 """
|
gremmie@1
|
153 try:
|
gremmie@1
|
154 s = self.get(pk=1)
|
bgneal@330
|
155 except Sequence.DoesNotExist:
|
bgneal@330
|
156 pass
|
bgneal@330
|
157 else:
|
gremmie@1
|
158 seq = [int(x) for x in s.seq.split(',')]
|
gremmie@1
|
159 if photo_id in seq:
|
gremmie@1
|
160 seq.remove(photo_id)
|
gremmie@1
|
161 s.seq = ','.join([str(x) for x in seq])
|
gremmie@1
|
162 s.save()
|
gremmie@1
|
163
|
gremmie@1
|
164
|
gremmie@1
|
165 class Sequence(models.Model):
|
gremmie@1
|
166 """This model stores the sequence of photos for the POTD."""
|
gremmie@1
|
167 seq = models.CommaSeparatedIntegerField(max_length=4096)
|
gremmie@1
|
168
|
gremmie@1
|
169 objects = SequenceManager()
|
gremmie@1
|
170
|
gremmie@1
|
171 def __unicode__(self):
|
gremmie@1
|
172 return self.seq
|
gremmie@1
|
173
|
gremmie@1
|
174 class Meta:
|
gremmie@1
|
175 verbose_name_plural = 'Sequence'
|