diff --git a/boards/models/tag.py b/boards/models/tag.py --- a/boards/models/tag.py +++ b/boards/models/tag.py @@ -11,9 +11,11 @@ from boards.models.base import Viewable from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE from boards.utils import cached_result + __author__ = 'neko259' +TAG_DELIMITER = ', ' RELATED_TAGS_COUNT = 5 DEFAULT_LOCALE = 'default' @@ -42,30 +44,18 @@ class TagAlias(models.Model, Viewable): class TagManager(models.Manager): - def get_not_empty_tags(self): - """ - Gets tags that have non-archived threads. - """ - - return self.annotate(num_threads=Count('thread_tags'))\ - .filter(num_threads__gt=0)\ - .filter(aliases__in=TagAlias.objects.filter_localized())\ - .order_by('aliases__name') - def get_tag_url_list(self, tags: list) -> str: """ Gets a comma-separated list of tag links. """ - return ', '.join([tag.get_view() for tag in tags]) + return TAG_DELIMITER.join([tag.get_view() for tag in tags]) def get_by_alias(self, alias): - tag = None - aliases = TagAlias.objects.filter(name=alias).all() - if aliases: - tag = aliases[0].parent - - return tag + try: + return self.get(aliases__name__in=alias) + except Tag.DoesNotExist: + pass def get_or_create_with_alias(self, name, required=False): tag = self.get_by_alias(name) @@ -150,7 +140,7 @@ class Tag(models.Model, Viewable): elif alias.locale == DEFAULT_LOCALE: default_tag_name = alias.name - return localized_tag_name if localized_tag_name else default_tag_name + return localized_tag_name or default_tag_name def get_view(self): name = self.get_localized_name() @@ -175,10 +165,6 @@ class Tag(models.Model, Viewable): posts = posts.filter(thread__status__in=status) return posts.order_by('?').first() - def get_first_letter(self): - name = self.get_localized_name() - return name and name[0] or '' - def get_related_tags(self): return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( id=self.id).order_by('?')[:RELATED_TAGS_COUNT]) @@ -209,4 +195,3 @@ class Tag(models.Model, Viewable): return Attachment.objects.filter( attachment_posts__thread__tags__in=[self]).filter( mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') - diff --git a/boards/models/thread.py b/boards/models/thread.py --- a/boards/models/thread.py +++ b/boards/models/thread.py @@ -312,15 +312,15 @@ class Thread(models.Model): searching_for_index = True parent_depth = depth - if not found_parent: - tree.append((0, reply)) - else: - if searching_for_index: - tree.append((parent_depth + 1, reply)) + if not found_parent: + tree.append((0, reply)) + else: + if searching_for_index: + tree.append((parent_depth + 1, reply)) - offset = 0 - for last_index, parent_depth in indexes_to_insert: - tree.insert(last_index + offset, (parent_depth + 1, reply)) - offset += 1 + offset = 0 + for last_index, parent_depth in indexes_to_insert: + tree.insert(last_index + offset, (parent_depth + 1, reply)) + offset += 1 return tree