tag_threads.py
99 lines
| 3.1 KiB
| text/x-python
|
PythonLexer
|
r542 | from django.shortcuts import get_object_or_404 | ||
|
r730 | |||
|
r998 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS | ||||
|
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_TAG = 'tag' | ||||
|
r998 | PARAM_IS_FAVORITE = 'is_favorite' | ||
PARAM_IS_HIDDEN = 'is_hidden' | ||||
|
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): | ||||
|
r919 | params = super(TagView, self).get_context_data(**kwargs) | ||
|
r542 | |||
|
r730 | settings_manager = get_settings_manager(kwargs['request']) | ||
|
r728 | |||
|
r542 | tag = get_object_or_404(Tag, name=self.tag_name) | ||
|
r919 | params[PARAM_TAG] = tag | ||
|
r542 | |||
|
r998 | fav_tag_names = settings_manager.get_setting(SETTING_FAVORITE_TAGS) | ||
hidden_tag_names = settings_manager.get_setting(SETTING_HIDDEN_TAGS) | ||||
params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.name in fav_tag_names | ||||
params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.name in hidden_tag_names | ||||
|
r728 | |||
|
r919 | return params | ||
|
r542 | |||
|
r597 | def get(self, request, tag_name, page=DEFAULT_PAGE, form=None): | ||
|
r542 | self.tag_name = tag_name | ||
|
r1037 | return super(TagView, self).get(request, page, form) | ||
|
r597 | |||
def post(self, request, tag_name, page=DEFAULT_PAGE): | ||||
|
r1037 | self.tag_name = tag_name | ||
|
r597 | |||
|
r1037 | if 'method' in request.POST: | ||
self.dispatch_method(request) | ||||
form = None | ||||
else: | ||||
form = ThreadForm(request.POST, request.FILES, | ||||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
return self.create_thread(request, form) | ||||
if form.need_to_ban: | ||||
# Ban user because he is suspected to be a bot | ||||
self._ban_current_user(request) | ||||
|
r597 | |||
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 | |||
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 | |||
|
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) | ||