2022-07-12 19:28:20 -07:00
|
|
|
import functools
|
2022-06-23 16:03:09 -07:00
|
|
|
from typing import Any, Dict
|
2022-05-04 21:14:00 -07:00
|
|
|
from django.conf import settings
|
2022-07-12 19:28:20 -07:00
|
|
|
from django.contrib.auth import get_user
|
2022-06-23 16:03:09 -07:00
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
2022-06-23 18:16:00 -07:00
|
|
|
from django.db.models import Q
|
2022-05-03 18:02:04 -07:00
|
|
|
from django.http import Http404, HttpResponseRedirect
|
2022-06-28 23:55:44 -07:00
|
|
|
from django.http.request import QueryDict
|
2022-06-20 15:26:35 -07:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
from django.views.generic.base import TemplateView
|
2022-06-24 16:50:46 -07:00
|
|
|
from django.views.generic import edit
|
2022-06-14 14:56:50 -07:00
|
|
|
from django.urls import reverse, reverse_lazy
|
2022-06-21 16:59:32 -07:00
|
|
|
from django.utils import timezone
|
2022-06-28 21:37:45 -07:00
|
|
|
|
|
|
|
|
from guardian.shortcuts import get_objects_for_user
|
|
|
|
|
|
2022-06-30 16:21:36 -07:00
|
|
|
from board.forms import *
|
2022-06-23 18:16:00 -07:00
|
|
|
from board.models import Ban, BanTemplate, Board, Post, Report
|
2022-06-24 16:50:46 -07:00
|
|
|
from board.utils import *
|
2022-05-03 18:02:04 -07:00
|
|
|
|
2022-06-23 16:03:09 -07:00
|
|
|
__all__ = (
|
|
|
|
|
"BanCreateView",
|
|
|
|
|
"BanSuccessView",
|
|
|
|
|
"BannedView",
|
|
|
|
|
"BoardView",
|
2022-06-28 23:55:44 -07:00
|
|
|
"PostCreateView",
|
2022-06-30 16:21:36 -07:00
|
|
|
"PostModifyView",
|
2022-06-30 18:16:08 -07:00
|
|
|
"PostModifySuccessView",
|
2022-06-23 16:03:09 -07:00
|
|
|
"PostView",
|
2022-06-25 20:42:10 -07:00
|
|
|
"PostSuccessView",
|
|
|
|
|
"ReplyCreateView",
|
2022-06-23 16:03:09 -07:00
|
|
|
"ReportView",
|
2022-06-24 16:21:41 -07:00
|
|
|
"ReportSuccessView",
|
2022-06-23 16:03:09 -07:00
|
|
|
)
|
2022-05-03 18:02:04 -07:00
|
|
|
|
|
|
|
|
|
2022-06-30 18:16:08 -07:00
|
|
|
def can_modify(user):
|
|
|
|
|
if not user:
|
|
|
|
|
return False
|
|
|
|
|
# TODO add more permissions as required
|
2022-07-12 19:28:20 -07:00
|
|
|
return (
|
|
|
|
|
user.has_perm("board.set_sticky")
|
|
|
|
|
or user.has_perm("board.set_bump")
|
|
|
|
|
or user.has_perm("board.set_lock")
|
|
|
|
|
)
|
2022-06-30 18:16:08 -07:00
|
|
|
|
|
|
|
|
|
2022-06-20 15:26:35 -07:00
|
|
|
class BannedView(TemplateView):
|
|
|
|
|
template_name = "board/banned.html"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super(TemplateView, self).get_context_data(**kwargs)
|
|
|
|
|
ip = get_client_ip(self.request)
|
|
|
|
|
bans = get_ip_bans(ip)
|
2022-06-21 16:59:32 -07:00
|
|
|
|
|
|
|
|
now = timezone.now()
|
2022-06-26 20:27:42 -07:00
|
|
|
active_bans = [ban for ban in bans if not ban.expires or ban.expires > now]
|
2022-06-20 15:26:35 -07:00
|
|
|
context["bans"] = bans
|
2022-06-21 16:59:32 -07:00
|
|
|
context["active_bans"] = active_bans
|
2022-06-20 15:26:35 -07:00
|
|
|
context["ip"] = ip
|
2022-06-21 16:59:32 -07:00
|
|
|
|
2022-06-20 15:26:35 -07:00
|
|
|
return context
|
2022-05-03 18:02:04 -07:00
|
|
|
|
|
|
|
|
|
2022-07-12 19:28:20 -07:00
|
|
|
class BoardMixin:
|
|
|
|
|
@functools.cached_property
|
|
|
|
|
def board(self) -> Board:
|
|
|
|
|
return get_object_or_404(Board, url=self.kwargs["url"]) # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CreateView(BoardMixin, edit.CreateView):
|
2022-05-07 13:24:43 -07:00
|
|
|
"""
|
2022-06-24 16:50:46 -07:00
|
|
|
Helper class that sets a few variables for posts and check against bans.
|
|
|
|
|
This should not be used by itself.
|
2022-05-07 13:24:43 -07:00
|
|
|
|
|
|
|
|
This class sets the following variables on GET and POST:
|
|
|
|
|
* self.board
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
2022-06-24 16:50:46 -07:00
|
|
|
kwargs = super(CreateView, self).get_form_kwargs()
|
2022-05-07 13:24:43 -07:00
|
|
|
kwargs["board"] = self.board
|
|
|
|
|
kwargs["ip"] = get_client_ip(self.request)
|
|
|
|
|
return kwargs
|
|
|
|
|
|
2022-06-20 15:26:35 -07:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
2022-06-24 16:50:46 -07:00
|
|
|
# Check for bans
|
2022-06-21 16:59:32 -07:00
|
|
|
ip = get_client_ip(request)
|
2022-06-24 16:50:46 -07:00
|
|
|
if request.method == "POST" and is_banned(ip, self.board):
|
|
|
|
|
return HttpResponseRedirect(reverse("board:banned"))
|
|
|
|
|
else:
|
|
|
|
|
return super(CreateView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
2022-07-12 19:28:20 -07:00
|
|
|
class BoardView(BoardMixin, TemplateView):
|
2022-06-28 23:55:44 -07:00
|
|
|
model = Board
|
2022-05-03 18:02:04 -07:00
|
|
|
slug_field = "url"
|
|
|
|
|
slug_url_kwarg = "url"
|
|
|
|
|
template_name = "board/board_detail.html"
|
|
|
|
|
|
2022-05-07 16:31:32 -07:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
# If the page isn't set, then redirect to the /page/1 url
|
|
|
|
|
if "page" not in kwargs:
|
|
|
|
|
return HttpResponseRedirect(
|
|
|
|
|
reverse("board:board_detail", kwargs={"url": kwargs["url"], "page": 1})
|
|
|
|
|
)
|
|
|
|
|
return super(BoardView, self).get(request, *args, **kwargs)
|
|
|
|
|
|
2022-05-03 18:02:04 -07:00
|
|
|
def get_context_data(self, **kwargs):
|
2022-05-07 16:31:32 -07:00
|
|
|
page = self.kwargs["page"]
|
2022-05-03 18:02:04 -07:00
|
|
|
|
2022-05-07 13:24:43 -07:00
|
|
|
if page not in range(1, self.board.max_pages + 1):
|
2022-05-03 18:02:04 -07:00
|
|
|
raise Http404()
|
|
|
|
|
|
2022-05-07 15:14:50 -07:00
|
|
|
threads_per_page = self.board.threads_per_page
|
|
|
|
|
start = (page - 1) * threads_per_page
|
|
|
|
|
end = start + threads_per_page
|
|
|
|
|
thread_count = self.board.threads.count()
|
|
|
|
|
last_page = (thread_count // threads_per_page) + 1
|
|
|
|
|
|
|
|
|
|
kwargs["board"] = self.board
|
2022-06-30 16:21:36 -07:00
|
|
|
kwargs["threads"] = self.board.threads.order_by("-sticky", "-last_bump")[
|
|
|
|
|
start:end
|
|
|
|
|
]
|
2022-05-07 15:14:50 -07:00
|
|
|
kwargs["current_page"] = page
|
|
|
|
|
kwargs["pages"] = range(1, last_page + 1)
|
|
|
|
|
kwargs["last_page"] = last_page
|
2022-05-04 21:14:00 -07:00
|
|
|
kwargs["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
2022-06-30 18:16:08 -07:00
|
|
|
kwargs["can_modify"] = can_modify(self.request.user)
|
2022-05-07 15:14:50 -07:00
|
|
|
|
2022-05-03 18:02:04 -07:00
|
|
|
return super(BoardView, self).get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
2022-06-28 23:55:44 -07:00
|
|
|
class PostCreateView(CreateView):
|
|
|
|
|
model = Post
|
|
|
|
|
form_class = PostForm
|
|
|
|
|
slug_field = "url"
|
|
|
|
|
slug_url_kwarg = "url"
|
|
|
|
|
template_name = "board/post_create_view.html"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["board"] = self.board
|
|
|
|
|
context["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
|
|
|
|
context["capcodes"] = get_objects_for_user(
|
|
|
|
|
get_user(self.request), "board.use_capcode"
|
|
|
|
|
)
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
|
kwargs = super(PostCreateView, self).get_form_kwargs()
|
|
|
|
|
kwargs["user"] = self.request.user
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
def get_success_url(self) -> str:
|
|
|
|
|
query = QueryDict(mutable=True)
|
|
|
|
|
query["next"] = self.get_form().instance.get_absolute_url()
|
|
|
|
|
return reverse("board:post_success") + "?" + query.urlencode()
|
|
|
|
|
|
|
|
|
|
|
2022-06-30 16:21:36 -07:00
|
|
|
class PostModifyView(PermissionRequiredMixin, edit.UpdateView):
|
|
|
|
|
model = Post
|
|
|
|
|
form_class = PostModifyForm
|
|
|
|
|
template_name = "board/post_modify_view.html"
|
2022-06-30 18:16:08 -07:00
|
|
|
success_url = reverse_lazy("board:post_modify_success")
|
2022-06-30 16:21:36 -07:00
|
|
|
|
|
|
|
|
def has_permission(self) -> bool:
|
2022-06-30 18:16:08 -07:00
|
|
|
return can_modify(self.request.user)
|
2022-06-30 16:21:36 -07:00
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
|
self.post_obj = get_object_or_404(Post, id=kwargs["pk"])
|
|
|
|
|
self.board = self.post_obj.board
|
|
|
|
|
return super(PostModifyView, self).dispatch(request, *args, **kwargs)
|
|
|
|
|
|
2022-06-30 18:16:08 -07:00
|
|
|
def get_form_kwargs(self) -> Dict[str, Any]:
|
|
|
|
|
kwargs = super(PostModifyView, self).get_form_kwargs()
|
|
|
|
|
kwargs["user"] = self.request.user
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PostModifySuccessView(PermissionRequiredMixin, TemplateView):
|
|
|
|
|
template_name = "board/post_modify_success.html"
|
|
|
|
|
|
|
|
|
|
def has_permission(self) -> bool:
|
|
|
|
|
return can_modify(self.request.user)
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["window_timeout"] = settings.BAN_WINDOW_CLOSE_TIMEOUT
|
|
|
|
|
context["can_modify"] = self.has_permission()
|
|
|
|
|
return context
|
|
|
|
|
|
2022-06-30 16:21:36 -07:00
|
|
|
|
2022-06-25 20:42:10 -07:00
|
|
|
class ReplyCreateView(CreateView):
|
|
|
|
|
model = Post
|
|
|
|
|
form_class = ReplyForm
|
|
|
|
|
slug_field = "url"
|
|
|
|
|
slug_url_kwarg = "url"
|
|
|
|
|
template_name = "board/reply_create_view.html"
|
|
|
|
|
success_url = reverse_lazy("board:post_success")
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["board"] = self.board
|
|
|
|
|
post_id = self.kwargs["id"]
|
|
|
|
|
context["post"] = get_object_or_404(Post, id=post_id)
|
|
|
|
|
context["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
2022-06-28 21:37:45 -07:00
|
|
|
context["capcodes"] = get_objects_for_user(
|
|
|
|
|
get_user(self.request), "board.use_capcode"
|
|
|
|
|
)
|
2022-06-25 20:42:10 -07:00
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
|
kwargs = super(ReplyCreateView, self).get_form_kwargs()
|
|
|
|
|
post_id = self.kwargs["id"]
|
|
|
|
|
post = get_object_or_404(Post, id=post_id)
|
|
|
|
|
kwargs["op"] = post
|
2022-06-28 21:37:45 -07:00
|
|
|
kwargs["user"] = self.request.user
|
2022-06-25 20:42:10 -07:00
|
|
|
return kwargs
|
|
|
|
|
|
2022-06-28 23:55:44 -07:00
|
|
|
def get_success_url(self) -> str:
|
|
|
|
|
query = QueryDict(mutable=True)
|
|
|
|
|
query["next"] = self.get_form().instance.get_absolute_url()
|
|
|
|
|
return reverse("board:post_success") + "?" + query.urlencode()
|
2022-06-25 20:42:10 -07:00
|
|
|
|
2022-06-28 23:55:44 -07:00
|
|
|
|
|
|
|
|
class PostView(TemplateView):
|
2022-05-03 18:02:04 -07:00
|
|
|
model = Post
|
|
|
|
|
slug_field = "url"
|
|
|
|
|
slug_url_kwarg = "url"
|
|
|
|
|
|
|
|
|
|
template_name = "board/post_detail.html"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2022-05-07 13:24:43 -07:00
|
|
|
kwargs["board"] = self.board
|
2022-05-03 18:02:04 -07:00
|
|
|
post_id = self.kwargs["id"]
|
|
|
|
|
kwargs["post"] = get_object_or_404(Post, id=post_id)
|
2022-05-04 21:14:00 -07:00
|
|
|
kwargs["max_upload_size"] = settings.MAX_UPLOAD_SIZE
|
2022-06-30 18:52:44 -07:00
|
|
|
kwargs["can_modify"] = can_modify(self.request.user)
|
2022-05-04 21:14:00 -07:00
|
|
|
|
2022-05-03 18:02:04 -07:00
|
|
|
return super(PostView, self).get_context_data(**kwargs)
|
|
|
|
|
|
2022-06-28 23:55:44 -07:00
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
|
# Set the board on this object
|
|
|
|
|
self.board = get_object_or_404(Board, url=kwargs["url"])
|
|
|
|
|
return super(PostView, self).dispatch(request, *args, **kwargs)
|
2022-06-14 14:56:50 -07:00
|
|
|
|
|
|
|
|
|
2022-06-25 20:42:10 -07:00
|
|
|
class PostSuccessView(TemplateView):
|
|
|
|
|
template_name = "board/post_success.html"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["window_timeout"] = settings.POST_WINDOW_CLOSE_TIMEOUT
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
2022-06-24 16:50:46 -07:00
|
|
|
class ReportView(CreateView):
|
2022-06-14 14:56:50 -07:00
|
|
|
model = Report
|
|
|
|
|
form_class = ReportForm
|
|
|
|
|
success_url = reverse_lazy("board:report_success")
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def board_url(self) -> str:
|
|
|
|
|
return self.kwargs["url"]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def post_id(self) -> int:
|
|
|
|
|
return self.kwargs["id"]
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
|
kwargs = super(ReportView, self).get_form_kwargs()
|
|
|
|
|
post_id = self.kwargs["id"]
|
|
|
|
|
post = get_object_or_404(Post, id=post_id)
|
|
|
|
|
kwargs["op"] = post
|
|
|
|
|
return kwargs
|
2022-06-23 16:03:09 -07:00
|
|
|
|
|
|
|
|
|
2022-06-24 16:21:41 -07:00
|
|
|
class ReportSuccessView(TemplateView):
|
|
|
|
|
template_name = "board/report_success.html"
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["window_timeout"] = settings.REPORT_WINDOW_CLOSE_TIMEOUT
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
|
2022-06-25 20:42:10 -07:00
|
|
|
class BanCreateView(PermissionRequiredMixin, edit.CreateView):
|
2022-06-23 16:03:09 -07:00
|
|
|
model = Ban
|
|
|
|
|
form_class = BanForm
|
2022-06-30 18:16:08 -07:00
|
|
|
permission_required = "board.add_ban"
|
2022-06-23 16:03:09 -07:00
|
|
|
success_url = reverse_lazy("board:ban_success")
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["board"] = self.board
|
|
|
|
|
context["post"] = self.post_obj
|
|
|
|
|
ip = self.post_obj.ip
|
|
|
|
|
now = timezone.now()
|
|
|
|
|
bans = [
|
|
|
|
|
ban
|
|
|
|
|
for ban in get_ip_bans(ip)
|
|
|
|
|
if ban.board == self.board or ban.board is None
|
|
|
|
|
]
|
|
|
|
|
context["previous_bans"] = [
|
2022-06-26 20:27:42 -07:00
|
|
|
ban for ban in bans if ban.expires and ban.expires < now
|
2022-06-23 16:03:09 -07:00
|
|
|
]
|
|
|
|
|
context["current_bans"] = [
|
2022-06-26 20:27:42 -07:00
|
|
|
ban for ban in bans if not ban.expires or ban.expires > now
|
2022-06-23 16:03:09 -07:00
|
|
|
]
|
2022-06-23 18:16:00 -07:00
|
|
|
context["templates"] = BanTemplate.objects.filter(
|
|
|
|
|
Q(board=self.board) | Q(board=None)
|
|
|
|
|
)
|
2022-06-23 16:03:09 -07:00
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self) -> Dict[str, Any]:
|
2022-06-25 20:42:10 -07:00
|
|
|
kwargs = super(edit.CreateView, self).get_form_kwargs()
|
2022-06-23 16:03:09 -07:00
|
|
|
post_id = self.kwargs["id"]
|
|
|
|
|
post = get_object_or_404(Post, id=post_id)
|
|
|
|
|
kwargs["op"] = post
|
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
|
self.board = get_object_or_404(Board, url=kwargs["url"])
|
|
|
|
|
self.post_obj = get_object_or_404(Post, pk=kwargs["id"])
|
|
|
|
|
return super().dispatch(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BanSuccessView(PermissionRequiredMixin, TemplateView):
|
|
|
|
|
permission_required = "ban.create"
|
|
|
|
|
template_name = "board/ban_success.html"
|
2022-06-24 16:21:41 -07:00
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
context["window_timeout"] = settings.BAN_WINDOW_CLOSE_TIMEOUT
|
|
|
|
|
return context
|