107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
from django import forms
|
|
from django.conf import settings
|
|
from django.core.exceptions import ValidationError
|
|
from django.http import Http404
|
|
from django.shortcuts import redirect
|
|
from django.urls import reverse
|
|
from django.views.generic import FormView
|
|
|
|
from .account_administration import OperatorAuthorization
|
|
from .domain_policies import policy_for
|
|
from .domain_services import (
|
|
DomainServicesForm,
|
|
allowed_domains,
|
|
save_domain_policy,
|
|
)
|
|
from .models import HostingAccount
|
|
from .update_views import inspect_linked_nodes
|
|
|
|
|
|
class OperatorDomainServicesForm(DomainServicesForm):
|
|
account = forms.ModelChoiceField(
|
|
queryset=HostingAccount.objects.filter(
|
|
retired_at__isnull=True
|
|
).order_by("primary_domain")
|
|
)
|
|
domain = forms.CharField(max_length=253)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.order_fields(["account", "domain", "dns_managed", "mail_managed"])
|
|
|
|
def clean(self):
|
|
data = super().clean()
|
|
account = data.get("account")
|
|
domain = data.get("domain")
|
|
if account and domain:
|
|
from coulobre.domain_names import canonical_domain
|
|
|
|
try:
|
|
domain = canonical_domain(domain)
|
|
except ValueError as error:
|
|
self.add_error("domain", str(error))
|
|
else:
|
|
if domain not in allowed_domains(account):
|
|
self.add_error(
|
|
"domain",
|
|
"Choose a domain assigned to the hosting account.",
|
|
)
|
|
data["domain"] = domain
|
|
return data
|
|
|
|
|
|
class DnsTopologyView(OperatorAuthorization, FormView):
|
|
form_class = OperatorDomainServicesForm
|
|
template_name = "hosting/dns_topology.html"
|
|
|
|
def get_initial(self):
|
|
selected = self.request.GET.get("account")
|
|
domain = self.request.GET.get("domain")
|
|
if selected is None or domain is None:
|
|
return {}
|
|
if (
|
|
not selected.isascii()
|
|
or not selected.isdecimal()
|
|
or len(selected) > 18
|
|
):
|
|
raise Http404
|
|
account = HostingAccount.objects.filter(
|
|
pk=int(selected), retired_at__isnull=True
|
|
).first()
|
|
if account is None or domain not in allowed_domains(account):
|
|
raise Http404
|
|
policy = policy_for(account, domain)
|
|
return {
|
|
"account": account.pk,
|
|
"domain": domain,
|
|
"dns_managed": policy.dns_managed,
|
|
"mail_managed": policy.mail_managed,
|
|
}
|
|
|
|
def get_context_data(self, **kwargs):
|
|
return super().get_context_data(
|
|
topology=inspect_linked_nodes(),
|
|
local_dns=settings.LOCAL_DNS_PUBLISHING,
|
|
local_mail=settings.LOCAL_MAILBOX_PROVISIONING,
|
|
**kwargs,
|
|
)
|
|
|
|
def form_valid(self, form):
|
|
data = form.cleaned_data
|
|
account = data["account"]
|
|
domain = data["domain"]
|
|
choices = {key: data[key] for key in ("dns_managed", "mail_managed")}
|
|
try:
|
|
save_domain_policy(account.pk, domain, None, choices)
|
|
except HostingAccount.DoesNotExist:
|
|
raise Http404 from None
|
|
except ValidationError as error:
|
|
form.add_error(None, error)
|
|
return self.form_invalid(form)
|
|
return redirect(
|
|
reverse(
|
|
"panel:dns_topology",
|
|
query={"account": account.pk, "domain": domain},
|
|
)
|
|
)
|