thread.py
129 lines
| 4.3 KiB
| text/x-python
|
PythonLexer
|
r542 | import string | ||
from django.core.urlresolvers import reverse | ||||
from django.db import transaction | ||||
from django.http import Http404 | ||||
from django.shortcuts import get_object_or_404, render, redirect | ||||
|
r633 | from django.views.generic.edit import FormMixin | ||
|
r542 | from boards import utils | ||
from boards.forms import PostForm, PlainErrorList | ||||
from boards.models import Post, Ban, Tag | ||||
from boards.views.banned import BannedView | ||||
from boards.views.base import BaseBoardView, PARAMETER_FORM | ||||
from boards.views.posting_mixin import PostMixin | ||||
import neboard | ||||
MODE_GALLERY = 'gallery' | ||||
MODE_NORMAL = 'normal' | ||||
|
r633 | PARAMETER_MAX_REPLIES = 'max_replies' | ||
PARAMETER_THREAD = 'thread' | ||||
PARAMETER_BUMPABLE = 'bumpable' | ||||
|
r542 | |||
|
r633 | |||
class ThreadView(BaseBoardView, PostMixin, FormMixin): | ||||
|
r542 | |||
|
r544 | def get(self, request, post_id, mode=MODE_NORMAL, form=None): | ||
|
r659 | opening_post = Post.objects.filter(id=post_id).only('thread_new')[0] | ||
|
r542 | |||
# If this is not OP, don't show it as it is | ||||
|
r659 | if not opening_post or not opening_post.is_opening(): | ||
|
r542 | raise Http404 | ||
|
r544 | if not form: | ||
form = PostForm(error_class=PlainErrorList) | ||||
|
r542 | |||
|
r617 | thread_to_show = opening_post.get_thread() | ||
|
r542 | |||
context = self.get_context_data(request=request) | ||||
context[PARAMETER_FORM] = form | ||||
context["last_update"] = utils.datetime_to_epoch( | ||||
thread_to_show.last_edit_time) | ||||
|
r633 | context[PARAMETER_THREAD] = thread_to_show | ||
context[PARAMETER_MAX_REPLIES] = neboard.settings.MAX_POSTS_PER_THREAD | ||||
|
r542 | |||
if MODE_NORMAL == mode: | ||||
|
r633 | context[PARAMETER_BUMPABLE] = thread_to_show.can_bump() | ||
if context[PARAMETER_BUMPABLE]: | ||||
|
r569 | context['posts_left'] = neboard.settings.MAX_POSTS_PER_THREAD \ | ||
- thread_to_show.get_reply_count() | ||||
|
r542 | context['bumplimit_progress'] = str( | ||
float(context['posts_left']) / | ||||
neboard.settings.MAX_POSTS_PER_THREAD * 100) | ||||
|
r625 | context['opening_post'] = opening_post | ||
|
r542 | |||
document = 'boards/thread.html' | ||||
elif MODE_GALLERY == mode: | ||||
|
r659 | posts = thread_to_show.get_replies(view_fields_only=True) | ||
|
r542 | context['posts'] = posts.filter(image_width__gt=0) | ||
document = 'boards/thread_gallery.html' | ||||
else: | ||||
raise Http404 | ||||
return render(request, document, context) | ||||
def post(self, request, post_id, mode=MODE_NORMAL): | ||||
opening_post = get_object_or_404(Post, id=post_id) | ||||
# If this is not OP, don't show it as it is | ||||
if not opening_post.is_opening(): | ||||
raise Http404 | ||||
|
r617 | if not opening_post.get_thread().archived: | ||
|
r542 | form = PostForm(request.POST, request.FILES, | ||
error_class=PlainErrorList) | ||||
form.session = request.session | ||||
if form.is_valid(): | ||||
return self.new_post(request, form, opening_post) | ||||
if form.need_to_ban: | ||||
# Ban user because he is suspected to be a bot | ||||
self._ban_current_user(request) | ||||
|
r544 | return self.get(request, post_id, mode, form) | ||
|
r542 | |||
@transaction.atomic | ||||
def new_post(self, request, form, opening_post=None, html_response=True): | ||||
"""Add a new post (in thread or as a reply).""" | ||||
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: | ||||
|
r640 | return None | ||
|
r542 | |||
data = form.cleaned_data | ||||
title = data['title'] | ||||
text = data['text'] | ||||
text = self._remove_invalid_links(text) | ||||
if 'image' in data.keys(): | ||||
image = data['image'] | ||||
else: | ||||
image = None | ||||
tags = [] | ||||
|
r617 | post_thread = opening_post.get_thread() | ||
|
r542 | |||
post = Post.objects.create_post(title=title, text=text, ip=ip, | ||||
thread=post_thread, image=image, | ||||
tags=tags, | ||||
user=self._get_user(request)) | ||||
thread_to_show = (opening_post.id if opening_post else post.id) | ||||
if html_response: | ||||
if opening_post: | ||||
|
r561 | return redirect(reverse( | ||
'thread', | ||||
kwargs={'post_id': thread_to_show}) + '#' + str(post.id)) | ||||
|
r638 | else: | ||
|
r640 | return post | ||