##// END OF EJS Templates
Simplify some expressions
neko259 -
r2022:cab2634a default
parent child Browse files
Show More
@@ -11,9 +11,11 b' 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 __author__ = 'neko259'
15 __author__ = 'neko259'
15
16
16
17
18 TAG_DELIMITER = ', '
17 RELATED_TAGS_COUNT = 5
19 RELATED_TAGS_COUNT = 5
18 DEFAULT_LOCALE = 'default'
20 DEFAULT_LOCALE = 'default'
19
21
@@ -42,30 +44,18 b' class TagAlias(models.Model, Viewable):'
42
44
43
45
44 class TagManager(models.Manager):
46 class TagManager(models.Manager):
45 def get_not_empty_tags(self):
46 """
47 Gets tags that have non-archived threads.
48 """
49
50 return self.annotate(num_threads=Count('thread_tags'))\
51 .filter(num_threads__gt=0)\
52 .filter(aliases__in=TagAlias.objects.filter_localized())\
53 .order_by('aliases__name')
54
55 def get_tag_url_list(self, tags: list) -> str:
47 def get_tag_url_list(self, tags: list) -> str:
56 """
48 """
57 Gets a comma-separated list of tag links.
49 Gets a comma-separated list of tag links.
58 """
50 """
59
51
60 return ', '.join([tag.get_view() for tag in tags])
52 return TAG_DELIMITER.join([tag.get_view() for tag in tags])
61
53
62 def get_by_alias(self, alias):
54 def get_by_alias(self, alias):
63 tag = None
55 try:
64 aliases = TagAlias.objects.filter(name=alias).all()
56 return self.get(aliases__name__in=alias)
65 if aliases:
57 except Tag.DoesNotExist:
66 tag = aliases[0].parent
58 pass
67
68 return tag
69
59
70 def get_or_create_with_alias(self, name, required=False):
60 def get_or_create_with_alias(self, name, required=False):
71 tag = self.get_by_alias(name)
61 tag = self.get_by_alias(name)
@@ -150,7 +140,7 b' class Tag(models.Model, Viewable):'
150 elif alias.locale == DEFAULT_LOCALE:
140 elif alias.locale == DEFAULT_LOCALE:
151 default_tag_name = alias.name
141 default_tag_name = alias.name
152
142
153 return localized_tag_name if localized_tag_name else default_tag_name
143 return localized_tag_name or default_tag_name
154
144
155 def get_view(self):
145 def get_view(self):
156 name = self.get_localized_name()
146 name = self.get_localized_name()
@@ -175,10 +165,6 b' class Tag(models.Model, Viewable):'
175 posts = posts.filter(thread__status__in=status)
165 posts = posts.filter(thread__status__in=status)
176 return posts.order_by('?').first()
166 return posts.order_by('?').first()
177
167
178 def get_first_letter(self):
179 name = self.get_localized_name()
180 return name and name[0] or ''
181
182 def get_related_tags(self):
168 def get_related_tags(self):
183 return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude(
169 return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude(
184 id=self.id).order_by('?')[:RELATED_TAGS_COUNT])
170 id=self.id).order_by('?')[:RELATED_TAGS_COUNT])
@@ -209,4 +195,3 b' class Tag(models.Model, Viewable):'
209 return Attachment.objects.filter(
195 return Attachment.objects.filter(
210 attachment_posts__thread__tags__in=[self]).filter(
196 attachment_posts__thread__tags__in=[self]).filter(
211 mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time')
197 mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time')
212
@@ -312,15 +312,15 b' class Thread(models.Model):'
312 searching_for_index = True
312 searching_for_index = True
313 parent_depth = depth
313 parent_depth = depth
314
314
315 if not found_parent:
315 if not found_parent:
316 tree.append((0, reply))
316 tree.append((0, reply))
317 else:
317 else:
318 if searching_for_index:
318 if searching_for_index:
319 tree.append((parent_depth + 1, reply))
319 tree.append((parent_depth + 1, reply))
320
320
321 offset = 0
321 offset = 0
322 for last_index, parent_depth in indexes_to_insert:
322 for last_index, parent_depth in indexes_to_insert:
323 tree.insert(last_index + offset, (parent_depth + 1, reply))
323 tree.insert(last_index + offset, (parent_depth + 1, reply))
324 offset += 1
324 offset += 1
325
325
326 return tree
326 return tree
General Comments 0
You need to be logged in to leave comments. Login now