thread.py
121 lines
| 3.6 KiB
| text/x-python
|
PythonLexer
neko259
|
r1014 | from django.core.exceptions import ObjectDoesNotExist | ||
neko259
|
r951 | from django.http import Http404 | ||
from django.shortcuts import get_object_or_404, render, redirect | ||||
from django.views.generic.edit import FormMixin | ||||
from boards import utils, settings | ||||
from boards.forms import PostForm, PlainErrorList | ||||
neko259
|
r1040 | from boards.models import Post | ||
neko259
|
r951 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
from boards.views.posting_mixin import PostMixin | ||||
import neboard | ||||
neko259
|
r1040 | |||
neko259
|
r951 | CONTEXT_LASTUPDATE = "last_update" | ||
CONTEXT_THREAD = 'thread' | ||||
CONTEXT_WS_TOKEN = 'ws_token' | ||||
CONTEXT_WS_PROJECT = 'ws_project' | ||||
CONTEXT_WS_HOST = 'ws_host' | ||||
CONTEXT_WS_PORT = 'ws_port' | ||||
FORM_TITLE = 'title' | ||||
FORM_TEXT = 'text' | ||||
FORM_IMAGE = 'image' | ||||
class ThreadView(BaseBoardView, PostMixin, FormMixin): | ||||
neko259
|
r954 | def get(self, request, post_id, form: PostForm=None): | ||
neko259
|
r951 | try: | ||
neko259
|
r1013 | opening_post = Post.objects.get(id=post_id) | ||
neko259
|
r1014 | except ObjectDoesNotExist: | ||
neko259
|
r951 | raise Http404 | ||
# If this is not OP, don't show it as it is | ||||
neko259
|
r1013 | if not opening_post.is_opening(): | ||
return redirect(opening_post.get_thread().get_opening_post().get_url()) | ||||
neko259
|
r951 | |||
if not form: | ||||
form = PostForm(error_class=PlainErrorList) | ||||
thread_to_show = opening_post.get_thread() | ||||
params = dict() | ||||
params[CONTEXT_FORM] = form | ||||
params[CONTEXT_LASTUPDATE] = str(utils.datetime_to_epoch( | ||||
thread_to_show.last_edit_time)) | ||||
params[CONTEXT_THREAD] = thread_to_show | ||||
if settings.WEBSOCKETS_ENABLED: | ||||
params[CONTEXT_WS_TOKEN] = utils.get_websocket_token( | ||||
timestamp=params[CONTEXT_LASTUPDATE]) | ||||
params[CONTEXT_WS_PROJECT] = neboard.settings.CENTRIFUGE_PROJECT_ID | ||||
params[CONTEXT_WS_HOST] = request.get_host().split(':')[0] | ||||
params[CONTEXT_WS_PORT] = neboard.settings.CENTRIFUGE_PORT | ||||
params.update(self.get_data(thread_to_show)) | ||||
return render(request, self.get_template(), params) | ||||
def post(self, request, post_id): | ||||
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 | ||||
if not opening_post.get_thread().archived: | ||||
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) | ||||
return self.get(request, post_id, form) | ||||
neko259
|
r954 | def new_post(self, request, form: PostForm, opening_post: Post=None, | ||
html_response=True): | ||||
neko259
|
r951 | """ | ||
Adds a new post (in thread or as a reply). | ||||
""" | ||||
ip = utils.get_client_ip(request) | ||||
data = form.cleaned_data | ||||
title = data[FORM_TITLE] | ||||
text = data[FORM_TEXT] | ||||
neko259
|
r954 | image = form.get_image() | ||
neko259
|
r951 | |||
text = self._remove_invalid_links(text) | ||||
post_thread = opening_post.get_thread() | ||||
post = Post.objects.create_post(title=title, text=text, image=image, | ||||
thread=post_thread, ip=ip) | ||||
post.send_to_websocket(request) | ||||
if html_response: | ||||
if opening_post: | ||||
neko259
|
r954 | return redirect(post.get_url()) | ||
neko259
|
r951 | else: | ||
return post | ||||
def get_data(self, thread): | ||||
""" | ||||
Returns context params for the view. | ||||
""" | ||||
pass | ||||
def get_template(self): | ||||
""" | ||||
Gets template to show the thread mode on. | ||||
""" | ||||
pass | ||||