##// END OF EJS Templates
Simplify some expressions
neko259 -
r2022:cab2634a default
parent child Browse files
Show More
@@ -11,9 +11,11 from boards.models.base import Viewable
11 11 from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE
12 12 from boards.utils import cached_result
13 13
14
14 15 __author__ = 'neko259'
15 16
16 17
18 TAG_DELIMITER = ', '
17 19 RELATED_TAGS_COUNT = 5
18 20 DEFAULT_LOCALE = 'default'
19 21
@@ -42,30 +44,18 class TagAlias(models.Model, Viewable):
42 44
43 45
44 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 47 def get_tag_url_list(self, tags: list) -> str:
56 48 """
57 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 54 def get_by_alias(self, alias):
63 tag = None
64 aliases = TagAlias.objects.filter(name=alias).all()
65 if aliases:
66 tag = aliases[0].parent
67
68 return tag
55 try:
56 return self.get(aliases__name__in=alias)
57 except Tag.DoesNotExist:
58 pass
69 59
70 60 def get_or_create_with_alias(self, name, required=False):
71 61 tag = self.get_by_alias(name)
@@ -150,7 +140,7 class Tag(models.Model, Viewable):
150 140 elif alias.locale == DEFAULT_LOCALE:
151 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 145 def get_view(self):
156 146 name = self.get_localized_name()
@@ -175,10 +165,6 class Tag(models.Model, Viewable):
175 165 posts = posts.filter(thread__status__in=status)
176 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 168 def get_related_tags(self):
183 169 return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude(
184 170 id=self.id).order_by('?')[:RELATED_TAGS_COUNT])
@@ -209,4 +195,3 class Tag(models.Model, Viewable):
209 195 return Attachment.objects.filter(
210 196 attachment_posts__thread__tags__in=[self]).filter(
211 197 mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time')
212
General Comments 0
You need to be logged in to leave comments. Login now