tag_threads.py
51 lines
| 1.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 | ||
|
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 | ||||
def get(self, request, tag_name, page=DEFAULT_PAGE): | ||||
self.tag_name = tag_name | ||||
|
r563 | dispatch_result = self.dispatch_method(request) | ||
if dispatch_result: | ||||
return dispatch_result | ||||
else: | ||||
return super(TagView, self).get(request, page) | ||||
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) | ||||