|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
|
from boards.abstracts.settingsmanager import get_settings_manager, \
|
|
|
SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS
|
|
|
from boards.models import Tag, Thread
|
|
|
from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
|
|
|
from boards.views.mixins import DispatcherMixin, RedirectNextMixin
|
|
|
from boards.forms import ThreadForm, PlainErrorList
|
|
|
|
|
|
PARAM_HIDDEN_TAGS = 'hidden_tags'
|
|
|
PARAM_TAG = 'tag'
|
|
|
PARAM_IS_FAVORITE = 'is_favorite'
|
|
|
PARAM_IS_HIDDEN = 'is_hidden'
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
class TagView(AllThreadsView, DispatcherMixin, RedirectNextMixin):
|
|
|
|
|
|
tag_name = None
|
|
|
|
|
|
def get_threads(self):
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
|
|
|
return tag.get_threads()
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
params = super(TagView, self).get_context_data(**kwargs)
|
|
|
|
|
|
settings_manager = get_settings_manager(kwargs['request'])
|
|
|
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
params[PARAM_TAG] = tag
|
|
|
|
|
|
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
|
|
|
|
|
|
return params
|
|
|
|
|
|
def get(self, request, tag_name, page=DEFAULT_PAGE, form=None):
|
|
|
self.tag_name = tag_name
|
|
|
|
|
|
return super(TagView, self).get(request, page, form)
|
|
|
|
|
|
|
|
|
def post(self, request, tag_name, page=DEFAULT_PAGE):
|
|
|
self.tag_name = tag_name
|
|
|
|
|
|
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)
|
|
|
|
|
|
return self.get(request, tag_name, page, form)
|
|
|
|
|
|
def subscribe(self, request):
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
settings_manager.add_fav_tag(tag)
|
|
|
|
|
|
def unsubscribe(self, request):
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
settings_manager.del_fav_tag(tag)
|
|
|
|
|
|
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)
|
|
|
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
settings_manager.add_hidden_tag(tag)
|
|
|
|
|
|
def unhide(self, request):
|
|
|
"""
|
|
|
Removed tag from user's hidden tags.
|
|
|
"""
|
|
|
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
|
|
|
settings_manager = get_settings_manager(request)
|
|
|
settings_manager.del_hidden_tag(tag)
|
|
|
|