moviewyrm/bookwyrm/views/admin/ip_blocklist.py

56 lines
1.8 KiB
Python
Raw Normal View History

2021-09-17 19:59:16 +00:00
""" Manage IP blocklist """
from django.contrib.auth.decorators import login_required, permission_required
from django.shortcuts import get_object_or_404, redirect
from django.template.response import TemplateResponse
from django.utils.decorators import method_decorator
from django.views import View
from bookwyrm import forms, models
# pylint: disable=no-self-use
@method_decorator(login_required, name="dispatch")
@method_decorator(
permission_required("bookwyrm.moderate_user", raise_exception=True),
name="dispatch",
)
class IPBlocklist(View):
"""Block registration by ip address"""
def get(self, request):
"""view and compose blocks"""
data = {
"addresses": models.IPBlocklist.objects.all(),
"form": forms.IPBlocklistForm(),
}
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/ip_blocklist/ip_blocklist.html", data
)
2021-09-17 19:59:16 +00:00
def post(self, request, block_id=None):
"""create a new ip address block"""
if block_id:
return self.delete(request, block_id)
form = forms.IPBlocklistForm(request.POST)
data = {
"addresses": models.IPBlocklist.objects.all(),
"form": form,
}
if not form.is_valid():
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/ip_blocklist/ip_blocklist.html", data
)
2021-09-17 19:59:16 +00:00
form.save()
data["form"] = forms.IPBlocklistForm()
2021-09-28 19:39:52 +00:00
return TemplateResponse(
request, "settings/ip_blocklist/ip_blocklist.html", data
)
2021-09-17 19:59:16 +00:00
# pylint: disable=unused-argument
def delete(self, request, domain_id):
"""remove a domain block"""
domain = get_object_or_404(models.IPBlocklist, id=domain_id)
domain.delete()
return redirect("settings-ip-blocks")