##// END OF EJS Templates
Fixed tag ordering
neko259 -
r932:f79c8862 default
parent child Browse files
Show More
@@ -1,71 +1,71 b''
1 from django.template.loader import render_to_string
1 from django.template.loader import render_to_string
2 from django.db import models
2 from django.db import models
3 from django.db.models import Count
3 from django.db.models import Count
4 from django.core.urlresolvers import reverse
4 from django.core.urlresolvers import reverse
5
5
6 from boards.models.base import Viewable
6 from boards.models.base import Viewable
7
7
8
8
9 __author__ = 'neko259'
9 __author__ = 'neko259'
10
10
11
11
12 class TagManager(models.Manager):
12 class TagManager(models.Manager):
13
13
14 def get_not_empty_tags(self):
14 def get_not_empty_tags(self):
15 """
15 """
16 Gets tags that have non-archived threads.
16 Gets tags that have non-archived threads.
17 """
17 """
18
18
19 return self.filter(thread__archived=False)\
19 return self.filter(thread__archived=False)\
20 .annotate(num_threads=Count('thread')).filter(num_threads__gt=0)\
20 .annotate(num_threads=Count('thread')).filter(num_threads__gt=0)\
21 .order_by('-required')
21 .order_by('-required', 'name')
22
22
23
23
24 class Tag(models.Model, Viewable):
24 class Tag(models.Model, Viewable):
25 """
25 """
26 A tag is a text node assigned to the thread. The tag serves as a board
26 A tag is a text node assigned to the thread. The tag serves as a board
27 section. There can be multiple tags for each thread
27 section. There can be multiple tags for each thread
28 """
28 """
29
29
30 objects = TagManager()
30 objects = TagManager()
31
31
32 class Meta:
32 class Meta:
33 app_label = 'boards'
33 app_label = 'boards'
34 ordering = ('name',)
34 ordering = ('name',)
35
35
36 name = models.CharField(max_length=100, db_index=True)
36 name = models.CharField(max_length=100, db_index=True)
37 required = models.BooleanField(default=False)
37 required = models.BooleanField(default=False)
38
38
39 def __str__(self):
39 def __str__(self):
40 return self.name
40 return self.name
41
41
42 def is_empty(self) -> bool:
42 def is_empty(self) -> bool:
43 """
43 """
44 Checks if the tag has some threads.
44 Checks if the tag has some threads.
45 """
45 """
46
46
47 return self.get_thread_count() == 0
47 return self.get_thread_count() == 0
48
48
49 def get_thread_count(self) -> int:
49 def get_thread_count(self) -> int:
50 return self.get_threads().count()
50 return self.get_threads().count()
51
51
52 def get_url(self):
52 def get_url(self):
53 return reverse('tag', kwargs={'tag_name': self.name})
53 return reverse('tag', kwargs={'tag_name': self.name})
54
54
55 def get_threads(self):
55 def get_threads(self):
56 return self.thread_set.order_by('-bump_time')
56 return self.thread_set.order_by('-bump_time')
57
57
58 def is_required(self):
58 def is_required(self):
59 return self.required
59 return self.required
60
60
61 def get_view(self):
61 def get_view(self):
62 link = '<a class="tag" href="{}">{}</a>'.format(
62 link = '<a class="tag" href="{}">{}</a>'.format(
63 self.get_url(), self.name)
63 self.get_url(), self.name)
64 if self.is_required():
64 if self.is_required():
65 link = '<b>{}</b>'.format(link)
65 link = '<b>{}</b>'.format(link)
66 return link
66 return link
67
67
68 def get_search_view(self, *args, **kwargs):
68 def get_search_view(self, *args, **kwargs):
69 return render_to_string('boards/tag.html', {
69 return render_to_string('boards/tag.html', {
70 'tag': self,
70 'tag': self,
71 })
71 })
General Comments 0
You need to be logged in to leave comments. Login now