Bans are deleted when they expire after a user tries to post

If a user had a ban previously, range-ban or single IP, which has since
expired, they will not be redirected to the "you are banned" page and
the active bans are deleted.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-06-21 16:59:32 -07:00
parent 28ccd7d73b
commit 5763c33f39

View File

@@ -1,12 +1,12 @@
from django.conf import settings from django.conf import settings
from django.http import Http404, HttpResponseRedirect from django.http import Http404, HttpResponseRedirect
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.views.generic import DetailView
from django.views.generic.base import TemplateView from django.views.generic.base import TemplateView
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.urls import reverse, reverse_lazy from django.urls import reverse, reverse_lazy
from django.utils import timezone
from board.forms import PostForm, ReplyForm, ReportForm from board.forms import PostForm, ReplyForm, ReportForm
from board.models import Ban, Post, Board, Report from board.models import Post, Board, Report
from board.utils import get_client_ip, get_ip_bans from board.utils import get_client_ip, get_ip_bans
__all__ = ("BannedView", "BoardView", "PostView", "ReportView") __all__ = ("BannedView", "BoardView", "PostView", "ReportView")
@@ -19,8 +19,13 @@ class BannedView(TemplateView):
context = super(TemplateView, self).get_context_data(**kwargs) context = super(TemplateView, self).get_context_data(**kwargs)
ip = get_client_ip(self.request) ip = get_client_ip(self.request)
bans = get_ip_bans(ip) bans = get_ip_bans(ip)
now = timezone.now()
active_bans = [ban for ban in bans if ban.expires > now]
context["bans"] = bans context["bans"] = bans
context["active_bans"] = active_bans
context["ip"] = ip context["ip"] = ip
return context return context
@@ -52,14 +57,22 @@ class CreatePostView(CreateView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self._set_board(kwargs["url"]) self._set_board(kwargs["url"])
if request.method == "POST": ip = get_client_ip(request)
ip = get_client_ip(request) # Filter bans by board first
bans = [ bans = [
ban ban for ban in get_ip_bans(ip) if ban.board == self.board or not ban.board
for ban in get_ip_bans(ip) ]
if ban.board == self.board or not ban.board if bans:
] # Check if any bans are expired
if bans: now = timezone.now()
active = [ban for ban in bans if ban.expires > now]
expired = [ban for ban in bans if ban.expires <= now]
# Delete expired bans
for ban in expired:
ban.delete()
# If there are any active bans, and someone is trying to create
# something with a POST request, stop and redirect
if request.method == "POST" and active:
return HttpResponseRedirect(reverse("board:banned")) return HttpResponseRedirect(reverse("board:banned"))
return super(CreatePostView, self).dispatch(request, *args, **kwargs) return super(CreatePostView, self).dispatch(request, *args, **kwargs)