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