##// END OF EJS Templates
Merged with default
Merged with default

File last commit:

r1270:ba7bc8db default
r1320:c450e81f merge decentral
Show More
tag.py
109 lines | 3.1 KiB | text/x-python | PythonLexer
neko259
Added tag search. Refactored search to show any model results in a list.
r692 from django.template.loader import render_to_string
neko259
Split up user models
r386 from django.db import models
neko259
Optimized tag DB queries
r928 from django.db.models import Count
neko259
Added tag search. Refactored search to show any model results in a list.
r692 from django.core.urlresolvers import reverse
neko259
Removed obsolete import
r732
neko259
Added tag search. Refactored search to show any model results in a list.
r692 from boards.models.base import Viewable
neko259
Cache tag post count
r973 from boards.utils import cached_result
neko259
Speed up getting tag's random image
r1264 import boards
neko259
Removed obsolete import
r732
neko259
Split up tag module from post module
r385 __author__ = 'neko259'
neko259
Add up to 5 random related tags instead of all related list
r1270 RELATED_TAGS_COUNT = 5
neko259
Split up tag module from post module
r385 class TagManager(models.Manager):
def get_not_empty_tags(self):
neko259
Small updates to the tag model docstrings.
r623 """
Gets tags that have non-archived threads.
"""
neko259
Show related tags in the tag page
r1269 return self.annotate(num_threads=Count('thread_tags')).filter(num_threads__gt=0)\
neko259
Fixed tag ordering
r932 .order_by('-required', 'name')
neko259
Split up tag module from post module
r385
neko259
Refactoring
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])
neko259
Split up tag module from post module
r385
neko259
Added tag search. Refactored search to show any model results in a list.
r692 class Tag(models.Model, Viewable):
neko259
Split up tag module from post module
r385 """
neko259
Split up post model into post and thread to normalise models. Still need some refactoring
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
neko259
Split up tag module from post module
r385 """
objects = TagManager()
class Meta:
app_label = 'boards'
neko259
Optimized thread view and threads list
r649 ordering = ('name',)
neko259
Split up tag module from post module
r385
neko259
Made tag name field unique. Added index to "required" field for tag
r983 name = models.CharField(max_length=100, db_index=True, unique=True)
required = models.BooleanField(default=False, db_index=True)
neko259
Added tag description
r1258 description = models.TextField(blank=True)
neko259
Split up tag module from post module
r385
neko259
Show thread, post and tag names in admin with python3
r875 def __str__(self):
neko259
Split up tag module from post module
r385 return self.name
neko259
Use threads' tags list to aggregate threads by tag
r908 def is_empty(self) -> bool:
neko259
Small updates to the tag model docstrings.
r623 """
Checks if the tag has some threads.
"""
neko259
Speed up tag list generation a bit
r606 return self.get_thread_count() == 0
neko259
Split up tag module from post module
r385
neko259
Show active threads count in the tag info
r1260 def get_thread_count(self, archived=None) -> int:
threads = self.get_threads()
if archived is not None:
threads = threads.filter(archived=archived)
return threads.count()
def get_active_thread_count(self) -> int:
return self.get_thread_count(archived=False)
neko259
Split up tag module from post module
r385
neko259
Use get_absolute_url instead of get_url for post, tag and thread
r1160 def get_absolute_url(self):
neko259
Added tag search. Refactored search to show any model results in a list.
r692 return reverse('tag', kwargs={'tag_name': self.name})
neko259
Use threads' tags list to aggregate threads by tag
r908 def get_threads(self):
neko259
Show related tags in the tag page
r1269 return self.thread_tags.order_by('-bump_time')
neko259
Added required tags. At least one such tag is needed to create a thread. All...
r922
def is_required(self):
return self.required
def get_view(self):
link = '<a class="tag" href="{}">{}</a>'.format(
neko259
Added "view on site" link for tag
r1150 self.get_absolute_url(), self.name)
neko259
Added required tags. At least one such tag is needed to create a thread. All...
r922 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', {
neko259
Optimized tag DB queries
r928 'tag': self,
})
neko259
Removed TODO as html5 does not support "s" and "strike" tags any more
r960
neko259
Fixed tag post count cache
r1107 @cached_result()
neko259
Removed TODO as html5 does not support "s" and "strike" tags any more
r960 def get_post_count(self):
return self.get_threads().aggregate(num_posts=Count('post'))['num_posts']
neko259
Added tag description
r1258
def get_description(self):
return self.description
neko259
Speed up getting tag's random image
r1264
neko259
Don't include archived posts in the tag images
r1265 def get_random_image_post(self, archived=False):
posts = boards.models.Post.objects.annotate(images_count=Count(
'images')).filter(images_count__gt=0, threads__tags__in=[self])
if archived is not None:
posts = posts.filter(thread__archived=archived)
return posts.order_by('?').first()
neko259
Speed up getting tag's random image
r1264
neko259
Added first letter sections to the tag list
r1267 def get_first_letter(self):
return self.name and self.name[0] or ''
neko259
Show related tags in the tag page
r1269
def get_related_tags(self):
neko259
Add up to 5 random related tags instead of all related list
r1270 return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude(
id=self.id).order_by('?')[:RELATED_TAGS_COUNT])