tag_threads.py
124 lines
| 3.8 KiB
| text/x-python
|
PythonLexer
|
r1095 | from django.shortcuts import get_object_or_404, redirect | ||
|
r1986 | from django.urls import reverse | ||
|
r730 | |||
|
r998 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
SETTING_FAVORITE_TAGS, SETTING_HIDDEN_TAGS | ||||
|
r1997 | from boards.models import Tag, TagAlias, Post | ||
|
r1590 | from boards.views.all_threads import AllThreadsView | ||
|
r1366 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD | ||
|
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' | ||||
|
r1263 | PARAM_RANDOM_IMAGE_POST = 'random_image_post' | ||
|
r1269 | PARAM_RELATED_TAGS = 'related_tags' | ||
|
r730 | |||
|
r542 | __author__ = 'neko259' | ||
|
r1090 | class TagView(AllThreadsView, DispatcherMixin): | ||
|
r542 | |||
tag_name = None | ||||
def get_threads(self): | ||||
|
r1874 | tag_alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = tag_alias.parent | ||||
|
r542 | |||
|
r1064 | hidden_tags = self.settings_manager.get_hidden_tags() | ||
try: | ||||
hidden_tags.remove(tag) | ||||
except ValueError: | ||||
pass | ||||
|
r1062 | return tag.get_threads().exclude( | ||
|
r1064 | tags__in=hidden_tags) | ||
|
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 | |||
|
r1874 | tag_alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = tag_alias.parent | ||||
|
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) | ||||
|
r1874 | params[PARAM_IS_FAVORITE] = fav_tag_names is not None and tag.get_name() in fav_tag_names | ||
params[PARAM_IS_HIDDEN] = hidden_tag_names is not None and tag.get_name() in hidden_tag_names | ||||
|
r1263 | |||
|
r1264 | params[PARAM_RANDOM_IMAGE_POST] = tag.get_random_image_post() | ||
|
r1270 | params[PARAM_RELATED_TAGS] = tag.get_related_tags() | ||
|
r728 | |||
|
r919 | return params | ||
|
r542 | |||
|
r1377 | def get_reverse_url(self): | ||
return reverse('tag', kwargs={'tag_name': self.tag_name}) | ||||
|
r1129 | |||
|
r1204 | def get(self, request, tag_name, form=None): | ||
|
r542 | self.tag_name = tag_name | ||
|
r1204 | return super(TagView, self).get(request, form) | ||
|
r1037 | |||
|
r597 | |||
|
r1204 | def post(self, request, tag_name): | ||
|
r1037 | self.tag_name = tag_name | ||
|
r597 | |||
|
r1366 | if PARAMETER_METHOD in request.POST: | ||
|
r1037 | self.dispatch_method(request) | ||
|
r1103 | |||
return redirect('tag', tag_name) | ||||
|
r1037 | else: | ||
form = ThreadForm(request.POST, request.FILES, | ||||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
|
r1997 | return Post.objects.create_from_form(request, form, opening_post=None) | ||
|
r1037 | if form.need_to_ban: | ||
# Ban user because he is suspected to be a bot | ||||
self._ban_current_user(request) | ||||
|
r597 | |||
|
r1292 | return self.get(request, tag_name, form) | ||
|
r563 | |||
def subscribe(self, request): | ||||
|
r1874 | alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = alias.parent | ||||
|
r563 | |||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.add_fav_tag(tag) | ||
|
r563 | |||
def unsubscribe(self, request): | ||||
|
r1874 | alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = alias.parent | ||||
|
r563 | |||
|
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. | ||||
""" | ||||
|
r1874 | alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = alias.parent | ||||
|
r635 | |||
|
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. | ||||
""" | ||||
|
r1874 | alias = get_object_or_404(TagAlias, name=self.tag_name) | ||
tag = alias.parent | ||||
|
r635 | |||
|
r730 | settings_manager = get_settings_manager(request) | ||
|
r728 | settings_manager.del_hidden_tag(tag) | ||