Reserve website hostnames and dispatch alias lifecycle jobs
This commit is contained in:
1 parent
e4f31cec2f
commit
077332c1bb
6 files changed
+404
-2
No files matched your search
@@ -1,12 +1,13 @@
|
||||
import uuid
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import (
|
||||
MaxValueValidator,
|
||||
MinValueValidator,
|
||||
RegexValidator,
|
||||
)
|
||||
from django.db import models
|
||||
from django.db import models, router, transaction
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
|
||||
@@ -206,9 +207,82 @@ class HostingAccount(models.Model):
|
||||
def __str__(self):
|
||||
return str(self.primary_domain)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
update_fields = kwargs.get("update_fields")
|
||||
if update_fields is not None and "primary_domain" not in update_fields:
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
self.primary_domain = self._meta.get_field("primary_domain").to_python(
|
||||
self.primary_domain
|
||||
)
|
||||
using = kwargs.get("using") or router.db_for_write(
|
||||
type(self), instance=self
|
||||
)
|
||||
with transaction.atomic(using=using):
|
||||
super().save(*args, **kwargs)
|
||||
claims = WebsiteDomain.objects.using(using).filter(account=self)
|
||||
primary = claims.filter(is_primary=True).first()
|
||||
if primary is None:
|
||||
claims.create(
|
||||
domain=self.primary_domain, account=self, is_primary=True
|
||||
)
|
||||
elif primary.domain != self.primary_domain:
|
||||
if claims.filter(is_primary=False).exists():
|
||||
raise ValidationError(
|
||||
{
|
||||
"primary_domain": (
|
||||
"Remove website aliases before changing the "
|
||||
"primary domain."
|
||||
),
|
||||
}
|
||||
)
|
||||
primary.domain = self.primary_domain
|
||||
primary.save(update_fields=["domain"], using=using)
|
||||
|
||||
def get_absolute_url(self):
|
||||
return reverse("panel:account", kwargs={"pk": self.pk})
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if not self.primary_domain:
|
||||
return
|
||||
if WebsiteDomain.objects.filter(
|
||||
domain=self.primary_domain, is_primary=False
|
||||
).exists():
|
||||
raise ValidationError(
|
||||
{"primary_domain": "This domain is unavailable."}
|
||||
)
|
||||
|
||||
if self.pk:
|
||||
claims = self.website_domains
|
||||
if (
|
||||
claims.filter(is_primary=False).exists()
|
||||
and claims.filter(is_primary=True)
|
||||
.exclude(domain=self.primary_domain)
|
||||
.exists()
|
||||
):
|
||||
raise ValidationError(
|
||||
{
|
||||
"primary_domain": (
|
||||
"Remove website aliases before changing the "
|
||||
"primary domain."
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class WebsiteDomain(models.Model):
|
||||
account = models.ForeignKey(
|
||||
HostingAccount,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="website_domains",
|
||||
)
|
||||
domain = DomainNameField(max_length=253, unique=True)
|
||||
is_primary = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return self.domain
|
||||
|
||||
|
||||
class HostingDatabase(models.Model):
|
||||
account = models.ForeignKey(
|
||||
@@ -438,6 +512,31 @@ class WebHTTPS(ProvisioningJob):
|
||||
verbose_name = "website HTTPS request"
|
||||
|
||||
|
||||
class WebsiteAlias(ProvisioningJob):
|
||||
class Operation(models.TextChoices):
|
||||
ACTIVATE = "activate", "Activate alias"
|
||||
HTTPS = "https", "Enable HTTPS"
|
||||
REMOVE = "remove", "Remove alias"
|
||||
|
||||
reservation = models.OneToOneField(
|
||||
WebsiteDomain, on_delete=models.PROTECT, related_name="alias"
|
||||
)
|
||||
primary_domain = DomainNameField(max_length=253)
|
||||
operation = models.CharField(
|
||||
max_length=8, choices=Operation, default=Operation.ACTIVATE
|
||||
)
|
||||
http_active = models.BooleanField(default=False)
|
||||
https_active = models.BooleanField(default=False)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=("operation", "finished_at", "requested_at"))
|
||||
]
|
||||
permissions = [
|
||||
("manage_website_aliases", "Can manage website aliases")
|
||||
]
|
||||
|
||||
|
||||
class SiteSFTP(ProvisioningJob):
|
||||
account = models.OneToOneField(
|
||||
HostingAccount, on_delete=models.CASCADE, related_name="sftp"
|
||||
|
||||
Reference in new issue
Block a user