Show More
@@ -1,126 +1,126 b'' | |||||
1 | from django.shortcuts import get_object_or_404, redirect |
|
1 | from django.shortcuts import get_object_or_404, redirect | |
2 | from django.core.urlresolvers import reverse |
|
2 | from django.core.urlresolvers import reverse | |
3 |
|
3 | |||
4 | from boards.abstracts.settingsmanager import get_settings_manager, \ |
|
4 | from boards.abstracts.settingsmanager import get_settings_manager, \ | |
5 | SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS |
|
5 | SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS | |
6 | from boards.models import Tag, PostImage |
|
6 | from boards.models import Tag, PostImage | |
7 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE |
|
7 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE | |
8 | from boards.views.mixins import DispatcherMixin |
|
8 | from boards.views.mixins import DispatcherMixin | |
9 | from boards.forms import ThreadForm, PlainErrorList |
|
9 | from boards.forms import ThreadForm, PlainErrorList | |
10 |
|
10 | |||
11 | PARAM_HIDDEN_TAGS = 'hidden_tags' |
|
11 | PARAM_HIDDEN_TAGS = 'hidden_tags' | |
12 | PARAM_TAG = 'tag' |
|
12 | PARAM_TAG = 'tag' | |
13 | PARAM_IS_FAVORITE = 'is_favorite' |
|
13 | PARAM_IS_FAVORITE = 'is_favorite' | |
14 | PARAM_IS_HIDDEN = 'is_hidden' |
|
14 | PARAM_IS_HIDDEN = 'is_hidden' | |
15 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' |
|
15 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' | |
16 | PARAM_RELATED_TAGS = 'related_tags' |
|
16 | PARAM_RELATED_TAGS = 'related_tags' | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | __author__ = 'neko259' |
|
19 | __author__ = 'neko259' | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | class TagView(AllThreadsView, DispatcherMixin): |
|
22 | class TagView(AllThreadsView, DispatcherMixin): | |
23 |
|
23 | |||
24 | tag_name = None |
|
24 | tag_name = None | |
25 |
|
25 | |||
26 | def get_threads(self): |
|
26 | def get_threads(self): | |
27 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
27 | tag = get_object_or_404(Tag, name=self.tag_name) | |
28 |
|
28 | |||
29 | hidden_tags = self.settings_manager.get_hidden_tags() |
|
29 | hidden_tags = self.settings_manager.get_hidden_tags() | |
30 |
|
30 | |||
31 | try: |
|
31 | try: | |
32 | hidden_tags.remove(tag) |
|
32 | hidden_tags.remove(tag) | |
33 | except ValueError: |
|
33 | except ValueError: | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 | return tag.get_threads().exclude( |
|
36 | return tag.get_threads().exclude( | |
37 | tags__in=hidden_tags) |
|
37 | tags__in=hidden_tags) | |
38 |
|
38 | |||
39 | def get_context_data(self, **kwargs): |
|
39 | def get_context_data(self, **kwargs): | |
40 | params = super(TagView, self).get_context_data(**kwargs) |
|
40 | params = super(TagView, self).get_context_data(**kwargs) | |
41 |
|
41 | |||
42 | settings_manager = get_settings_manager(kwargs['request']) |
|
42 | settings_manager = get_settings_manager(kwargs['request']) | |
43 |
|
43 | |||
44 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
44 | tag = get_object_or_404(Tag, name=self.tag_name) | |
45 | params[PARAM_TAG] = tag |
|
45 | params[PARAM_TAG] = tag | |
46 |
|
46 | |||
47 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) |
|
47 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) | |
48 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) |
|
48 | hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) | |
49 |
|
49 | |||
50 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names |
|
50 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names | |
51 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names |
|
51 | params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names | |
52 |
|
52 | |||
53 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() |
|
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 | return params |
|
56 | return params | |
57 |
|
57 | |||
58 | def get_previous_page_link(self, current_page): |
|
58 | def get_previous_page_link(self, current_page): | |
59 | return reverse('tag', kwargs={ |
|
59 | return reverse('tag', kwargs={ | |
60 | 'tag_name': self.tag_name, |
|
60 | 'tag_name': self.tag_name, | |
61 | }) + '?page=' + str(current_page.previous_page_number()) |
|
61 | }) + '?page=' + str(current_page.previous_page_number()) | |
62 |
|
62 | |||
63 | def get_next_page_link(self, current_page): |
|
63 | def get_next_page_link(self, current_page): | |
64 | return reverse('tag', kwargs={ |
|
64 | return reverse('tag', kwargs={ | |
65 | 'tag_name': self.tag_name, |
|
65 | 'tag_name': self.tag_name, | |
66 | }) + '?page=' + str(current_page.next_page_number()) |
|
66 | }) + '?page=' + str(current_page.next_page_number()) | |
67 |
|
67 | |||
68 | def get(self, request, tag_name, form=None): |
|
68 | def get(self, request, tag_name, form=None): | |
69 | self.tag_name = tag_name |
|
69 | self.tag_name = tag_name | |
70 |
|
70 | |||
71 | return super(TagView, self).get(request, form) |
|
71 | return super(TagView, self).get(request, form) | |
72 |
|
72 | |||
73 |
|
73 | |||
74 | def post(self, request, tag_name): |
|
74 | def post(self, request, tag_name): | |
75 | self.tag_name = tag_name |
|
75 | self.tag_name = tag_name | |
76 |
|
76 | |||
77 | if 'method' in request.POST: |
|
77 | if 'method' in request.POST: | |
78 | self.dispatch_method(request) |
|
78 | self.dispatch_method(request) | |
79 | form = None |
|
79 | form = None | |
80 |
|
80 | |||
81 | return redirect('tag', tag_name) |
|
81 | return redirect('tag', tag_name) | |
82 | else: |
|
82 | else: | |
83 | form = ThreadForm(request.POST, request.FILES, |
|
83 | form = ThreadForm(request.POST, request.FILES, | |
84 | error_class=PlainErrorList) |
|
84 | error_class=PlainErrorList) | |
85 | form.session = request.session |
|
85 | form.session = request.session | |
86 |
|
86 | |||
87 | if form.is_valid(): |
|
87 | if form.is_valid(): | |
88 | return self.create_thread(request, form) |
|
88 | return self.create_thread(request, form) | |
89 | if form.need_to_ban: |
|
89 | if form.need_to_ban: | |
90 | # Ban user because he is suspected to be a bot |
|
90 | # Ban user because he is suspected to be a bot | |
91 | self._ban_current_user(request) |
|
91 | self._ban_current_user(request) | |
92 |
|
92 | |||
93 |
return self.get(request, tag_name, |
|
93 | return self.get(request, tag_name, form) | |
94 |
|
94 | |||
95 | def subscribe(self, request): |
|
95 | def subscribe(self, request): | |
96 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
96 | tag = get_object_or_404(Tag, name=self.tag_name) | |
97 |
|
97 | |||
98 | settings_manager = get_settings_manager(request) |
|
98 | settings_manager = get_settings_manager(request) | |
99 | settings_manager.add_fav_tag(tag) |
|
99 | settings_manager.add_fav_tag(tag) | |
100 |
|
100 | |||
101 | def unsubscribe(self, request): |
|
101 | def unsubscribe(self, request): | |
102 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
102 | tag = get_object_or_404(Tag, name=self.tag_name) | |
103 |
|
103 | |||
104 | settings_manager = get_settings_manager(request) |
|
104 | settings_manager = get_settings_manager(request) | |
105 | settings_manager.del_fav_tag(tag) |
|
105 | settings_manager.del_fav_tag(tag) | |
106 |
|
106 | |||
107 | def hide(self, request): |
|
107 | def hide(self, request): | |
108 | """ |
|
108 | """ | |
109 | Adds tag to user's hidden tags. Threads with this tag will not be |
|
109 | Adds tag to user's hidden tags. Threads with this tag will not be | |
110 | shown. |
|
110 | shown. | |
111 | """ |
|
111 | """ | |
112 |
|
112 | |||
113 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
113 | tag = get_object_or_404(Tag, name=self.tag_name) | |
114 |
|
114 | |||
115 | settings_manager = get_settings_manager(request) |
|
115 | settings_manager = get_settings_manager(request) | |
116 | settings_manager.add_hidden_tag(tag) |
|
116 | settings_manager.add_hidden_tag(tag) | |
117 |
|
117 | |||
118 | def unhide(self, request): |
|
118 | def unhide(self, request): | |
119 | """ |
|
119 | """ | |
120 | Removed tag from user's hidden tags. |
|
120 | Removed tag from user's hidden tags. | |
121 | """ |
|
121 | """ | |
122 |
|
122 | |||
123 | tag = get_object_or_404(Tag, name=self.tag_name) |
|
123 | tag = get_object_or_404(Tag, name=self.tag_name) | |
124 |
|
124 | |||
125 | settings_manager = get_settings_manager(request) |
|
125 | settings_manager = get_settings_manager(request) | |
126 | settings_manager.del_hidden_tag(tag) |
|
126 | settings_manager.del_hidden_tag(tag) |
General Comments 0
You need to be logged in to leave comments.
Login now