all_threads.py
169 lines
| 5.2 KiB
| text/x-python
|
PythonLexer
neko259
|
r1129 | from django.core.urlresolvers import reverse | ||
neko259
|
r954 | from django.core.files import File | ||
from django.core.files.temp import NamedTemporaryFile | ||||
neko259
|
r977 | from django.core.paginator import EmptyPage | ||
neko259
|
r542 | from django.db import transaction | ||
neko259
|
r977 | from django.http import Http404 | ||
neko259
|
r542 | from django.shortcuts import render, redirect | ||
neko259
|
r954 | import requests | ||
neko259
|
r542 | |||
neko259
|
r716 | from boards import utils, settings | ||
neko259
|
r596 | from boards.abstracts.paginator import get_paginator | ||
neko259
|
r730 | from boards.abstracts.settingsmanager import get_settings_manager | ||
neko259
|
r542 | from boards.forms import ThreadForm, PlainErrorList | ||
neko259
|
r1148 | from boards.models import Post, Thread, Ban, Tag, PostImage, Banner | ||
neko259
|
r542 | from boards.views.banned import BannedView | ||
neko259
|
r722 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
neko259
|
r542 | from boards.views.posting_mixin import PostMixin | ||
neko259
|
r900 | |||
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
|
r1149 | PARAMETER_BANNERS = 'banners' | ||
neko259
|
r542 | |||
neko259
|
r1129 | PARAMETER_PREV_LINK = 'prev_page_link' | ||
PARAMETER_NEXT_LINK = 'next_page_link' | ||||
neko259
|
r1164 | TEMPLATE = 'boards/all_threads.html' | ||
neko259
|
r542 | DEFAULT_PAGE = 1 | ||
class AllThreadsView(PostMixin, BaseBoardView): | ||||
neko259
|
r728 | def __init__(self): | ||
self.settings_manager = None | ||||
super(AllThreadsView, self).__init__() | ||||
neko259
|
r635 | |||
neko259
|
r1204 | def get(self, request, form: ThreadForm=None): | ||
page = request.GET.get('page', DEFAULT_PAGE) | ||||
neko259
|
r919 | params = self.get_context_data(request=request) | ||
neko259
|
r542 | |||
neko259
|
r544 | if not form: | ||
form = ThreadForm(error_class=PlainErrorList) | ||||
neko259
|
r542 | |||
neko259
|
r730 | self.settings_manager = get_settings_manager(request) | ||
neko259
|
r596 | paginator = get_paginator(self.get_threads(), | ||
neko259
|
r1153 | settings.get_int('View', 'ThreadsPerPage')) | ||
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
|
r1149 | params[PARAMETER_BANNERS] = Banner.objects.order_by('-id').all() | ||
neko259
|
r542 | |||
neko259
|
r1129 | self.get_page_context(paginator, params, page) | ||
neko259
|
r542 | |||
neko259
|
r918 | return render(request, TEMPLATE, params) | ||
neko259
|
r542 | |||
neko259
|
r1204 | def post(self, request): | ||
neko259
|
r542 | form = ThreadForm(request.POST, request.FILES, | ||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
neko259
|
r634 | return self.create_thread(request, form) | ||
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
|
r1129 | def get_page_context(self, paginator, params, page): | ||
neko259
|
r542 | """ | ||
Get pagination context variables | ||||
""" | ||||
neko259
|
r918 | params[PARAMETER_PAGINATOR] = paginator | ||
neko259
|
r1129 | current_page = paginator.page(int(page)) | ||
params[PARAMETER_CURRENT_PAGE] = current_page | ||||
if current_page.has_previous(): | ||||
params[PARAMETER_PREV_LINK] = self.get_previous_page_link( | ||||
current_page) | ||||
if current_page.has_next(): | ||||
params[PARAMETER_NEXT_LINK] = self.get_next_page_link(current_page) | ||||
def get_previous_page_link(self, current_page): | ||||
neko259
|
r1204 | return reverse('index') + '?page=' \ | ||
+ str(current_page.previous_page_number()) | ||||
neko259
|
r1129 | |||
def get_next_page_link(self, current_page): | ||||
neko259
|
r1204 | return reverse('index') + '?page=' \ | ||
+ str(current_page.next_page_number()) | ||||
neko259
|
r542 | |||
neko259
|
r690 | @staticmethod | ||
def parse_tags_string(tag_strings): | ||||
neko259
|
r634 | """ | ||
Parses tag list string and returns tag object list. | ||||
""" | ||||
tags = [] | ||||
if tag_strings: | ||||
tag_strings = tag_strings.split(TAG_DELIMITER) | ||||
for tag_name in tag_strings: | ||||
neko259
|
r766 | tag_name = tag_name.strip().lower() | ||
neko259
|
r634 | if len(tag_name) > 0: | ||
tag, created = Tag.objects.get_or_create(name=tag_name) | ||||
tags.append(tag) | ||||
return tags | ||||
neko259
|
r542 | @transaction.atomic | ||
neko259
|
r954 | def create_thread(self, request, form: ThreadForm, html_response=True): | ||
neko259
|
r542 | """ | ||
neko259
|
r634 | Creates a new thread with an opening post. | ||
neko259
|
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 | ||||
neko259
|
r722 | title = data[FORM_TITLE] | ||
text = data[FORM_TEXT] | ||||
neko259
|
r1273 | file = form.get_file() | ||
neko259
|
r1077 | threads = data[FORM_THREADS] | ||
neko259
|
r542 | |||
text = self._remove_invalid_links(text) | ||||
neko259
|
r722 | tag_strings = data[FORM_TAGS] | ||
neko259
|
r542 | |||
neko259
|
r634 | tags = self.parse_tags_string(tag_strings) | ||
neko259
|
r542 | |||
neko259
|
r1273 | post = Post.objects.create_post(title=title, text=text, file=file, | ||
neko259
|
r1221 | ip=ip, tags=tags, opening_posts=threads) | ||
neko259
|
r900 | |||
# This is required to update the threads to which posts we have replied | ||||
# when creating this one | ||||
neko259
|
r1091 | post.notify_clients() | ||
neko259
|
r542 | |||
if html_response: | ||||
neko259
|
r1160 | return redirect(post.get_absolute_url()) | ||
neko259
|
r542 | |||
def get_threads(self): | ||||
neko259
|
r634 | """ | ||
Gets list of threads that will be shown on a page. | ||||
""" | ||||
neko259
|
r942 | return Thread.objects.order_by('-bump_time')\ | ||
neko259
|
r728 | .exclude(tags__in=self.settings_manager.get_hidden_tags()) | ||