diff --git a/board/views.py b/board/views.py index 3ecaab7..377cb23 100644 --- a/board/views.py +++ b/board/views.py @@ -1,12 +1,12 @@ from django.conf import settings from django.http import Http404, HttpResponseRedirect 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.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 Ban, Post, Board, Report +from board.models import Post, Board, Report from board.utils import get_client_ip, get_ip_bans __all__ = ("BannedView", "BoardView", "PostView", "ReportView") @@ -19,8 +19,13 @@ class BannedView(TemplateView): context = super(TemplateView, self).get_context_data(**kwargs) ip = get_client_ip(self.request) bans = get_ip_bans(ip) + + now = timezone.now() + active_bans = [ban for ban in bans if ban.expires > now] context["bans"] = bans + context["active_bans"] = active_bans context["ip"] = ip + return context @@ -52,14 +57,22 @@ class CreatePostView(CreateView): def dispatch(self, request, *args, **kwargs): self._set_board(kwargs["url"]) - if request.method == "POST": - ip = get_client_ip(request) - bans = [ - ban - for ban in get_ip_bans(ip) - if ban.board == self.board or not ban.board - ] - if bans: + ip = get_client_ip(request) + # Filter bans by board first + bans = [ + ban for ban in get_ip_bans(ip) if ban.board == self.board or not ban.board + ] + if bans: + # Check if any bans are expired + 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 super(CreatePostView, self).dispatch(request, *args, **kwargs)