thread.py
181 lines
| 6.0 KiB
| text/x-python
|
PythonLexer
|
r1366 | from django.contrib.auth.decorators import permission_required | ||
|
r1014 | from django.core.exceptions import ObjectDoesNotExist | ||
|
r1399 | from django.core.urlresolvers import reverse | ||
|
r951 | from django.http import Http404 | ||
from django.shortcuts import get_object_or_404, render, redirect | ||||
|
r1422 | from django.template.context_processors import csrf | ||
from django.utils.decorators import method_decorator | ||||
from django.views.decorators.csrf import csrf_protect | ||||
|
r951 | from django.views.generic.edit import FormMixin | ||
|
r1086 | from django.utils import timezone | ||
from django.utils.dateformat import format | ||||
|
r951 | |||
from boards import utils, settings | ||||
|
r1323 | from boards.abstracts.settingsmanager import get_settings_manager | ||
|
r951 | from boards.forms import PostForm, PlainErrorList | ||
|
r1040 | from boards.models import Post | ||
|
r951 | from boards.views.base import BaseBoardView, CONTEXT_FORM | ||
|
r1366 | from boards.views.mixins import DispatcherMixin, PARAMETER_METHOD | ||
|
r951 | from boards.views.posting_mixin import PostMixin | ||
import neboard | ||||
|
r1366 | REQ_POST_ID = 'post_id' | ||
|
r1040 | |||
|
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' | ||||
|
r1086 | CONTEXT_WS_TIME = 'ws_token_time' | ||
|
r1180 | CONTEXT_MODE = 'mode' | ||
|
r1185 | CONTEXT_OP = 'opening_post' | ||
|
r1323 | CONTEXT_FAVORITE = 'is_favorite' | ||
|
r1399 | CONTEXT_RSS_URL = 'rss_url' | ||
|
r951 | |||
FORM_TITLE = 'title' | ||||
FORM_TEXT = 'text' | ||||
FORM_IMAGE = 'image' | ||||
|
r1077 | FORM_THREADS = 'threads' | ||
|
r951 | |||
|
r1323 | class ThreadView(BaseBoardView, PostMixin, FormMixin, DispatcherMixin): | ||
|
r951 | |||
|
r1422 | @method_decorator(csrf_protect) | ||
|
r954 | def get(self, request, post_id, form: PostForm=None): | ||
|
r951 | try: | ||
|
r1013 | opening_post = Post.objects.get(id=post_id) | ||
|
r1014 | except ObjectDoesNotExist: | ||
|
r951 | raise Http404 | ||
|
r1323 | # If the tag is favorite, update the counter | ||
settings_manager = get_settings_manager(request) | ||||
favorite = settings_manager.thread_is_fav(opening_post) | ||||
if favorite: | ||||
settings_manager.add_or_read_fav_thread(opening_post) | ||||
|
r951 | # If this is not OP, don't show it as it is | ||
|
r1013 | if not opening_post.is_opening(): | ||
|
r1160 | return redirect(opening_post.get_thread().get_opening_post() | ||
|
r1323 | .get_absolute_url()) | ||
|
r951 | |||
if not form: | ||||
form = PostForm(error_class=PlainErrorList) | ||||
thread_to_show = opening_post.get_thread() | ||||
params = dict() | ||||
params[CONTEXT_FORM] = form | ||||
|
r1086 | params[CONTEXT_LASTUPDATE] = str(thread_to_show.last_edit_time) | ||
|
r951 | params[CONTEXT_THREAD] = thread_to_show | ||
|
r1180 | params[CONTEXT_MODE] = self.get_mode() | ||
|
r1185 | params[CONTEXT_OP] = opening_post | ||
|
r1323 | params[CONTEXT_FAVORITE] = favorite | ||
|
r1399 | params[CONTEXT_RSS_URL] = self.get_rss_url(post_id) | ||
|
r951 | |||
|
r1153 | if settings.get_bool('External', 'WebsocketsEnabled'): | ||
|
r1086 | token_time = format(timezone.now(), u'U') | ||
params[CONTEXT_WS_TIME] = token_time | ||||
|
r951 | params[CONTEXT_WS_TOKEN] = utils.get_websocket_token( | ||
|
r1086 | timestamp=token_time) | ||
|
r951 | 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) | ||||
|
r1422 | @method_decorator(csrf_protect) | ||
|
r951 | 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 | ||||
|
r1366 | if PARAMETER_METHOD in request.POST: | ||
|
r1324 | self.dispatch_method(request, opening_post) | ||
return redirect('thread', post_id) # FIXME Different for different modes | ||||
|
r1414 | if not opening_post.get_thread().is_archived(): | ||
|
r951 | 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) | ||||
|
r954 | def new_post(self, request, form: PostForm, opening_post: Post=None, | ||
html_response=True): | ||||
|
r951 | """ | ||
Adds a new post (in thread or as a reply). | ||||
""" | ||||
ip = utils.get_client_ip(request) | ||||
data = form.cleaned_data | ||||
|
r1299 | title = form.get_title() | ||
|
r951 | text = data[FORM_TEXT] | ||
|
r1273 | file = form.get_file() | ||
|
r1660 | file_url = form.get_file_url() | ||
|
r1500 | images = form.get_images() | ||
|
r951 | |||
text = self._remove_invalid_links(text) | ||||
post_thread = opening_post.get_thread() | ||||
|
r1273 | post = Post.objects.create_post(title=title, text=text, file=file, | ||
|
r1091 | thread=post_thread, ip=ip, | ||
|
r1500 | tripcode=form.get_tripcode(), | ||
|
r1660 | images=images, file_url=file_url) | ||
|
r1091 | post.notify_clients() | ||
|
r951 | |||
|
r1625 | if form.is_subscribe(): | ||
settings_manager = get_settings_manager(request) | ||||
settings_manager.add_or_read_fav_thread( | ||||
post_thread.get_opening_post()) | ||||
|
r951 | if html_response: | ||
if opening_post: | ||||
|
r1160 | return redirect(post.get_absolute_url()) | ||
|
r951 | else: | ||
return post | ||||
|
r1185 | def get_data(self, thread) -> dict: | ||
|
r951 | """ | ||
Returns context params for the view. | ||||
""" | ||||
|
r1185 | return dict() | ||
|
r951 | |||
|
r1185 | def get_template(self) -> str: | ||
|
r951 | """ | ||
Gets template to show the thread mode on. | ||||
""" | ||||
pass | ||||
|
r1180 | |||
|
r1185 | def get_mode(self) -> str: | ||
|
r1180 | pass | ||
|
r1323 | |||
def subscribe(self, request, opening_post): | ||||
settings_manager = get_settings_manager(request) | ||||
settings_manager.add_or_read_fav_thread(opening_post) | ||||
def unsubscribe(self, request, opening_post): | ||||
settings_manager = get_settings_manager(request) | ||||
settings_manager.del_fav_thread(opening_post) | ||||
|
r1366 | |||
|
r1399 | def get_rss_url(self, opening_id): | ||
|
r1400 | return reverse('thread', kwargs={'post_id': opening_id}) + 'rss/' | ||