Files
coulobre/src/coulobre/hosting/models.py
T

243 lines
7.3 KiB
Python

from django.conf import settings
from django.core.validators import RegexValidator
from django.db import models
from django.urls import reverse
from django.utils import timezone
from coulobre.database_node import DATABASE_SUFFIX_PATTERN, INVALID_SUFFIX, DatabaseSpec
from coulobre.dns_node import (
MAX_SOA_SERIAL,
AddressRecord,
DnsZone,
MxRecord,
TxtRecord,
)
from coulobre.mail_node import LOCAL_PART_MAX_LENGTH, MailboxSpec
from .fields import DomainNameField
class Reseller(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
related_name="hosting_reseller",
limit_choices_to={"is_superuser": False, "hosting_customer__isnull": True},
)
account_limit = models.PositiveIntegerField(default=0)
hosting_plans = models.ManyToManyField("HostingPlan", blank=True)
def may_create_accounts(self):
return self.account_limit > 0 and self.hosting_plans.exists()
def __str__(self):
return self.user.get_username()
class Customer(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.PROTECT,
related_name="hosting_customer",
limit_choices_to={
"is_staff": False,
"is_superuser": False,
"hosting_reseller__isnull": True,
},
)
def __str__(self):
return self.user.get_username()
class HostingPlan(models.Model):
name = models.CharField(max_length=120, unique=True)
description = models.CharField(max_length=240, blank=True)
database_limit = models.PositiveIntegerField(
blank=True,
null=True,
help_text="Leave blank for unlimited. Enter 0 to block new database requests.",
)
mailbox_limit = models.PositiveIntegerField(
blank=True,
null=True,
help_text="Leave blank for unlimited. Enter 0 to block new mailbox requests.",
)
class Meta:
ordering = ("name", "pk")
def __str__(self):
return self.name
class HostingAccount(models.Model):
name = models.CharField(max_length=120)
primary_domain = DomainNameField(max_length=253, unique=True)
hosting_plan = models.ForeignKey(
HostingPlan, on_delete=models.PROTECT, null=True, blank=True
)
reseller = models.ForeignKey(
Reseller, on_delete=models.PROTECT, null=True, blank=True
)
customer = models.ForeignKey(
Customer, on_delete=models.PROTECT, null=True, blank=True
)
created_at = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse("panel:account", kwargs={"pk": self.pk})
def __str__(self):
return str(self.primary_domain)
class HostingDatabase(models.Model):
account = models.ForeignKey(
HostingAccount, on_delete=models.PROTECT, related_name="databases"
)
suffix = models.CharField(
"database suffix",
max_length=24,
validators=[RegexValidator(DATABASE_SUFFIX_PATTERN, INVALID_SUFFIX)],
help_text=INVALID_SUFFIX,
)
class Meta:
ordering = ("suffix",)
constraints = [
models.UniqueConstraint(
fields=("account", "suffix"),
name="hosting_database_account_suffix_unique",
)
]
@property
def spec(self):
return DatabaseSpec(self.account_id, self.suffix)
def __str__(self):
return self.spec.name
class DirectAdminImport(models.Model):
account = models.OneToOneField(
HostingAccount, on_delete=models.CASCADE, related_name="directadmin_import"
)
fingerprint = models.CharField(max_length=64, unique=True)
class HostingDnsZone(models.Model):
account = models.ForeignKey(
HostingAccount, on_delete=models.PROTECT, related_name="dns_zones"
)
domain = DomainNameField(max_length=253, unique=True)
serial = models.PositiveBigIntegerField(default=1)
nameservers = models.JSONField()
responsible_name = DomainNameField(max_length=253)
addresses = models.JSONField(default=list)
mx_records = models.JSONField(default=list)
txt_records = models.JSONField(default=list)
class Meta:
ordering = ("domain",)
constraints = [
models.CheckConstraint(
condition=models.Q(serial__lte=MAX_SOA_SERIAL),
name="hosting_dns_zone_valid_serial",
)
]
@property
def spec(self):
return DnsZone(
domain=self.domain,
serial=self.serial,
nameservers=tuple(self.nameservers),
responsible_name=self.responsible_name,
addresses=tuple(
AddressRecord.from_manifest(item) for item in self.addresses
),
mx_records=tuple(MxRecord.from_manifest(item) for item in self.mx_records),
txt_records=tuple(
TxtRecord.from_manifest(item) for item in self.txt_records
),
)
def __str__(self):
return str(self.domain)
class HostingMailDomain(models.Model):
account = models.ForeignKey(
HostingAccount, on_delete=models.PROTECT, related_name="mail_domains"
)
domain = DomainNameField(max_length=253, unique=True)
def __str__(self):
return str(self.domain)
class HostingMailbox(models.Model):
mail_domain = models.ForeignKey(
HostingMailDomain, on_delete=models.PROTECT, related_name="mailboxes"
)
local_part = models.CharField(max_length=LOCAL_PART_MAX_LENGTH)
class Meta:
ordering = ("mail_domain__domain", "local_part")
constraints = [
models.UniqueConstraint(
fields=("mail_domain", "local_part"),
name="hosting_mailbox_domain_local_part_unique",
)
]
@property
def spec(self):
return MailboxSpec(self.mail_domain.domain, self.local_part)
def __str__(self):
return self.spec.address
class WebProvisioningPlan(models.Model):
account = models.ForeignKey(HostingAccount, on_delete=models.CASCADE)
primary_domain = DomainNameField(max_length=253)
requested_at = models.DateTimeField(auto_now_add=True)
last_dispatched_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
apache_config = models.TextField(blank=True)
@classmethod
def for_account(cls, account):
return cls(account=account, primary_domain=account.primary_domain)
@property
def can_export(self):
return (
self.completed_at is not None
and bool(self.apache_config)
and self.primary_domain == self.account.primary_domain
)
def __str__(self):
return str(self.primary_domain)
class WebActivation(models.Model):
plan = models.OneToOneField(
WebProvisioningPlan, on_delete=models.CASCADE, related_name="activation"
)
requested_at = models.DateTimeField(default=timezone.now)
last_dispatched_at = models.DateTimeField(null=True, blank=True)
started_at = models.DateTimeField(null=True, blank=True)
finished_at = models.DateTimeField(null=True, blank=True)
problem = models.TextField(blank=True)
@property
def state(self):
if self.finished_at is not None:
return "failed" if self.problem else "configured"
return "running" if self.started_at else "queued"