Add mailbox requests and deployment manifests for hosting accounts

This commit is contained in:
parkel committed 2026-11-19 23:24:40 +01:00
1 parent 4e05e63b15
commit 4bd7a90580
10 files changed
+519

No files matched your search

+69
View File
@@ -11,6 +11,7 @@ from django.views.generic import CreateView, DetailView, FormView, ListView, Upd
from django.views.generic.detail import SingleObjectMixin
from coulobre.dns_node import MAX_SOA_SERIAL
from coulobre.mail_node import INVALID_LOCAL_PART, MailboxSpec
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
from .dns_forms import DnsZoneEditForm, DnsZoneForm
@@ -18,6 +19,8 @@ from .models import (
HostingAccount,
HostingDatabase,
HostingDnsZone,
HostingMailbox,
HostingMailDomain,
HostingPlan,
WebProvisioningPlan,
)
@@ -31,6 +34,7 @@ VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
VIEW_RESELLERS = "hosting.view_reseller"
VIEW_DATABASES = "hosting.view_hostingdatabase"
VIEW_DNS_ZONES = "hosting.view_hostingdnszone"
VIEW_MAILBOXES = "hosting.view_hostingmailbox"
class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
@@ -166,6 +170,10 @@ class AccountDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
context["has_primary_zone"] = any(
zone.domain == self.object.primary_domain for zone in zones
)
if reseller is not None or user.has_perm(VIEW_MAILBOXES):
context["mailboxes"] = HostingMailbox.objects.filter(
mail_domain__account=self.object
).select_related("mail_domain")
return context
@@ -324,6 +332,67 @@ class DownloadDnsZoneManifest(OperatorOnlyMixin, PermissionRequiredMixin, View):
)
class MailboxCreate(OperatorOnlyMixin, PermissionRequiredMixin, CreateView):
model = HostingMailbox
fields = ("local_part",)
template_name = "hosting/mailbox_form.html"
permission_required = (VIEW_ACCOUNTS, VIEW_MAILBOXES, "hosting.add_hostingmailbox")
@cached_property
def account(self):
return get_object_or_404(HostingAccount, pk=self.kwargs["pk"])
def get_form(self, form_class=None):
form = super().get_form(form_class)
form.fields["local_part"].help_text = INVALID_LOCAL_PART
return form
def get_context_data(self, **kwargs):
return super().get_context_data(account=self.account, **kwargs)
def form_valid(self, form):
try:
spec = MailboxSpec(
self.account.primary_domain, form.cleaned_data["local_part"]
)
except ValueError as error:
form.add_error("local_part", str(error))
return self.form_invalid(form)
with transaction.atomic():
mail_domain, _ = HostingMailDomain.objects.get_or_create(
domain=spec.domain, defaults={"account": self.account}
)
if mail_domain.account_id != self.account.pk:
form.add_error(
None, "This mail domain belongs to another hosting account."
)
return self.form_invalid(form)
self.object, created = HostingMailbox.objects.get_or_create(
mail_domain=mail_domain, local_part=spec.local_part
)
if not created:
form.add_error("local_part", "This mailbox has already been requested.")
return self.form_invalid(form)
return redirect(self.account)
class DownloadMailboxManifest(OperatorOnlyMixin, PermissionRequiredMixin, View):
permission_required = (VIEW_ACCOUNTS, VIEW_MAILBOXES)
def get(self, request, pk):
mailbox = get_object_or_404(
HostingMailbox.objects.select_related("mail_domain"), pk=pk
)
return JsonResponse(
mailbox.spec.manifest(),
headers={
"Cache-Control": "private, no-store",
"Content-Disposition": f'attachment; filename="coulobre-mailbox-{mailbox.pk}.json"',
},
)
class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
model = HostingPlan
permission_required = VIEW_HOSTING_PLANS