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