Show More
@@ -45,13 +45,11 b' class PostManager(models.Manager):' | |||||
45 | last_edit_time=timezone.now(), |
|
45 | last_edit_time=timezone.now(), | |
46 | user=user) |
|
46 | user=user) | |
47 |
|
47 | |||
48 | if thread: |
|
|||
49 | thread.replies.add(post) |
|
|||
50 |
|
||||
51 | if tags: |
|
48 | if tags: | |
52 | map(post.tags.add, tags) |
|
49 | map(post.tags.add, tags) | |
53 |
|
50 | |||
54 | if thread: |
|
51 | if thread: | |
|
52 | thread.replies.add(post) | |||
55 | thread.bump() |
|
53 | thread.bump() | |
56 | else: |
|
54 | else: | |
57 | self._delete_old_threads() |
|
55 | self._delete_old_threads() | |
@@ -138,32 +136,20 b' class PostManager(models.Manager):' | |||||
138 |
|
136 | |||
139 | map(self.delete_post, old_threads) |
|
137 | map(self.delete_post, old_threads) | |
140 |
|
138 | |||
141 | def _bump_thread(self, thread_id): |
|
|||
142 | thread = self.get(id=thread_id) |
|
|||
143 |
|
||||
144 | if thread.can_bump(): |
|
|||
145 | thread.last_edit_time = timezone.now() |
|
|||
146 | thread.save() |
|
|||
147 |
|
||||
148 |
|
139 | |||
149 | class TagManager(models.Manager): |
|
140 | class TagManager(models.Manager): | |
|
141 | ||||
150 | def get_not_empty_tags(self): |
|
142 | def get_not_empty_tags(self): | |
151 | all_tags = self.all().order_by('name') |
|
143 | all_tags = self.all().order_by('name') | |
152 | tags = [] |
|
144 | tags = [] | |
|
145 | ||||
|
146 | # TODO Use query here | |||
153 | for tag in all_tags: |
|
147 | for tag in all_tags: | |
154 | if not tag.is_empty(): |
|
148 | if not tag.is_empty(): | |
155 | tags.append(tag) |
|
149 | tags.append(tag) | |
156 |
|
150 | |||
157 | return tags |
|
151 | return tags | |
158 |
|
152 | |||
159 | def get_popular_tags(self): |
|
|||
160 | all_tags = self.get_not_empty_tags() |
|
|||
161 |
|
||||
162 | sorted_tags = sorted(all_tags, key=lambda tag: tag.get_popularity(), |
|
|||
163 | reverse=True) |
|
|||
164 |
|
||||
165 | return sorted_tags[:settings.POPULAR_TAGS] |
|
|||
166 |
|
||||
167 |
|
153 | |||
168 | class Tag(models.Model): |
|
154 | class Tag(models.Model): | |
169 | """ |
|
155 | """ | |
@@ -182,11 +168,11 b' class Tag(models.Model):' | |||||
182 | return self.get_post_count() == 0 |
|
168 | return self.get_post_count() == 0 | |
183 |
|
169 | |||
184 | def get_post_count(self): |
|
170 | def get_post_count(self): | |
185 |
posts_with_tag = Post.objects. |
|
171 | posts_with_tag = Post.objects.filter(tag=self) | |
186 | return posts_with_tag.count() |
|
172 | return posts_with_tag.count() | |
187 |
|
173 | |||
188 | def get_popularity(self): |
|
174 | def get_popularity(self): | |
189 |
posts_with_tag = Post.objects. |
|
175 | posts_with_tag = Post.objects.filter(tag=self) | |
190 | reply_count = 0 |
|
176 | reply_count = 0 | |
191 | for post in posts_with_tag: |
|
177 | for post in posts_with_tag: | |
192 | reply_count += post.get_reply_count() |
|
178 | reply_count += post.get_reply_count() | |
@@ -227,7 +213,7 b' class Post(models.Model):' | |||||
227 | poster_ip = models.GenericIPAddressField() |
|
213 | poster_ip = models.GenericIPAddressField() | |
228 | poster_user_agent = models.TextField() |
|
214 | poster_user_agent = models.TextField() | |
229 |
|
215 | |||
230 | # TODO Convert this field to ForeignKey |
|
216 | # TODO Remove this field after everything has been updated to 'thread' | |
231 | parent = models.BigIntegerField(default=NO_PARENT) |
|
217 | parent = models.BigIntegerField(default=NO_PARENT) | |
232 |
|
218 | |||
233 | thread = models.ForeignKey('Post', null=True, default=None) |
|
219 | thread = models.ForeignKey('Post', null=True, default=None) | |
@@ -249,15 +235,13 b' class Post(models.Model):' | |||||
249 |
|
235 | |||
250 | return title |
|
236 | return title | |
251 |
|
237 | |||
252 | def _get_replies(self): |
|
|||
253 | return self.replies |
|
|||
254 |
|
||||
255 | def get_reply_count(self): |
|
238 | def get_reply_count(self): | |
256 | return self.replies.count() |
|
239 | return self.replies.count() | |
257 |
|
240 | |||
258 | def get_images_count(self): |
|
241 | def get_images_count(self): | |
259 | images_count = 1 if self.image else 0 |
|
242 | images_count = 1 if self.image else 0 | |
260 |
|
243 | |||
|
244 | # TODO Use query here | |||
261 | for reply in self.replies: |
|
245 | for reply in self.replies: | |
262 | if reply.image: |
|
246 | if reply.image: | |
263 | images_count += 1 |
|
247 | images_count += 1 | |
@@ -272,6 +256,8 b' class Post(models.Model):' | |||||
272 | return post_count <= settings.MAX_POSTS_PER_THREAD |
|
256 | return post_count <= settings.MAX_POSTS_PER_THREAD | |
273 |
|
257 | |||
274 | def bump(self): |
|
258 | def bump(self): | |
|
259 | """Bump (move to up) thread""" | |||
|
260 | ||||
275 | if self.can_bump(): |
|
261 | if self.can_bump(): | |
276 | self.last_edit_time = timezone.now() |
|
262 | self.last_edit_time = timezone.now() | |
277 | self.save() |
|
263 | self.save() | |
@@ -335,6 +321,7 b' class Setting(models.Model):' | |||||
335 |
|
321 | |||
336 |
|
322 | |||
337 | class Ban(models.Model): |
|
323 | class Ban(models.Model): | |
|
324 | ||||
338 | ip = models.GenericIPAddressField() |
|
325 | ip = models.GenericIPAddressField() | |
339 |
|
326 | |||
340 | def __unicode__(self): |
|
327 | def __unicode__(self): |
General Comments 0
You need to be logged in to leave comments.
Login now