tag.py
211 lines
| 6.2 KiB
| text/x-python
|
PythonLexer
|
r1338 | import hashlib | ||
|
r1860 | import re | ||
|
r1590 | from boards.models.attachment import FILE_TYPES_IMAGE | ||
|
r692 | from django.template.loader import render_to_string | ||
|
r386 | from django.db import models | ||
|
r1889 | from django.db.models import Count, Q | ||
|
r692 | from django.core.urlresolvers import reverse | ||
|
r1860 | from django.utils.translation import get_language | ||
|
r732 | |||
|
r1590 | from boards.models import Attachment | ||
|
r692 | from boards.models.base import Viewable | ||
|
r1414 | from boards.models.thread import STATUS_ACTIVE, STATUS_BUMPLIMIT, STATUS_ARCHIVE | ||
|
r973 | from boards.utils import cached_result | ||
|
r1264 | import boards | ||
|
r732 | |||
|
r385 | __author__ = 'neko259' | ||
|
r1270 | RELATED_TAGS_COUNT = 5 | ||
|
r1874 | DEFAULT_LOCALE = 'default' | ||
|
r1270 | |||
|
r1862 | |||
|
r1891 | class TagAliasManager(models.Manager): | ||
def filter_localized(self, *args, **kwargs): | ||||
locale = get_language() | ||||
tag_aliases = (self.filter(locale=locale) | ||||
| self.filter(Q(locale=DEFAULT_LOCALE) | ||||
& ~Q(parent__aliases__locale=locale))) | ||||
return tag_aliases.filter(**kwargs) | ||||
|
r1862 | class TagAlias(models.Model, Viewable): | ||
class Meta: | ||||
app_label = 'boards' | ||||
ordering = ('name',) | ||||
|
r1891 | objects = TagAliasManager() | ||
|
r1873 | name = models.CharField(max_length=100, db_index=True) | ||
|
r1862 | locale = models.CharField(max_length=10, db_index=True) | ||
parent = models.ForeignKey('Tag', null=True, blank=True, | ||||
related_name='aliases') | ||||
|
r1860 | |||
|
r1270 | |||
|
r385 | class TagManager(models.Manager): | ||
def get_not_empty_tags(self): | ||||
|
r623 | """ | ||
Gets tags that have non-archived threads. | ||||
""" | ||||
|
r1874 | return self.annotate(num_threads=Count('thread_tags'))\ | ||
.filter(num_threads__gt=0)\ | ||||
.filter(aliases__locale=DEFAULT_LOCALE)\ | ||||
.order_by('aliases__name') | ||||
|
r385 | |||
|
r1027 | 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]) | ||||
|
r1860 | def get_by_alias(self, alias): | ||
|
r1862 | tag = None | ||
|
r1873 | aliases = TagAlias.objects.filter(name=alias).all() | ||
if aliases: | ||||
tag = aliases[0].parent | ||||
|
r1862 | return tag | ||
|
r1860 | |||
|
r1874 | def get_or_create_with_alias(self, name, required=False): | ||
tag = self.get_by_alias(name) | ||||
created = False | ||||
if not tag: | ||||
tag = self.create(required=required) | ||||
|
r1883 | TagAlias.objects.create(name=name, locale=DEFAULT_LOCALE, parent=tag) | ||
|
r1874 | created = True | ||
return tag, created | ||||
|
r385 | |||
|
r692 | class Tag(models.Model, Viewable): | ||
|
r385 | """ | ||
|
r398 | A tag is a text node assigned to the thread. The tag serves as a board | ||
section. There can be multiple tags for each thread | ||||
|
r385 | """ | ||
objects = TagManager() | ||||
class Meta: | ||||
app_label = 'boards' | ||||
|
r983 | required = models.BooleanField(default=False, db_index=True) | ||
|
r1258 | description = models.TextField(blank=True) | ||
|
r385 | |||
|
r1367 | parent = models.ForeignKey('Tag', null=True, blank=True, | ||
related_name='children') | ||||
|
r1348 | |||
|
r1874 | def get_name(self): | ||
return self.aliases.get(locale=DEFAULT_LOCALE).name | ||||
|
r875 | def __str__(self): | ||
|
r1874 | return self.get_name() | ||
|
r385 | |||
|
r908 | def is_empty(self) -> bool: | ||
|
r623 | """ | ||
Checks if the tag has some threads. | ||||
""" | ||||
|
r606 | return self.get_thread_count() == 0 | ||
|
r385 | |||
|
r1414 | def get_thread_count(self, status=None) -> int: | ||
|
r1260 | threads = self.get_threads() | ||
|
r1414 | if status is not None: | ||
threads = threads.filter(status=status) | ||||
|
r1260 | return threads.count() | ||
def get_active_thread_count(self) -> int: | ||||
|
r1414 | return self.get_thread_count(status=STATUS_ACTIVE) | ||
|
r1383 | |||
def get_bumplimit_thread_count(self) -> int: | ||||
|
r1414 | return self.get_thread_count(status=STATUS_BUMPLIMIT) | ||
|
r385 | |||
|
r1364 | def get_archived_thread_count(self) -> int: | ||
|
r1414 | return self.get_thread_count(status=STATUS_ARCHIVE) | ||
|
r1364 | |||
|
r1160 | def get_absolute_url(self): | ||
|
r1874 | return reverse('tag', kwargs={'tag_name': self.get_name()}) | ||
|
r692 | |||
|
r908 | def get_threads(self): | ||
|
r1269 | return self.thread_tags.order_by('-bump_time') | ||
|
r922 | |||
def is_required(self): | ||||
return self.required | ||||
|
r1888 | def _get_locale_cache_key(self): | ||
return '{}_{}'.format(self.id, get_language()) | ||||
@cached_result(key_method=_get_locale_cache_key) | ||||
|
r922 | def get_view(self): | ||
|
r1860 | locale = get_language() | ||
|
r1889 | aliases = self.aliases.filter(Q(locale=locale) | Q(locale=DEFAULT_LOCALE)) | ||
localized_tag_name = None | ||||
default_tag_name = None | ||||
|
r1860 | |||
|
r1889 | for alias in aliases: | ||
if alias.locale == locale: | ||||
localized_tag_name = alias.name | ||||
elif alias.locale == DEFAULT_LOCALE: | ||||
default_tag_name = alias.name | ||||
|
r1874 | |||
|
r1891 | name = localized_tag_name if localized_tag_name else default_tag_name | ||
|
r922 | link = '<a class="tag" href="{}">{}</a>'.format( | ||
|
r1861 | self.get_absolute_url(), name) | ||
|
r922 | if self.is_required(): | ||
link = '<b>{}</b>'.format(link) | ||||
return link | ||||
|
r1107 | @cached_result() | ||
|
r960 | def get_post_count(self): | ||
|
r1704 | return self.get_threads().aggregate(num_posts=Count('replies'))['num_posts'] | ||
|
r1258 | |||
def get_description(self): | ||||
return self.description | ||||
|
r1264 | |||
|
r1417 | def get_random_image_post(self, status=[STATUS_ACTIVE, STATUS_BUMPLIMIT]): | ||
|
r1590 | posts = boards.models.Post.objects.filter(attachments__mimetype__in=FILE_TYPES_IMAGE)\ | ||
.annotate(images_count=Count( | ||||
|
r1704 | 'attachments')).filter(images_count__gt=0, thread__tags__in=[self]) | ||
|
r1414 | if status is not None: | ||
|
r1417 | posts = posts.filter(thread__status__in=status) | ||
|
r1265 | return posts.order_by('?').first() | ||
|
r1264 | |||
|
r1267 | def get_first_letter(self): | ||
|
r1874 | name = self.get_name() | ||
return name and name[0] or '' | ||||
|
r1269 | |||
def get_related_tags(self): | ||||
|
r1270 | return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( | ||
|
r1338 | id=self.id).order_by('?')[:RELATED_TAGS_COUNT]) | ||
@cached_result() | ||||
def get_color(self): | ||||
""" | ||||
Gets color hashed from the tag name. | ||||
""" | ||||
|
r1874 | return hashlib.md5(self.get_name().encode()).hexdigest()[:6] | ||
|
r1348 | |||
def get_parent(self): | ||||
return self.parent | ||||
def get_all_parents(self): | ||||
|
r1361 | parents = list() | ||
|
r1348 | parent = self.get_parent() | ||
if parent and parent not in parents: | ||||
|
r1361 | parents.insert(0, parent) | ||
parents = parent.get_all_parents() + parents | ||||
|
r1348 | |||
return parents | ||||
def get_children(self): | ||||
return self.children | ||||
|
r1419 | |||
def get_images(self): | ||||
|
r1590 | return Attachment.objects.filter( | ||
|
r1591 | attachment_posts__thread__tags__in=[self]).filter( | ||
|
r1729 | mimetype__in=FILE_TYPES_IMAGE).order_by('-attachment_posts__pub_time') | ||
|
r1862 | |||