Rename CreatePostView -> CreateView, add utils.is_banned
The artist formerly known as CreatePostView is a little more generic now and outsources its ban check to a new util method, is_banned. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
from board.models import Ban, RangeBan
|
||||
from typing import TYPE_CHECKING
|
||||
from django.utils import timezone
|
||||
import ipaddress
|
||||
|
||||
from board.models import Ban, RangeBan
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from board.models import Board
|
||||
|
||||
|
||||
def get_client_ip(request):
|
||||
"Get the IP address of a client-side request. Shamelessly copy/pasted from StackOverflow."
|
||||
@@ -24,3 +31,17 @@ def get_ip_bans(ip: str) -> list:
|
||||
if start <= ip_addr <= end: # type: ignore
|
||||
bans += [rangeban]
|
||||
return bans
|
||||
|
||||
|
||||
def is_banned(ip: str, board: "Board" | None) -> bool:
|
||||
now = timezone.now()
|
||||
bans = [ban for ban in get_ip_bans(ip) if ban.board == board or not ban.board]
|
||||
if bans:
|
||||
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()
|
||||
return bool(active)
|
||||
else:
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user