all_threads.py
136 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
neko259
|
r977 | from django.core.paginator import EmptyPage | ||
from django.http import Http404 | ||||
neko259
|
r2053 | from django.shortcuts import render, redirect | ||
neko259
|
r1986 | from django.urls import reverse | ||
neko259
|
r1422 | from django.utils.decorators import method_decorator | ||
from django.views.decorators.csrf import csrf_protect | ||||
neko259
|
r542 | |||
neko259
|
r1997 | from boards import settings | ||
neko259
|
r2005 | from boards.abstracts.constants import PARAM_PAGE | ||
neko259
|
r596 | from boards.abstracts.paginator import get_paginator | ||
neko259
|
r1986 | from boards.abstracts.settingsmanager import get_settings_manager, \ | ||
neko259
|
r2088 | SETTING_ONLY_FAVORITES, SETTING_SUBSCRIBE_BY_DEFAULT | ||
neko259
|
r542 | from boards.forms import ThreadForm, PlainErrorList | ||
neko259
|
r1997 | from boards.models import Post, Thread | ||
neko259
|
r2035 | from boards.settings import SECTION_VIEW | ||
neko259
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
neko259
|
r2035 | from boards.views.mixins import PaginatedMixin, \ | ||
neko259
|
r1986 | DispatcherMixin, PARAMETER_METHOD | ||
neko259
|
r900 | |||
neko259
|
r2005 | ORDER_BUMP = 'bump' | ||
PARAM_ORDER = 'order' | ||||
neko259
|
r722 | FORM_TAGS = 'tags' | ||
FORM_TEXT = 'text' | ||||
FORM_TITLE = 'title' | ||||
FORM_IMAGE = 'image' | ||||
neko259
|
r1077 | FORM_THREADS = 'threads' | ||
neko259
|
r722 | |||
neko259
|
r634 | TAG_DELIMITER = ' ' | ||
neko259
|
r542 | PARAMETER_CURRENT_PAGE = 'current_page' | ||
PARAMETER_PAGINATOR = 'paginator' | ||||
PARAMETER_THREADS = 'threads' | ||||
neko259
|
r1377 | PARAMETER_ADDITIONAL = 'additional_params' | ||
neko259
|
r1399 | PARAMETER_RSS_URL = 'rss_url' | ||
neko259
|
r542 | |||
neko259
|
r1164 | TEMPLATE = 'boards/all_threads.html' | ||
neko259
|
r542 | DEFAULT_PAGE = 1 | ||
neko259
|
r1738 | |||
neko259
|
r2032 | class AllThreadsView(BaseBoardView, PaginatedMixin, DispatcherMixin): | ||
neko259
|
r542 | |||
neko259
|
r1738 | tag_name = '' | ||
neko259
|
r728 | def __init__(self): | ||
self.settings_manager = None | ||||
super(AllThreadsView, self).__init__() | ||||
neko259
|
r635 | |||
neko259
|
r1422 | @method_decorator(csrf_protect) | ||
neko259
|
r1204 | def get(self, request, form: ThreadForm=None): | ||
neko259
|
r2005 | page = request.GET.get(PARAM_PAGE, DEFAULT_PAGE) | ||
neko259
|
r1204 | |||
neko259
|
r919 | params = self.get_context_data(request=request) | ||
neko259
|
r542 | |||
neko259
|
r2088 | subscribe_by_default = get_settings_manager(request).get_setting( | ||
SETTING_SUBSCRIBE_BY_DEFAULT, False) | ||||
neko259
|
r544 | if not form: | ||
neko259
|
r1738 | form = ThreadForm(error_class=PlainErrorList, | ||
neko259
|
r2088 | initial={FORM_TAGS: self.tag_name, | ||
'subscribe': subscribe_by_default}) | ||||
neko259
|
r542 | |||
neko259
|
r730 | self.settings_manager = get_settings_manager(request) | ||
neko259
|
r1376 | |||
threads = self.get_threads() | ||||
neko259
|
r2005 | order = request.GET.get(PARAM_ORDER, ORDER_BUMP) | ||
if order == ORDER_BUMP: | ||||
neko259
|
r1376 | threads = threads.order_by('-bump_time') | ||
else: | ||||
neko259
|
r1704 | threads = threads.filter(replies__opening=True)\ | ||
.order_by('-replies__pub_time') | ||||
neko259
|
r1472 | threads = threads.distinct() | ||
neko259
|
r1376 | |||
neko259
|
r2005 | paginator = get_paginator(threads, settings.get_int( | ||
neko259
|
r2061 | SECTION_VIEW, 'ThreadsPerPage'), | ||
link=self.get_reverse_url(), | ||||
params=request.GET.dict()) | ||||
neko259
|
r596 | paginator.current_page = int(page) | ||
neko259
|
r542 | |||
neko259
|
r977 | try: | ||
threads = paginator.page(page).object_list | ||||
except EmptyPage: | ||||
raise Http404() | ||||
neko259
|
r542 | |||
neko259
|
r918 | params[PARAMETER_THREADS] = threads | ||
params[CONTEXT_FORM] = form | ||||
neko259
|
r1399 | params[PARAMETER_RSS_URL] = self.get_rss_url() | ||
neko259
|
r542 | |||
neko259
|
r1854 | params.update(self.get_page_context(paginator, page)) | ||
neko259
|
r542 | |||
neko259
|
r918 | return render(request, TEMPLATE, params) | ||
neko259
|
r542 | |||
neko259
|
r1422 | @method_decorator(csrf_protect) | ||
neko259
|
r1204 | def post(self, request): | ||
neko259
|
r1691 | if PARAMETER_METHOD in request.POST: | ||
self.dispatch_method(request) | ||||
neko259
|
r2053 | return redirect(self.get_reverse_url()) | ||
neko259
|
r1691 | |||
neko259
|
r542 | form = ThreadForm(request.POST, request.FILES, | ||
neko259
|
r2069 | error_class=PlainErrorList, session=request.session) | ||
neko259
|
r542 | |||
if form.is_valid(): | ||||
neko259
|
r1997 | return Post.objects.create_from_form(request, form, None) | ||
neko259
|
r542 | if form.need_to_ban: | ||
# Ban user because he is suspected to be a bot | ||||
self._ban_current_user(request) | ||||
neko259
|
r1204 | return self.get(request, form) | ||
neko259
|
r542 | |||
neko259
|
r1377 | def get_reverse_url(self): | ||
return reverse('index') | ||||
neko259
|
r1374 | |||
neko259
|
r542 | def get_threads(self): | ||
neko259
|
r634 | """ | ||
Gets list of threads that will be shown on a page. | ||||
""" | ||||
neko259
|
r1692 | threads = Thread.objects\ | ||
neko259
|
r728 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) | ||
neko259
|
r1692 | if self.settings_manager.get_setting(SETTING_ONLY_FAVORITES): | ||
fav_tags = self.settings_manager.get_fav_tags() | ||||
if len(fav_tags) > 0: | ||||
threads = threads.filter(tags__in=fav_tags) | ||||
return threads | ||||
neko259
|
r1399 | |||
def get_rss_url(self): | ||||
return self.get_reverse_url() + 'rss/' | ||||
neko259
|
r1691 | |||
def toggle_fav(self, request): | ||||
settings_manager = get_settings_manager(request) | ||||
settings_manager.set_setting(SETTING_ONLY_FAVORITES, | ||||
not settings_manager.get_setting(SETTING_ONLY_FAVORITES, False)) | ||||