tag_threads.py
95 lines
| 2.8 KiB
| text/x-python
|
PythonLexer
|
r542 | from django.shortcuts import get_object_or_404 | ||
|
r730 | |||
from boards.abstracts.settingsmanager import get_settings_manager | ||||
|
r908 | from boards.models import Tag, Thread | ||
|
r542 | from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE | ||
|
r563 | from boards.views.mixins import DispatcherMixin, RedirectNextMixin | ||
|
r597 | from boards.forms import ThreadForm, PlainErrorList | ||
|
r542 | |||
|
r900 | PARAM_HIDDEN_TAGS = 'hidden_tags' | ||
PARAM_FAV_TAGS = 'fav_tags' | ||||
PARAM_TAG = 'tag' | ||||
|
r730 | |||
|
r542 | __author__ = 'neko259' | ||
|
r563 | class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin): | ||
|
r542 | |||
tag_name = None | ||||
def get_threads(self): | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
|
r908 | return tag.get_threads() | ||
|
r542 | |||
def get_context_data(self, **kwargs): | ||||
context = super(TagView, self).get_context_data(**kwargs) | ||||
|
r730 | settings_manager = get_settings_manager(kwargs['request']) | ||
|
r728 | |||
|
r542 | tag = get_object_or_404(Tag, name=self.tag_name) | ||
|
r900 | context[PARAM_TAG] = tag | ||
|
r542 | |||
|
r900 | context[PARAM_FAV_TAGS] = settings_manager.get_fav_tags() | ||
context[PARAM_HIDDEN_TAGS] = settings_manager.get_hidden_tags() | ||||
|
r728 | |||
|
r542 | return context | ||
|
r597 | def get(self, request, tag_name, page=DEFAULT_PAGE, form=None): | ||
|
r542 | self.tag_name = tag_name | ||
|
r563 | dispatch_result = self.dispatch_method(request) | ||
if dispatch_result: | ||||
return dispatch_result | ||||
else: | ||||
|
r597 | return super(TagView, self).get(request, page, form) | ||
def post(self, request, tag_name, page=DEFAULT_PAGE): | ||||
form = ThreadForm(request.POST, request.FILES, | ||||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
|
r634 | return self.create_thread(request, form) | ||
|
r597 | if form.need_to_ban: | ||
# Ban user because he is suspected to be a bot | ||||
self._ban_current_user(request) | ||||
return self.get(request, tag_name, page, form) | ||||
|
r563 | |||
def subscribe(self, request): | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.add_fav_tag(tag) | ||
|
r563 | |||
return self.redirect_to_next(request) | ||||
def unsubscribe(self, request): | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.del_fav_tag(tag) | ||
|
r563 | |||
return self.redirect_to_next(request) | ||||
|
r635 | |||
def hide(self, request): | ||||
""" | ||||
Adds tag to user's hidden tags. Threads with this tag will not be | ||||
shown. | ||||
""" | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.add_hidden_tag(tag) | ||
|
r635 | |||
def unhide(self, request): | ||||
""" | ||||
Removed tag from user's hidden tags. | ||||
""" | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.del_hidden_tag(tag) | ||