107 lines
3.1 KiB
Python
107 lines
3.1 KiB
Python
from django.conf import settings
|
|
from django.core.validators import RegexValidator
|
|
from django.db import models
|
|
from django.urls import reverse
|
|
|
|
from coulobre.database_node import DATABASE_SUFFIX_PATTERN, INVALID_SUFFIX, DatabaseSpec
|
|
|
|
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},
|
|
)
|
|
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 HostingPlan(models.Model):
|
|
name = models.CharField(max_length=120, unique=True)
|
|
description = models.CharField(max_length=240, blank=True)
|
|
|
|
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
|
|
)
|
|
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 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)
|