moviewyrm/bookwyrm/views/admin/email_blocklist.py

56 lines
1.9 KiB
Python
Raw Normal View History

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