|
|
from django.shortcuts import get_object_or_404, redirect
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
|
|
from boards.abstracts.settingsmanager import get_settings_manager, \
|
|
|
SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS
|
|
|
from boards.models import Tag, PostImage
|
|
|
from boards.views.all_threads import AllThreadsView, DEFAULT_PAGE
|
|
|
from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD
|
|
|
from boards.forms import ThreadForm, PlainErrorList
|
|
|
|
|
|
PARAM_HIDDEN_TAGS = 'hidden_tags'
|
|
|
PARAM_TAG = 'tag'
|
|
|
PARAM_IS_FAVORITE = 'is_favorite'
|
|
|
PARAM_IS_HIDDEN = 'is_hidden'
|
|
|
PARAM_RANDOM_IMAGE_POST = 'random_image_post'
|
|
|
PARAM_RELATED_TAGS = 'related_tags'
|
|
|
|
|
|
|
|
|
__author__ = 'neko259'
|
|
|
|
|
|
|
|
|
class TagView(AllThreadsView, DispatcherMixin):
|
|
|
|
|
|
tag_name = None
|
|
|
|
|
|
def get_threads(self):
|
|
|
tag = get_object_or_404(Tag, name=self.tag_name)
|
|
|
|
|
|
hidden_tags = self.settings_manager.get_hidden_tags()
|
|
|
|
|
|
try:
|
|
|
hidden_tags.remove(tag)
|
|
|
except ValueError:
|
|
|
pass
|
|
|
|
|
|
return tag.get_threads().exclude(
|
|
|
tags__in=hidden_tags)
|
|
|
|
|
|
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
|
|
|
|
|
|
params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post()
|
|
|
params[PARAM_RELATED_TAGS] = tag.get_related_tags()
|
|
|
|
|
|
return params
|
|
|
|
|
|
def get_reverse_url(self, params):
|
|
|
return reverse('tag', kwargs={'tag_name': self.tag_name}) + self.get_url_params(params)
|
|
|
|
|
|
def get(self, request, tag_name, form=None):
|
|
|
self.tag_name = tag_name
|
|
|
|
|
|
return super(TagView, self).get(request, form)
|
|
|
|
|
|
|
|
|
def post(self, request, tag_name):
|
|
|
self.tag_name = tag_name
|
|
|
|
|
|
if PARAMETER_METHOD in request.POST:
|
|
|
self.dispatch_method(request)
|
|
|
|
|
|
return redirect('tag', tag_name)
|
|
|
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, 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)
|
|
|
|