Add ability to create bans from reports
This is done in the admin view and opens a new iframed window. The ban form is pretty barebones and doesn't have full functionality yet, but that is coming. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,15 +1,24 @@
|
||||
from typing import Any, Dict
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.http import Http404, HttpResponseRedirect
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic.base import TemplateView
|
||||
from django.views.generic.edit import CreateView
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils import timezone
|
||||
from board.forms import PostForm, ReplyForm, ReportForm
|
||||
from board.models import Post, Board, Report
|
||||
from board.forms import BanForm, PostForm, ReplyForm, ReportForm
|
||||
from board.models import Ban, Board, Post, Report
|
||||
from board.utils import get_client_ip, get_ip_bans
|
||||
|
||||
__all__ = ("BannedView", "BoardView", "PostView", "ReportView")
|
||||
__all__ = (
|
||||
"BanCreateView",
|
||||
"BanSuccessView",
|
||||
"BannedView",
|
||||
"BoardView",
|
||||
"PostView",
|
||||
"ReportView",
|
||||
)
|
||||
|
||||
|
||||
class BannedView(TemplateView):
|
||||
@@ -37,18 +46,6 @@ class CreatePostView(CreateView):
|
||||
* self.board
|
||||
"""
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
self._set_board(kwargs["url"])
|
||||
return super(CreatePostView, self).get(request, *args, **kwargs)
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
self._set_board(kwargs["url"])
|
||||
return super(CreatePostView, self).post(request, *args, **kwargs)
|
||||
|
||||
def _set_board(self, board_url: str):
|
||||
board = get_object_or_404(Board, url=board_url)
|
||||
self.board = board
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(CreatePostView, self).get_form_kwargs()
|
||||
kwargs["board"] = self.board
|
||||
@@ -56,7 +53,9 @@ class CreatePostView(CreateView):
|
||||
return kwargs
|
||||
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
self._set_board(kwargs["url"])
|
||||
# Set the board on this object
|
||||
self.board = get_object_or_404(Board, url=kwargs["url"])
|
||||
|
||||
ip = get_client_ip(request)
|
||||
# Filter bans by board first
|
||||
bans = [
|
||||
@@ -162,3 +161,47 @@ class ReportView(CreatePostView):
|
||||
post = get_object_or_404(Post, id=post_id)
|
||||
kwargs["op"] = post
|
||||
return kwargs
|
||||
|
||||
|
||||
class BanCreateView(PermissionRequiredMixin, CreateView):
|
||||
model = Ban
|
||||
form_class = BanForm
|
||||
permission_required = "ban.create"
|
||||
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"] = [
|
||||
ban for ban in bans if ban.expires is not None and ban.expires < now
|
||||
]
|
||||
context["current_bans"] = [
|
||||
ban for ban in bans if ban.expires is None or ban.expires > now
|
||||
]
|
||||
|
||||
return context
|
||||
|
||||
def get_form_kwargs(self) -> Dict[str, Any]:
|
||||
kwargs = super(CreateView, self).get_form_kwargs()
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user