tag_threads.py
86 lines
| 2.5 KiB
| text/x-python
|
PythonLexer
|
r542 | from django.shortcuts import get_object_or_404 | ||
from boards.models import Tag, Post | ||||
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 | |||
__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) | ||||
|
r548 | return tag.threads.filter(archived=False).order_by('-bump_time') | ||
|
r542 | |||
def get_context_data(self, **kwargs): | ||||
context = super(TagView, self).get_context_data(**kwargs) | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
context['tag'] = tag | ||||
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): | ||||
user = self._get_user(request) | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
if not tag in user.fav_tags.all(): | ||||
user.add_tag(tag) | ||||
return self.redirect_to_next(request) | ||||
def unsubscribe(self, request): | ||||
user = self._get_user(request) | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
if tag in user.fav_tags.all(): | ||||
user.remove_tag(tag) | ||||
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. | ||||
""" | ||||
user = self._get_user(request) | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
user.hide_tag(tag) | ||||
def unhide(self, request): | ||||
""" | ||||
Removed tag from user's hidden tags. | ||||
""" | ||||
user = self._get_user(request) | ||||
tag = get_object_or_404(Tag, name=self.tag_name) | ||||
user.unhide_tag(tag) | ||||