Mercurial > public > sg101
comparison gpp/potd/models.py @ 515:ae89ba801e8b
For #194, convert the POTD management command to a celery task.
Refactored to put the logic for the command into a function, and the command simply calls this function. The task can also just call this function. Added some basic tests for the new function.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 14 Dec 2011 02:41:15 +0000 |
parents | 7dbdbb08e68c |
children | ff67946fd4b0 |
comparison
equal
deleted
inserted
replaced
514:6d816aa586c1 | 515:ae89ba801e8b |
---|---|
1 """ | 1 """ |
2 Models for the Photo Of The Day (POTD) application. | 2 Models for the Photo Of The Day (POTD) application. |
3 | |
3 """ | 4 """ |
4 import os | 5 import os |
5 from PIL import ImageFile | 6 from PIL import ImageFile |
6 from PIL import Image | 7 from PIL import Image |
7 try: | 8 try: |
87 'og:description': desc, | 88 'og:description': desc, |
88 } | 89 } |
89 | 90 |
90 | 91 |
91 class CurrentManager(models.Manager): | 92 class CurrentManager(models.Manager): |
93 """ | |
94 Manager for the Current model. | |
95 | |
96 """ | |
92 def get_current_photo(self): | 97 def get_current_photo(self): |
98 """ | |
99 Retrieves the current photo object from the current record. | |
100 | |
101 """ | |
93 try: | 102 try: |
94 c = self.get(pk=1) | 103 c = self.get(pk=1) |
95 return c.potd | 104 return c.potd |
96 except Current.DoesNotExist: | 105 except Current.DoesNotExist: |
97 return None | 106 return None |
98 | 107 |
99 def get_current_id(self): | 108 def get_current_id(self): |
109 """ | |
110 Returns the ID of the current POTD from the current record. | |
111 | |
112 """ | |
100 potd = self.get_current_photo() | 113 potd = self.get_current_photo() |
101 if potd is not None: | 114 if potd is not None: |
102 return potd.pk | 115 return potd.pk |
103 return None | 116 return None |
104 | 117 |
115 class Meta: | 128 class Meta: |
116 verbose_name_plural = 'Current' | 129 verbose_name_plural = 'Current' |
117 | 130 |
118 | 131 |
119 class SequenceManager(models.Manager): | 132 class SequenceManager(models.Manager): |
133 """ | |
134 Manager for the Sequence model. | |
135 | |
136 """ | |
120 def insert_photo(self, photo_id): | 137 def insert_photo(self, photo_id): |
138 """ | |
139 Inserts the given photo_id just after the current photo so it | |
140 will appear as tomorrow's POTD. | |
141 | |
142 """ | |
121 current = Current.objects.get_current_id() | 143 current = Current.objects.get_current_id() |
122 if current is not None: | 144 if current is not None: |
123 s = self.get(pk=1) | 145 s = self.get(pk=1) |
124 seq = [int(x) for x in s.seq.split(',')] | 146 seq = [int(x) for x in s.seq.split(',')] |
125 if photo_id not in seq: | 147 if photo_id not in seq: |
127 seq.insert(i + 1, photo_id) | 149 seq.insert(i + 1, photo_id) |
128 s.seq = ','.join([str(x) for x in seq]) | 150 s.seq = ','.join([str(x) for x in seq]) |
129 s.save() | 151 s.save() |
130 | 152 |
131 def remove_photo(self, photo_id): | 153 def remove_photo(self, photo_id): |
154 """ | |
155 Removes a given photo id from the sequence of photos. | |
156 | |
157 """ | |
132 try: | 158 try: |
133 s = self.get(pk=1) | 159 s = self.get(pk=1) |
134 except Sequence.DoesNotExist: | 160 except Sequence.DoesNotExist: |
135 pass | 161 pass |
136 else: | 162 else: |