|
|
from django.template.loader import render_to_string
|
|
|
from django.db import models
|
|
|
from django.db.models import Count
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
from boards.models.base import Viewable
|
|
|
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
class TagManager(models.Manager):
|
|
|
|
|
|
def get_not_empty_tags(self):
|
|
|
"""
|
|
|
Gets tags that have non-archived threads.
|
|
|
"""
|
|
|
|
|
|
return self.filter(thread__archived=False)\
|
|
|
.annotate(num_threads=Count('thread')).filter(num_threads__gt=0)\
|
|
|
.order_by('-required', 'name')
|
|
|
|
|
|
|
|
|
class Tag(models.Model, Viewable):
|
|
|
"""
|
|
|
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
|
|
|
"""
|
|
|
|
|
|
objects = TagManager()
|
|
|
|
|
|
class Meta:
|
|
|
app_label = 'boards'
|
|
|
ordering = ('name',)
|
|
|
|
|
|
name = models.CharField(max_length=100, db_index=True)
|
|
|
required = models.BooleanField(default=False)
|
|
|
|
|
|
def __str__(self):
|
|
|
return self.name
|
|
|
|
|
|
def is_empty(self) -> bool:
|
|
|
"""
|
|
|
Checks if the tag has some threads.
|
|
|
"""
|
|
|
|
|
|
return self.get_thread_count() == 0
|
|
|
|
|
|
def get_thread_count(self) -> int:
|
|
|
return self.get_threads().count()
|
|
|
|
|
|
def get_url(self):
|
|
|
return reverse('tag', kwargs={'tag_name': self.name})
|
|
|
|
|
|
def get_threads(self):
|
|
|
return self.thread_set.order_by('-bump_time')
|
|
|
|
|
|
def is_required(self):
|
|
|
return self.required
|
|
|
|
|
|
def get_view(self):
|
|
|
link = '<a class="tag" href="{}">{}</a>'.format(
|
|
|
self.get_url(), self.name)
|
|
|
if self.is_required():
|
|
|
link = '<b>{}</b>'.format(link)
|
|
|
return link
|
|
|
|
|
|
def get_search_view(self, *args, **kwargs):
|
|
|
return render_to_string('boards/tag.html', {
|
|
|
'tag': self,
|
|
|
})
|
|
|
|
|
|
def get_post_count(self):
|
|
|
return self.get_threads().aggregate(num_posts=Count('post'))['num_posts']
|
|
|
|