all_threads.py
168 lines
| 5.4 KiB
| text/x-python
|
PythonLexer
|
r1129 | from django.core.urlresolvers import reverse | ||
|
r954 | from django.core.files import File | ||
from django.core.files.temp import NamedTemporaryFile | ||||
|
r977 | from django.core.paginator import EmptyPage | ||
|
r542 | from django.db import transaction | ||
|
r977 | from django.http import Http404 | ||
|
r542 | from django.shortcuts import render, redirect | ||
|
r1422 | from django.utils.decorators import method_decorator | ||
from django.views.decorators.csrf import csrf_protect | ||||
|
r542 | |||
|
r716 | from boards import utils, settings | ||
|
r596 | from boards.abstracts.paginator import get_paginator | ||
|
r730 | from boards.abstracts.settingsmanager import get_settings_manager | ||
|
r542 | from boards.forms import ThreadForm, PlainErrorList | ||
|
r1590 | from boards.models import Post, Thread, Ban, Banner | ||
|
r542 | from boards.views.banned import BannedView | ||
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
|
r542 | from boards.views.posting_mixin import PostMixin | ||
|
r1419 | from boards.views.mixins import FileUploadMixin, PaginatedMixin | ||
|
r900 | |||
|
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' | ||||
|
r1149 | PARAMETER_BANNERS = 'banners' | ||
|
r1377 | PARAMETER_ADDITIONAL = 'additional_params' | ||
|
r1396 | PARAMETER_MAX_FILE_SIZE = 'max_file_size' | ||
|
r1399 | PARAMETER_RSS_URL = 'rss_url' | ||
|
r542 | |||
|
r1164 | TEMPLATE = 'boards/all_threads.html' | ||
|
r542 | DEFAULT_PAGE = 1 | ||
|
r1419 | class AllThreadsView(PostMixin, FileUploadMixin, BaseBoardView, PaginatedMixin): | ||
|
r542 | |||
|
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): | ||
page = request.GET.get('page', DEFAULT_PAGE) | ||||
|
r919 | params = self.get_context_data(request=request) | ||
|
r542 | |||
|
r544 | if not form: | ||
form = ThreadForm(error_class=PlainErrorList) | ||||
|
r542 | |||
|
r730 | self.settings_manager = get_settings_manager(request) | ||
|
r1376 | |||
threads = self.get_threads() | ||||
order = request.GET.get('order', 'bump') | ||||
if order == 'bump': | ||||
threads = threads.order_by('-bump_time') | ||||
else: | ||||
threads = threads.filter(multi_replies__opening=True).order_by('-multi_replies__pub_time') | ||||
|
r1472 | filter = request.GET.get('filter') | ||
if filter == 'fav_tags': | ||||
fav_tags = self.settings_manager.get_fav_tags() | ||||
if len(fav_tags) > 0: | ||||
threads = threads.filter(tags__in=fav_tags) | ||||
threads = threads.distinct() | ||||
|
r1376 | |||
paginator = get_paginator(threads, | ||||
|
r1153 | settings.get_int('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 | ||||
|
r1149 | params[PARAMETER_BANNERS] = Banner.objects.order_by('-id').all() | ||
|
r1396 | params[PARAMETER_MAX_FILE_SIZE] = self.get_max_upload_size() | ||
|
r1399 | params[PARAMETER_RSS_URL] = self.get_rss_url() | ||
|
r542 | |||
|
r1377 | paginator.set_url(self.get_reverse_url(), request.GET.dict()) | ||
self.get_page_context(paginator, params, page) | ||||
|
r542 | |||
|
r918 | return render(request, TEMPLATE, params) | ||
|
r542 | |||
|
r1422 | @method_decorator(csrf_protect) | ||
|
r1204 | def post(self, request): | ||
|
r542 | form = ThreadForm(request.POST, request.FILES, | ||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
|
r634 | return self.create_thread(request, form) | ||
|
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_page_context(self, paginator, params, page): | ||
|
r542 | """ | ||
Get pagination context variables | ||||
""" | ||||
|
r918 | params[PARAMETER_PAGINATOR] = paginator | ||
|
r1129 | current_page = paginator.page(int(page)) | ||
params[PARAMETER_CURRENT_PAGE] = current_page | ||||
|
r1419 | self.set_page_urls(paginator, params) | ||
|
r1129 | |||
|
r1377 | def get_reverse_url(self): | ||
return reverse('index') | ||||
|
r1374 | |||
|
r542 | @transaction.atomic | ||
|
r954 | def create_thread(self, request, form: ThreadForm, html_response=True): | ||
|
r542 | """ | ||
|
r634 | Creates a new thread with an opening post. | ||
|
r542 | """ | ||
ip = utils.get_client_ip(request) | ||||
is_banned = Ban.objects.filter(ip=ip).exists() | ||||
if is_banned: | ||||
if html_response: | ||||
return redirect(BannedView().as_view()) | ||||
else: | ||||
return | ||||
data = form.cleaned_data | ||||
|
r1299 | title = form.get_title() | ||
|
r722 | text = data[FORM_TEXT] | ||
|
r1273 | file = form.get_file() | ||
|
r1077 | threads = data[FORM_THREADS] | ||
|
r1500 | images = form.get_images() | ||
|
r542 | |||
text = self._remove_invalid_links(text) | ||||
|
r1348 | tags = data[FORM_TAGS] | ||
|
r1434 | monochrome = form.is_monochrome() | ||
|
r542 | |||
|
r1273 | post = Post.objects.create_post(title=title, text=text, file=file, | ||
|
r1293 | ip=ip, tags=tags, opening_posts=threads, | ||
|
r1434 | tripcode=form.get_tripcode(), | ||
|
r1500 | monochrome=monochrome, images=images) | ||
|
r900 | |||
# This is required to update the threads to which posts we have replied | ||||
# when creating this one | ||||
|
r1091 | post.notify_clients() | ||
|
r542 | |||
if html_response: | ||||
|
r1160 | return redirect(post.get_absolute_url()) | ||
|
r542 | |||
def get_threads(self): | ||||
|
r634 | """ | ||
Gets list of threads that will be shown on a page. | ||||
""" | ||||
|
r1374 | return Thread.objects\ | ||
|
r728 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) | ||
|
r1399 | |||
def get_rss_url(self): | ||||
return self.get_reverse_url() + 'rss/' | ||||