79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
from django import forms
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
from django.db import IntegrityError, transaction
|
|
|
|
from coulobre.web_node import WebAlias, WebSite
|
|
|
|
from .models import WebsiteAlias, WebsiteDomain
|
|
from .resource_requests import lock_hosting_account, require_open_account
|
|
from .sftp import site_is_active
|
|
|
|
|
|
class WebsiteAliasForm(forms.Form):
|
|
domain = forms.CharField(
|
|
label="Alias hostname",
|
|
max_length=253,
|
|
help_text=(
|
|
"Point this hostname's DNS to the same destination as the "
|
|
"primary domain."
|
|
),
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_website_alias(account_id, domain):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
require_open_account(account)
|
|
if not site_is_active(account):
|
|
raise ValidationError(
|
|
"Activate the website for the primary domain first."
|
|
)
|
|
try:
|
|
alias = WebAlias(WebSite(account.pk, account.primary_domain), domain)
|
|
except ValueError as error:
|
|
raise ValidationError({"domain": str(error)}) from error
|
|
|
|
try:
|
|
with transaction.atomic():
|
|
reservation = WebsiteDomain.objects.create(
|
|
account=account, domain=alias.domain
|
|
)
|
|
except IntegrityError:
|
|
if not WebsiteDomain.objects.filter(domain=alias.domain).exists():
|
|
raise
|
|
raise ValidationError(
|
|
{"domain": "This domain is unavailable."}
|
|
) from None
|
|
WebsiteAlias.objects.create(
|
|
reservation=reservation, primary_domain=account.primary_domain
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def queue_alias_operation(account_id, alias_id, operation, *, allow_https):
|
|
account = lock_hosting_account(account_id, customer_id=None)
|
|
require_open_account(account)
|
|
aliases = WebsiteAlias.objects.filter(reservation__account=account)
|
|
alias = aliases.get(pk=alias_id)
|
|
if alias.finished_at is None:
|
|
raise ValidationError("Wait for the current alias request to finish.")
|
|
|
|
if operation == "retry":
|
|
if not alias.problem:
|
|
return
|
|
operation = alias.operation
|
|
if operation == WebsiteAlias.Operation.REMOVE and hasattr(
|
|
alias.reservation, "mail_alias"
|
|
):
|
|
raise ValidationError("Remove the mail domain alias first.")
|
|
if operation == WebsiteAlias.Operation.HTTPS:
|
|
if not allow_https:
|
|
raise PermissionDenied
|
|
if alias.https_active:
|
|
return
|
|
if not alias.http_active:
|
|
raise ValidationError("Activate the alias before enabling HTTPS.")
|
|
WebsiteAlias.objects.requeue(
|
|
aliases.filter(pk=alias.pk), operation=operation
|
|
)
|