Show More
@@ -1,201 +1,211 b'' | |||||
1 | import hashlib |
|
1 | import hashlib | |
2 | from django.urls import reverse |
|
2 | from django.urls import reverse | |
3 | from django.db import models |
|
3 | from django.db import models | |
4 | from django.db.models import Count, Q |
|
4 | from django.db.models import Count, Q | |
5 | from django.utils.translation import get_language |
|
5 | from django.utils.translation import get_language | |
6 |
|
6 | |||
7 | import boards |
|
7 | import boards | |
8 | from boards.models import Attachment |
|
8 | from boards.models import Attachment | |
9 | from boards.models.attachment import FILE_TYPES_IMAGE |
|
9 | from boards.models.attachment import FILE_TYPES_IMAGE | |
10 | from boards.models.base import Viewable |
|
10 | from boards.models.base import Viewable | |
11 | from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE |
|
11 | from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE | |
12 | from boards.utils import cached_result |
|
12 | from boards.utils import cached_result | |
13 |
|
13 | |||
14 |
|
14 | |||
15 | __author__ = 'neko259' |
|
15 | __author__ = 'neko259' | |
16 |
|
16 | |||
17 |
|
17 | |||
18 | TAG_DELIMITER = ', ' |
|
18 | TAG_DELIMITER = ', ' | |
19 | RELATED_TAGS_COUNT = 5 |
|
19 | RELATED_TAGS_COUNT = 5 | |
20 | DEFAULT_LOCALE = 'default' |
|
20 | DEFAULT_LOCALE = 'default' | |
21 |
|
21 | |||
22 |
|
22 | |||
23 | class TagAliasManager(models.Manager): |
|
23 | class TagAliasManager(models.Manager): | |
24 | def filter_localized(self, *args, **kwargs): |
|
24 | def filter_localized(self, *args, **kwargs): | |
25 | locale = get_language() |
|
25 | locale = get_language() | |
26 | tag_aliases = (self.filter(locale=locale) |
|
26 | tag_aliases = (self.filter(locale=locale) | |
27 | | self.filter(Q(locale=DEFAULT_LOCALE) |
|
27 | | self.filter(Q(locale=DEFAULT_LOCALE) | |
28 | & ~Q(parent__aliases__locale=locale))) |
|
28 | & ~Q(parent__aliases__locale=locale))) | |
29 | return tag_aliases.filter(**kwargs) |
|
29 | return tag_aliases.filter(**kwargs) | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | class TagAlias(models.Model, Viewable): |
|
32 | class TagAlias(models.Model, Viewable): | |
33 | class Meta: |
|
33 | class Meta: | |
34 | app_label = 'boards' |
|
34 | app_label = 'boards' | |
35 | ordering = ('name',) |
|
35 | ordering = ('name',) | |
36 | unique_together = ('name', 'locale') |
|
36 | unique_together = ('name', 'locale') | |
37 |
|
37 | |||
38 | objects = TagAliasManager() |
|
38 | objects = TagAliasManager() | |
39 |
|
39 | |||
40 | name = models.CharField(max_length=100, db_index=True) |
|
40 | name = models.CharField(max_length=100, db_index=True) | |
41 | locale = models.CharField(max_length=10, db_index=True) |
|
41 | locale = models.CharField(max_length=10, db_index=True) | |
42 |
|
42 | |||
43 | parent = models.ForeignKey('Tag', on_delete=models.CASCADE, null=True, |
|
43 | parent = models.ForeignKey('Tag', on_delete=models.CASCADE, null=True, | |
44 | blank=True, related_name='aliases') |
|
44 | blank=True, related_name='aliases') | |
45 |
|
45 | |||
46 | def __str__(self): |
|
46 | def __str__(self): | |
47 | return self.name |
|
47 | return self.name | |
48 |
|
48 | |||
49 |
|
49 | |||
50 | class TagManager(models.Manager): |
|
50 | class TagManager(models.Manager): | |
51 | def get_tag_url_list(self, tags: list) -> str: |
|
51 | def get_tag_url_list(self, tags: list) -> str: | |
52 | """ |
|
52 | """ | |
53 | Gets a comma-separated list of tag links. |
|
53 | Gets a comma-separated list of tag links. | |
54 | """ |
|
54 | """ | |
55 |
|
55 | |||
56 | return TAG_DELIMITER.join([tag.get_view() for tag in tags]) |
|
56 | return TAG_DELIMITER.join([tag.get_view() for tag in tags]) | |
57 |
|
57 | |||
58 | def get_by_alias(self, alias): |
|
58 | def get_by_alias(self, alias): | |
59 | try: |
|
59 | try: | |
60 | return self.filter(aliases__name__in=[alias]).first() |
|
60 | return self.filter(aliases__name__in=[alias]).first() | |
61 | except Tag.DoesNotExist: |
|
61 | except Tag.DoesNotExist: | |
62 | pass |
|
62 | pass | |
63 |
|
63 | |||
64 | def get_or_create_with_alias(self, name, required=False): |
|
64 | def get_or_create_with_alias(self, name, required=False): | |
65 | tag = self.get_by_alias(name) |
|
65 | tag = self.get_by_alias(name) | |
66 | created = False |
|
66 | created = False | |
67 | if not tag: |
|
67 | if not tag: | |
68 | tag = self.create(required=required) |
|
68 | tag = self.create(required=required) | |
69 | TagAlias.objects.create(name=name, locale=DEFAULT_LOCALE, parent=tag) |
|
69 | TagAlias.objects.create(name=name, locale=DEFAULT_LOCALE, parent=tag) | |
70 | created = True |
|
70 | created = True | |
71 | return tag, created |
|
71 | return tag, created | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | class Tag(models.Model, Viewable): |
|
74 | class Tag(models.Model, Viewable): | |
75 | """ |
|
75 | """ | |
76 | A tag is a text node assigned to the thread. The tag serves as a board |
|
76 | A tag is a text node assigned to the thread. The tag serves as a board | |
77 | section. There can be multiple tags for each thread |
|
77 | section. There can be multiple tags for each thread | |
78 | """ |
|
78 | """ | |
79 |
|
79 | |||
80 | objects = TagManager() |
|
80 | objects = TagManager() | |
81 |
|
81 | |||
82 | class Meta: |
|
82 | class Meta: | |
83 | app_label = 'boards' |
|
83 | app_label = 'boards' | |
84 |
|
84 | |||
85 | required = models.BooleanField(default=False, db_index=True) |
|
85 | required = models.BooleanField(default=False, db_index=True) | |
86 | description = models.TextField(blank=True) |
|
86 | description = models.TextField(blank=True) | |
87 |
|
87 | |||
88 | parent = models.ForeignKey('Tag', on_delete=models.CASCADE, null=True, |
|
88 | parent = models.ForeignKey('Tag', on_delete=models.CASCADE, null=True, | |
89 | blank=True, related_name='children') |
|
89 | blank=True, related_name='children') | |
90 |
|
90 | |||
91 | def get_name(self): |
|
91 | def get_name(self): | |
92 | return self.aliases.get(locale=DEFAULT_LOCALE).name |
|
92 | try: | |
|
93 | alias = self.aliases.get(locale=DEFAULT_LOCALE) | |||
|
94 | except TagAlias.DoesNotExist: | |||
|
95 | alias = self.aliases.first() | |||
|
96 | ||||
|
97 | if alias: | |||
|
98 | return alias.name | |||
|
99 | else: | |||
|
100 | # This is an extremely bad situation, somehow we have a tag without | |||
|
101 | # any alias | |||
|
102 | return 'BROKEN #{}'.format(self.id) | |||
93 |
|
103 | |||
94 | def __str__(self): |
|
104 | def __str__(self): | |
95 | return self.get_name() |
|
105 | return self.get_name() | |
96 |
|
106 | |||
97 | def is_empty(self) -> bool: |
|
107 | def is_empty(self) -> bool: | |
98 | """ |
|
108 | """ | |
99 | Checks if the tag has some threads. |
|
109 | Checks if the tag has some threads. | |
100 | """ |
|
110 | """ | |
101 |
|
111 | |||
102 | return self.get_thread_count() == 0 |
|
112 | return self.get_thread_count() == 0 | |
103 |
|
113 | |||
104 | def get_thread_count(self, status=None) -> int: |
|
114 | def get_thread_count(self, status=None) -> int: | |
105 | threads = self.get_threads() |
|
115 | threads = self.get_threads() | |
106 | if status is not None: |
|
116 | if status is not None: | |
107 | threads = threads.filter(status=status) |
|
117 | threads = threads.filter(status=status) | |
108 | return threads.count() |
|
118 | return threads.count() | |
109 |
|
119 | |||
110 | def get_active_thread_count(self) -> int: |
|
120 | def get_active_thread_count(self) -> int: | |
111 | return self.get_thread_count(status=STATUS_ACTIVE) |
|
121 | return self.get_thread_count(status=STATUS_ACTIVE) | |
112 |
|
122 | |||
113 | def get_bumplimit_thread_count(self) -> int: |
|
123 | def get_bumplimit_thread_count(self) -> int: | |
114 | return self.get_thread_count(status=STATUS_BUMPLIMIT) |
|
124 | return self.get_thread_count(status=STATUS_BUMPLIMIT) | |
115 |
|
125 | |||
116 | def get_archived_thread_count(self) -> int: |
|
126 | def get_archived_thread_count(self) -> int: | |
117 | return self.get_thread_count(status=STATUS_ARCHIVE) |
|
127 | return self.get_thread_count(status=STATUS_ARCHIVE) | |
118 |
|
128 | |||
119 | @cached_result() |
|
129 | @cached_result() | |
120 | def get_absolute_url(self): |
|
130 | def get_absolute_url(self): | |
121 | return reverse('tag', kwargs={'tag_name': self.get_name()}) |
|
131 | return reverse('tag', kwargs={'tag_name': self.get_name()}) | |
122 |
|
132 | |||
123 | def get_threads(self): |
|
133 | def get_threads(self): | |
124 | return self.thread_tags.order_by('-bump_time') |
|
134 | return self.thread_tags.order_by('-bump_time') | |
125 |
|
135 | |||
126 | def is_required(self): |
|
136 | def is_required(self): | |
127 | return self.required |
|
137 | return self.required | |
128 |
|
138 | |||
129 | def _get_locale_cache_key(self): |
|
139 | def _get_locale_cache_key(self): | |
130 | return [get_language()] |
|
140 | return [get_language()] | |
131 |
|
141 | |||
132 | @cached_result(key_method=_get_locale_cache_key) |
|
142 | @cached_result(key_method=_get_locale_cache_key) | |
133 | def get_localized_name(self): |
|
143 | def get_localized_name(self): | |
134 | locale = get_language() |
|
144 | locale = get_language() | |
135 |
|
145 | |||
136 | aliases = self.aliases.filter(Q(locale=locale) | Q(locale=DEFAULT_LOCALE)) |
|
146 | aliases = self.aliases.filter(Q(locale=locale) | Q(locale=DEFAULT_LOCALE)) | |
137 |
|
147 | |||
138 | localized_tag_name = None |
|
148 | localized_tag_name = None | |
139 | default_tag_name = None |
|
149 | default_tag_name = None | |
140 |
|
150 | |||
141 | for alias in aliases: |
|
151 | for alias in aliases: | |
142 | if alias.locale == locale: |
|
152 | if alias.locale == locale: | |
143 | localized_tag_name = alias.name |
|
153 | localized_tag_name = alias.name | |
144 | elif alias.locale == DEFAULT_LOCALE: |
|
154 | elif alias.locale == DEFAULT_LOCALE: | |
145 | default_tag_name = alias.name |
|
155 | default_tag_name = alias.name | |
146 |
|
156 | |||
147 | return localized_tag_name or default_tag_name |
|
157 | return localized_tag_name or default_tag_name | |
148 |
|
158 | |||
149 | def get_view(self): |
|
159 | def get_view(self): | |
150 | name = self.get_localized_name() |
|
160 | name = self.get_localized_name() | |
151 | link = '<a class="tag" href="{}">{}</a>'.format( |
|
161 | link = '<a class="tag" href="{}">{}</a>'.format( | |
152 | self.get_absolute_url(), name) |
|
162 | self.get_absolute_url(), name) | |
153 | if self.is_required(): |
|
163 | if self.is_required(): | |
154 | link = '<b>{}</b>'.format(link) |
|
164 | link = '<b>{}</b>'.format(link) | |
155 | return link |
|
165 | return link | |
156 |
|
166 | |||
157 | @cached_result() |
|
167 | @cached_result() | |
158 | def get_post_count(self): |
|
168 | def get_post_count(self): | |
159 | return self.get_threads().aggregate(num_posts=Count('replies'))['num_posts'] |
|
169 | return self.get_threads().aggregate(num_posts=Count('replies'))['num_posts'] | |
160 |
|
170 | |||
161 | def get_description(self): |
|
171 | def get_description(self): | |
162 | return self.description |
|
172 | return self.description | |
163 |
|
173 | |||
164 | def get_random_image_post(self, status=[STATUS_ACTIVE, STATUS_BUMPLIMIT]): |
|
174 | def get_random_image_post(self, status=[STATUS_ACTIVE, STATUS_BUMPLIMIT]): | |
165 | posts = boards.models.Post.objects.filter(attachments__mimetype__in=FILE_TYPES_IMAGE)\ |
|
175 | posts = boards.models.Post.objects.filter(attachments__mimetype__in=FILE_TYPES_IMAGE)\ | |
166 | .annotate(images_count=Count( |
|
176 | .annotate(images_count=Count( | |
167 | 'attachments')).filter(images_count__gt=0, thread__tags__in=[self]) |
|
177 | 'attachments')).filter(images_count__gt=0, thread__tags__in=[self]) | |
168 | if status is not None: |
|
178 | if status is not None: | |
169 | posts = posts.filter(thread__status__in=status) |
|
179 | posts = posts.filter(thread__status__in=status) | |
170 | return posts.order_by('?').first() |
|
180 | return posts.order_by('?').first() | |
171 |
|
181 | |||
172 | def get_related_tags(self): |
|
182 | def get_related_tags(self): | |
173 | return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( |
|
183 | return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( | |
174 | id=self.id).order_by('?')[:RELATED_TAGS_COUNT]) |
|
184 | id=self.id).order_by('?')[:RELATED_TAGS_COUNT]) | |
175 |
|
185 | |||
176 | @cached_result() |
|
186 | @cached_result() | |
177 | def get_color(self): |
|
187 | def get_color(self): | |
178 | """ |
|
188 | """ | |
179 | Gets color hashed from the tag name. |
|
189 | Gets color hashed from the tag name. | |
180 | """ |
|
190 | """ | |
181 | return hashlib.md5(self.get_name().encode()).hexdigest()[:6] |
|
191 | return hashlib.md5(self.get_name().encode()).hexdigest()[:6] | |
182 |
|
192 | |||
183 | def get_parent(self): |
|
193 | def get_parent(self): | |
184 | return self.parent |
|
194 | return self.parent | |
185 |
|
195 | |||
186 | def get_all_parents(self): |
|
196 | def get_all_parents(self): | |
187 | parents = list() |
|
197 | parents = list() | |
188 | parent = self.get_parent() |
|
198 | parent = self.get_parent() | |
189 | if parent and parent not in parents: |
|
199 | if parent and parent not in parents: | |
190 | parents.insert(0, parent) |
|
200 | parents.insert(0, parent) | |
191 | parents = parent.get_all_parents() + parents |
|
201 | parents = parent.get_all_parents() + parents | |
192 |
|
202 | |||
193 | return parents |
|
203 | return parents | |
194 |
|
204 | |||
195 | def get_children(self): |
|
205 | def get_children(self): | |
196 | return self.children |
|
206 | return self.children | |
197 |
|
207 | |||
198 | def get_images(self): |
|
208 | def get_images(self): | |
199 | return Attachment.objects.filter( |
|
209 | return Attachment.objects.filter( | |
200 | attachment_posts__thread__tags__in=[self]).filter( |
|
210 | attachment_posts__thread__tags__in=[self]).filter( | |
201 | mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') |
|
211 | mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') |
General Comments 0
You need to be logged in to leave comments.
Login now