Show More
@@ -1,106 +1,109 b'' | |||
|
1 | 1 | from django.template.loader import render_to_string |
|
2 | 2 | from django.db import models |
|
3 | 3 | from django.db.models import Count |
|
4 | 4 | from django.core.urlresolvers import reverse |
|
5 | 5 | |
|
6 | 6 | from boards.models.base import Viewable |
|
7 | 7 | from boards.utils import cached_result |
|
8 | 8 | import boards |
|
9 | 9 | |
|
10 | 10 | __author__ = 'neko259' |
|
11 | 11 | |
|
12 | 12 | |
|
13 | RELATED_TAGS_COUNT = 5 | |
|
14 | ||
|
15 | ||
|
13 | 16 | class TagManager(models.Manager): |
|
14 | 17 | |
|
15 | 18 | def get_not_empty_tags(self): |
|
16 | 19 | """ |
|
17 | 20 | Gets tags that have non-archived threads. |
|
18 | 21 | """ |
|
19 | 22 | |
|
20 | 23 | return self.annotate(num_threads=Count('thread_tags')).filter(num_threads__gt=0)\ |
|
21 | 24 | .order_by('-required', 'name') |
|
22 | 25 | |
|
23 | 26 | def get_tag_url_list(self, tags: list) -> str: |
|
24 | 27 | """ |
|
25 | 28 | Gets a comma-separated list of tag links. |
|
26 | 29 | """ |
|
27 | 30 | |
|
28 | 31 | return ', '.join([tag.get_view() for tag in tags]) |
|
29 | 32 | |
|
30 | 33 | |
|
31 | 34 | class Tag(models.Model, Viewable): |
|
32 | 35 | """ |
|
33 | 36 | A tag is a text node assigned to the thread. The tag serves as a board |
|
34 | 37 | section. There can be multiple tags for each thread |
|
35 | 38 | """ |
|
36 | 39 | |
|
37 | 40 | objects = TagManager() |
|
38 | 41 | |
|
39 | 42 | class Meta: |
|
40 | 43 | app_label = 'boards' |
|
41 | 44 | ordering = ('name',) |
|
42 | 45 | |
|
43 | 46 | name = models.CharField(max_length=100, db_index=True, unique=True) |
|
44 | 47 | required = models.BooleanField(default=False, db_index=True) |
|
45 | 48 | description = models.TextField(blank=True) |
|
46 | 49 | |
|
47 | 50 | def __str__(self): |
|
48 | 51 | return self.name |
|
49 | 52 | |
|
50 | 53 | def is_empty(self) -> bool: |
|
51 | 54 | """ |
|
52 | 55 | Checks if the tag has some threads. |
|
53 | 56 | """ |
|
54 | 57 | |
|
55 | 58 | return self.get_thread_count() == 0 |
|
56 | 59 | |
|
57 | 60 | def get_thread_count(self, archived=None) -> int: |
|
58 | 61 | threads = self.get_threads() |
|
59 | 62 | if archived is not None: |
|
60 | 63 | threads = threads.filter(archived=archived) |
|
61 | 64 | return threads.count() |
|
62 | 65 | |
|
63 | 66 | def get_active_thread_count(self) -> int: |
|
64 | 67 | return self.get_thread_count(archived=False) |
|
65 | 68 | |
|
66 | 69 | def get_absolute_url(self): |
|
67 | 70 | return reverse('tag', kwargs={'tag_name': self.name}) |
|
68 | 71 | |
|
69 | 72 | def get_threads(self): |
|
70 | 73 | return self.thread_tags.order_by('-bump_time') |
|
71 | 74 | |
|
72 | 75 | def is_required(self): |
|
73 | 76 | return self.required |
|
74 | 77 | |
|
75 | 78 | def get_view(self): |
|
76 | 79 | link = '<a class="tag" href="{}">{}</a>'.format( |
|
77 | 80 | self.get_absolute_url(), self.name) |
|
78 | 81 | if self.is_required(): |
|
79 | 82 | link = '<b>{}</b>'.format(link) |
|
80 | 83 | return link |
|
81 | 84 | |
|
82 | 85 | def get_search_view(self, *args, **kwargs): |
|
83 | 86 | return render_to_string('boards/tag.html', { |
|
84 | 87 | 'tag': self, |
|
85 | 88 | }) |
|
86 | 89 | |
|
87 | 90 | @cached_result() |
|
88 | 91 | def get_post_count(self): |
|
89 | 92 | return self.get_threads().aggregate(num_posts=Count('post'))['num_posts'] |
|
90 | 93 | |
|
91 | 94 | def get_description(self): |
|
92 | 95 | return self.description |
|
93 | 96 | |
|
94 | 97 | def get_random_image_post(self, archived=False): |
|
95 | 98 | posts = boards.models.Post.objects.annotate(images_count=Count( |
|
96 | 99 | 'images')).filter(images_count__gt=0, threads__tags__in=[self]) |
|
97 | 100 | if archived is not None: |
|
98 | 101 | posts = posts.filter(thread__archived=archived) |
|
99 | 102 | return posts.order_by('?').first() |
|
100 | 103 | |
|
101 | 104 | def get_first_letter(self): |
|
102 | 105 | return self.name and self.name[0] or '' |
|
103 | 106 | |
|
104 | 107 | def get_related_tags(self): |
|
105 | return Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( | |
|
106 |
id=self.id). |
|
|
108 | return set(Tag.objects.filter(thread_tags__in=self.get_threads()).exclude( | |
|
109 | id=self.id).order_by('?')[:RELATED_TAGS_COUNT]) |
@@ -1,126 +1,126 b'' | |||
|
1 | 1 | from django.shortcuts import get_object_or_404, redirect |
|
2 | 2 | from django.core.urlresolvers import reverse |
|
3 | 3 | |
|
4 | 4 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
5 | 5 | SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS |
|
6 | 6 | from boards.models import Tag, PostImage |
|
7 | 7 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE |
|
8 | 8 | from boards.views.mixins import DispatcherMixin |
|
9 | 9 | from boards.forms import ThreadForm, PlainErrorList |
|
10 | 10 | |
|
11 | 11 | PARAM_HIDDEN_TAGS = 'hidden_tags' |
|
12 | 12 | PARAM_TAG = 'tag' |
|
13 | 13 | PARAM_IS_FAVORITE = 'is_favorite' |
|
14 | 14 | PARAM_IS_HIDDEN = 'is_hidden' |
|
15 | 15 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' |
|
16 | 16 | PARAM_RELATED_TAGS = 'related_tags' |
|
17 | 17 | |
|
18 | 18 | |
|
19 | 19 | __author__ = 'neko259' |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | class TagView(AllThreadsView, DispatcherMixin): |
|
23 | 23 | |
|
24 | 24 | tag_name = None |
|
25 | 25 | |
|
26 | 26 | def get_threads(self): |
|
27 | 27 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
28 | 28 | |
|
29 | 29 | hidden_tags = self.settings_manager.get_hidden_tags() |
|
30 | 30 | |
|
31 | 31 | try: |
|
32 | 32 | hidden_tags.remove(tag) |
|
33 | 33 | except ValueError: |
|
34 | 34 | pass |
|
35 | 35 | |
|
36 | 36 | return tag.get_threads().exclude( |
|
37 | 37 | tags__in=hidden_tags) |
|
38 | 38 | |
|
39 | 39 | def get_context_data(self, **kwargs): |
|
40 | 40 | params = super(TagView, self).get_context_data(**kwargs) |
|
41 | 41 | |
|
42 | 42 | settings_manager = get_settings_manager(kwargs['request']) |
|
43 | 43 | |
|
44 | 44 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
45 | 45 | params[PARAM_TAG] = tag |
|
46 | 46 | |
|
47 | 47 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) |
|
48 | 48 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) |
|
49 | 49 | |
|
50 | 50 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names |
|
51 | 51 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names |
|
52 | 52 | |
|
53 | 53 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() |
|
54 |
params[PARAM_RELATED_TAGS] = tag.get_related_tags() |
|
|
54 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() | |
|
55 | 55 | |
|
56 | 56 | return params |
|
57 | 57 | |
|
58 | 58 | def get_previous_page_link(self, current_page): |
|
59 | 59 | return reverse('tag', kwargs={ |
|
60 | 60 | 'tag_name': self.tag_name, |
|
61 | 61 | }) + '?page=' + str(current_page.previous_page_number()) |
|
62 | 62 | |
|
63 | 63 | def get_next_page_link(self, current_page): |
|
64 | 64 | return reverse('tag', kwargs={ |
|
65 | 65 | 'tag_name': self.tag_name, |
|
66 | 66 | }) + '?page=' + str(current_page.next_page_number()) |
|
67 | 67 | |
|
68 | 68 | def get(self, request, tag_name, form=None): |
|
69 | 69 | self.tag_name = tag_name |
|
70 | 70 | |
|
71 | 71 | return super(TagView, self).get(request, form) |
|
72 | 72 | |
|
73 | 73 | |
|
74 | 74 | def post(self, request, tag_name): |
|
75 | 75 | self.tag_name = tag_name |
|
76 | 76 | |
|
77 | 77 | if 'method' in request.POST: |
|
78 | 78 | self.dispatch_method(request) |
|
79 | 79 | form = None |
|
80 | 80 | |
|
81 | 81 | return redirect('tag', tag_name) |
|
82 | 82 | else: |
|
83 | 83 | form = ThreadForm(request.POST, request.FILES, |
|
84 | 84 | error_class=PlainErrorList) |
|
85 | 85 | form.session = request.session |
|
86 | 86 | |
|
87 | 87 | if form.is_valid(): |
|
88 | 88 | return self.create_thread(request, form) |
|
89 | 89 | if form.need_to_ban: |
|
90 | 90 | # Ban user because he is suspected to be a bot |
|
91 | 91 | self._ban_current_user(request) |
|
92 | 92 | |
|
93 | 93 | return self.get(request, tag_name, page, form) |
|
94 | 94 | |
|
95 | 95 | def subscribe(self, request): |
|
96 | 96 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
97 | 97 | |
|
98 | 98 | settings_manager = get_settings_manager(request) |
|
99 | 99 | settings_manager.add_fav_tag(tag) |
|
100 | 100 | |
|
101 | 101 | def unsubscribe(self, request): |
|
102 | 102 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
103 | 103 | |
|
104 | 104 | settings_manager = get_settings_manager(request) |
|
105 | 105 | settings_manager.del_fav_tag(tag) |
|
106 | 106 | |
|
107 | 107 | def hide(self, request): |
|
108 | 108 | """ |
|
109 | 109 | Adds tag to user's hidden tags. Threads with this tag will not be |
|
110 | 110 | shown. |
|
111 | 111 | """ |
|
112 | 112 | |
|
113 | 113 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
114 | 114 | |
|
115 | 115 | settings_manager = get_settings_manager(request) |
|
116 | 116 | settings_manager.add_hidden_tag(tag) |
|
117 | 117 | |
|
118 | 118 | def unhide(self, request): |
|
119 | 119 | """ |
|
120 | 120 | Removed tag from user's hidden tags. |
|
121 | 121 | """ |
|
122 | 122 | |
|
123 | 123 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
124 | 124 | |
|
125 | 125 | settings_manager = get_settings_manager(request) |
|
126 | 126 | settings_manager.del_hidden_tag(tag) |
General Comments 0
You need to be logged in to leave comments.
Login now