Show More
@@ -0,0 +1,85 b'' | |||||
|
1 | from boards.models import Post | |||
|
2 | ||||
|
3 | __author__ = 'neko259' | |||
|
4 | ||||
|
5 | ||||
|
6 | from django.db import models | |||
|
7 | from django.db.models import Count | |||
|
8 | ||||
|
9 | TAG_FONT_MULTIPLIER = 0.1 | |||
|
10 | MAX_TAG_FONT = 10 | |||
|
11 | OPENING_POST_POPULARITY_WEIGHT = 2 | |||
|
12 | ||||
|
13 | ||||
|
14 | class TagManager(models.Manager): | |||
|
15 | ||||
|
16 | def get_not_empty_tags(self): | |||
|
17 | tags = self.annotate(Count('threads')) \ | |||
|
18 | .filter(threads__count__gt=0).order_by('name') | |||
|
19 | ||||
|
20 | return tags | |||
|
21 | ||||
|
22 | ||||
|
23 | class Tag(models.Model): | |||
|
24 | """ | |||
|
25 | A tag is a text node assigned to the post. The tag serves as a board | |||
|
26 | section. There can be multiple tags for each message | |||
|
27 | """ | |||
|
28 | ||||
|
29 | objects = TagManager() | |||
|
30 | ||||
|
31 | class Meta: | |||
|
32 | app_label = 'boards' | |||
|
33 | ||||
|
34 | name = models.CharField(max_length=100) | |||
|
35 | threads = models.ManyToManyField('Post', null=True, | |||
|
36 | blank=True, related_name='tag+') | |||
|
37 | linked = models.ForeignKey('Tag', null=True, blank=True) | |||
|
38 | ||||
|
39 | def __unicode__(self): | |||
|
40 | return self.name | |||
|
41 | ||||
|
42 | def is_empty(self): | |||
|
43 | return self.get_post_count() == 0 | |||
|
44 | ||||
|
45 | def get_post_count(self): | |||
|
46 | return self.threads.count() | |||
|
47 | ||||
|
48 | def get_popularity(self): | |||
|
49 | posts_with_tag = Post.objects.get_threads(tag=self) | |||
|
50 | reply_count = 0 | |||
|
51 | for post in posts_with_tag: | |||
|
52 | reply_count += post.get_reply_count() | |||
|
53 | reply_count += OPENING_POST_POPULARITY_WEIGHT | |||
|
54 | ||||
|
55 | return reply_count | |||
|
56 | ||||
|
57 | def get_linked_tags(self): | |||
|
58 | tag_list = [] | |||
|
59 | self.get_linked_tags_list(tag_list) | |||
|
60 | ||||
|
61 | return tag_list | |||
|
62 | ||||
|
63 | def get_linked_tags_list(self, tag_list=[]): | |||
|
64 | """ | |||
|
65 | Returns the list of tags linked to current. The list can be got | |||
|
66 | through returned value or tag_list parameter | |||
|
67 | """ | |||
|
68 | ||||
|
69 | linked_tag = self.linked | |||
|
70 | ||||
|
71 | if linked_tag and not (linked_tag in tag_list): | |||
|
72 | tag_list.append(linked_tag) | |||
|
73 | ||||
|
74 | linked_tag.get_linked_tags_list(tag_list) | |||
|
75 | ||||
|
76 | def get_font_value(self): | |||
|
77 | """Get tag font value to differ most popular tags in the list""" | |||
|
78 | ||||
|
79 | post_count = self.get_post_count() | |||
|
80 | if post_count > MAX_TAG_FONT: | |||
|
81 | post_count = MAX_TAG_FONT | |||
|
82 | ||||
|
83 | font_value = str(1 + (post_count - 1) * TAG_FONT_MULTIPLIER) | |||
|
84 | ||||
|
85 | return font_value No newline at end of file |
@@ -1,7 +1,7 b'' | |||||
1 | __author__ = 'neko259' |
|
1 | __author__ = 'neko259' | |
2 |
|
2 | |||
3 | from boards.models.post import Post |
|
3 | from boards.models.post import Post | |
4 |
from boards.models. |
|
4 | from boards.models.tag import Tag | |
5 | from boards.models.post import Ban |
|
5 | from boards.models.post import Ban | |
6 | from boards.models.post import Setting |
|
6 | from boards.models.post import Setting | |
7 | from boards.models.post import User |
|
7 | from boards.models.post import User |
@@ -16,10 +16,6 b' from boards import thumbs' | |||||
16 |
|
16 | |||
17 | import re |
|
17 | import re | |
18 |
|
18 | |||
19 | TAG_FONT_MULTIPLIER = 0.2 |
|
|||
20 |
|
||||
21 | MAX_TAG_FONT = 4 |
|
|||
22 |
|
||||
23 | BAN_REASON_MAX_LENGTH = 200 |
|
19 | BAN_REASON_MAX_LENGTH = 200 | |
24 |
|
20 | |||
25 | BAN_REASON_AUTO = 'Auto' |
|
21 | BAN_REASON_AUTO = 'Auto' | |
@@ -34,7 +30,6 b' NO_PARENT = -1' | |||||
34 | NO_IP = '0.0.0.0' |
|
30 | NO_IP = '0.0.0.0' | |
35 | UNKNOWN_UA = '' |
|
31 | UNKNOWN_UA = '' | |
36 | ALL_PAGES = -1 |
|
32 | ALL_PAGES = -1 | |
37 | OPENING_POST_POPULARITY_WEIGHT = 2 |
|
|||
38 | IMAGES_DIRECTORY = 'images/' |
|
33 | IMAGES_DIRECTORY = 'images/' | |
39 | FILE_EXTENSION_DELIMITER = '.' |
|
34 | FILE_EXTENSION_DELIMITER = '.' | |
40 |
|
35 | |||
@@ -201,80 +196,6 b' class PostManager(models.Manager):' | |||||
201 | return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) |
|
196 | return int(math.ceil(thread_count / float(settings.THREADS_PER_PAGE))) | |
202 |
|
197 | |||
203 |
|
198 | |||
204 | class TagManager(models.Manager): |
|
|||
205 |
|
||||
206 | def get_not_empty_tags(self): |
|
|||
207 | tags = self.annotate(Count('threads')) \ |
|
|||
208 | .filter(threads__count__gt=0).order_by('name') |
|
|||
209 |
|
||||
210 | return tags |
|
|||
211 |
|
||||
212 |
|
||||
213 | class Tag(models.Model): |
|
|||
214 | """ |
|
|||
215 | A tag is a text node assigned to the post. The tag serves as a board |
|
|||
216 | section. There can be multiple tags for each message |
|
|||
217 | """ |
|
|||
218 |
|
||||
219 | objects = TagManager() |
|
|||
220 |
|
||||
221 | class Meta: |
|
|||
222 | app_label = 'boards' |
|
|||
223 |
|
||||
224 | name = models.CharField(max_length=100) |
|
|||
225 | threads = models.ManyToManyField('Post', null=True, |
|
|||
226 | blank=True, related_name='tag+') |
|
|||
227 | linked = models.ForeignKey('Tag', null=True, blank=True) |
|
|||
228 |
|
||||
229 | def __unicode__(self): |
|
|||
230 | return self.name |
|
|||
231 |
|
||||
232 | def is_empty(self): |
|
|||
233 | return self.get_post_count() == 0 |
|
|||
234 |
|
||||
235 | def get_post_count(self): |
|
|||
236 | return self.threads.count() |
|
|||
237 |
|
||||
238 | def get_popularity(self): |
|
|||
239 | posts_with_tag = Post.objects.get_threads(tag=self) |
|
|||
240 | reply_count = 0 |
|
|||
241 | for post in posts_with_tag: |
|
|||
242 | reply_count += post.get_reply_count() |
|
|||
243 | reply_count += OPENING_POST_POPULARITY_WEIGHT |
|
|||
244 |
|
||||
245 | return reply_count |
|
|||
246 |
|
||||
247 | def get_linked_tags(self): |
|
|||
248 | tag_list = [] |
|
|||
249 | self.get_linked_tags_list(tag_list) |
|
|||
250 |
|
||||
251 | return tag_list |
|
|||
252 |
|
||||
253 | def get_linked_tags_list(self, tag_list=[]): |
|
|||
254 | """ |
|
|||
255 | Returns the list of tags linked to current. The list can be got |
|
|||
256 | through returned value or tag_list parameter |
|
|||
257 | """ |
|
|||
258 |
|
||||
259 | linked_tag = self.linked |
|
|||
260 |
|
||||
261 | if linked_tag and not (linked_tag in tag_list): |
|
|||
262 | tag_list.append(linked_tag) |
|
|||
263 |
|
||||
264 | linked_tag.get_linked_tags_list(tag_list) |
|
|||
265 |
|
||||
266 | def get_font_value(self): |
|
|||
267 | """Get tag font value to differ most popular tags in the list""" |
|
|||
268 |
|
||||
269 | post_count = self.get_post_count() |
|
|||
270 | if post_count > MAX_TAG_FONT: |
|
|||
271 | post_count = MAX_TAG_FONT |
|
|||
272 |
|
||||
273 | font_value = str(1 + (post_count - 1) * TAG_FONT_MULTIPLIER) |
|
|||
274 |
|
||||
275 | return font_value |
|
|||
276 |
|
||||
277 |
|
||||
278 | class Post(models.Model): |
|
199 | class Post(models.Model): | |
279 | """A post is a message.""" |
|
200 | """A post is a message.""" | |
280 |
|
201 | |||
@@ -311,7 +232,7 b' class Post(models.Model):' | |||||
311 | poster_user_agent = models.TextField() |
|
232 | poster_user_agent = models.TextField() | |
312 |
|
233 | |||
313 | thread = models.ForeignKey('Post', null=True, default=None) |
|
234 | thread = models.ForeignKey('Post', null=True, default=None) | |
314 | tags = models.ManyToManyField(Tag) |
|
235 | tags = models.ManyToManyField('Tag') | |
315 | last_edit_time = models.DateTimeField() |
|
236 | last_edit_time = models.DateTimeField() | |
316 | bump_time = models.DateTimeField() |
|
237 | bump_time = models.DateTimeField() | |
317 | user = models.ForeignKey('User', null=True, default=None) |
|
238 | user = models.ForeignKey('User', null=True, default=None) | |
@@ -392,7 +313,7 b' class User(models.Model):' | |||||
392 |
|
313 | |||
393 | registration_time = models.DateTimeField() |
|
314 | registration_time = models.DateTimeField() | |
394 |
|
315 | |||
395 | fav_tags = models.ManyToManyField(Tag, null=True, blank=True) |
|
316 | fav_tags = models.ManyToManyField('Tag', null=True, blank=True) | |
396 | fav_threads = models.ManyToManyField(Post, related_name='+', null=True, |
|
317 | fav_threads = models.ManyToManyField(Post, related_name='+', null=True, | |
397 | blank=True) |
|
318 | blank=True) | |
398 |
|
319 |
@@ -19,8 +19,8 b' import boards' | |||||
19 | from boards import utils |
|
19 | from boards import utils | |
20 | from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ |
|
20 | from boards.forms import ThreadForm, PostForm, SettingsForm, PlainErrorList, \ | |
21 | ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm |
|
21 | ThreadCaptchaForm, PostCaptchaForm, LoginForm, ModeratorSettingsForm | |
22 |
from boards.models |
|
22 | from boards.models import Post, Tag, Ban, User | |
23 | SETTING_MODERATE, REGEX_REPLY |
|
23 | from boards.models.post import RANK_USER, SETTING_MODERATE, REGEX_REPLY | |
24 | from boards import authors |
|
24 | from boards import authors | |
25 | from boards.utils import get_client_ip |
|
25 | from boards.utils import get_client_ip | |
26 | import neboard |
|
26 | import neboard |
General Comments 0
You need to be logged in to leave comments.
Login now