2015-01-18 13:22:06 +01:00
|
|
|
"""
|
|
|
|
This module contains the hosting package models.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
from django.conf import settings
|
2023-05-07 13:45:30 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.db import models, transaction
|
2018-11-19 23:28:40 +01:00
|
|
|
from django.urls import reverse
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.utils.translation import ngettext
|
2015-01-18 13:22:06 +01:00
|
|
|
from model_utils import Choices
|
|
|
|
from model_utils.models import TimeStampedModel
|
|
|
|
|
2015-01-24 19:10:58 +01:00
|
|
|
from domains.models import HostingDomain
|
2015-01-18 16:14:47 +01:00
|
|
|
from managemails.models import Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
from osusers.models import AdditionalGroup, Group
|
|
|
|
from osusers.models import User as OsUser
|
2019-01-30 21:27:25 +01:00
|
|
|
from userdbs.models import DB_TYPES, UserDatabase
|
|
|
|
|
|
|
|
DISK_SPACE_UNITS = Choices((0, "M", _("MiB")), (1, "G", _("GiB")), (2, "T", _("TiB")))
|
|
|
|
|
|
|
|
DISK_SPACE_FACTORS = ((1, None, None), (1024, 1, None), (1024 * 1024, 1024, 1))
|
2015-01-18 16:14:47 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class HostingPackageBase(TimeStampedModel):
|
2019-01-30 21:27:25 +01:00
|
|
|
description = models.TextField(_("description"), blank=True)
|
|
|
|
mailboxcount = models.PositiveIntegerField(_("mailbox count"))
|
2015-01-18 13:22:06 +01:00
|
|
|
diskspace = models.PositiveIntegerField(
|
2019-01-30 21:27:25 +01:00
|
|
|
_("disk space"), help_text=_("disk space for the hosting package")
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
diskspace_unit = models.PositiveSmallIntegerField(
|
2019-01-30 21:27:25 +01:00
|
|
|
_("unit of disk space"), choices=DISK_SPACE_UNITS
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class HostingPackageTemplate(HostingPackageBase):
|
2019-01-30 21:27:25 +01:00
|
|
|
name = models.CharField(_("name"), max_length=128, unique=True)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
2019-01-30 21:27:25 +01:00
|
|
|
verbose_name = _("Hosting package")
|
|
|
|
verbose_name_plural = _("Hosting packages")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class HostingOption(TimeStampedModel):
|
|
|
|
"""
|
|
|
|
This is the base class for several types of hosting options.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2015-01-24 19:10:58 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
class DiskSpaceOptionBase(models.Model):
|
2019-01-30 21:27:25 +01:00
|
|
|
diskspace = models.PositiveIntegerField(_("disk space"))
|
2015-01-18 13:22:06 +01:00
|
|
|
diskspace_unit = models.PositiveSmallIntegerField(
|
2019-01-30 21:27:25 +01:00
|
|
|
_("unit of disk space"), choices=DISK_SPACE_UNITS
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2019-01-30 21:27:25 +01:00
|
|
|
ordering = ["diskspace_unit", "diskspace"]
|
|
|
|
verbose_name = _("Disk space option")
|
|
|
|
verbose_name_plural = _("Disk space options")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return _("Additional disk space {space} {unit}").format(
|
2019-01-30 21:27:25 +01:00
|
|
|
space=self.diskspace, unit=self.get_diskspace_unit_display()
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
2015-01-24 19:10:58 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
class DiskSpaceOption(DiskSpaceOptionBase, HostingOption):
|
|
|
|
"""
|
|
|
|
This is a class for hosting options adding additional disk space to
|
|
|
|
existing hosting packages.
|
|
|
|
|
|
|
|
"""
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-25 15:15:39 +01:00
|
|
|
class Meta:
|
2019-01-30 21:27:25 +01:00
|
|
|
unique_together = ["diskspace", "diskspace_unit"]
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseOptionBase(models.Model):
|
2019-01-30 21:27:25 +01:00
|
|
|
number = models.PositiveIntegerField(_("number of databases"), default=1)
|
|
|
|
db_type = models.PositiveSmallIntegerField(_("database type"), choices=DB_TYPES)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2019-01-30 21:27:25 +01:00
|
|
|
ordering = ["db_type", "number"]
|
|
|
|
verbose_name = _("Database option")
|
|
|
|
verbose_name_plural = _("Database options")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return ngettext(
|
2019-01-30 21:27:25 +01:00
|
|
|
"{type} database", "{count} {type} databases", self.number
|
|
|
|
).format(type=self.get_db_type_display(), count=self.number)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseOption(UserDatabaseOptionBase, HostingOption):
|
|
|
|
"""
|
|
|
|
This is a class for hosting options adding user databases to existing
|
|
|
|
hosting packages.
|
|
|
|
|
|
|
|
"""
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-25 15:15:39 +01:00
|
|
|
class Meta:
|
2019-01-30 21:27:25 +01:00
|
|
|
unique_together = ["number", "db_type"]
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailboxOptionBase(models.Model):
|
|
|
|
"""
|
|
|
|
Base class for mailbox options.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
|
|
|
number = models.PositiveIntegerField(_("number of mailboxes"), unique=True)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
2019-01-30 21:27:25 +01:00
|
|
|
ordering = ["number"]
|
|
|
|
verbose_name = _("Mailbox option")
|
|
|
|
verbose_name_plural = _("Mailbox options")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return ngettext(
|
2019-01-30 21:27:25 +01:00
|
|
|
"{count} additional mailbox", "{count} additional mailboxes", self.number
|
|
|
|
).format(count=self.number)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MailboxOption(MailboxOptionBase, HostingOption):
|
|
|
|
"""
|
|
|
|
This is a class for hosting options adding more mailboxes to existing
|
|
|
|
hosting packages.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerHostingPackageManager(models.Manager):
|
2015-01-18 13:37:21 +01:00
|
|
|
"""
|
|
|
|
This is the default manager implementation for
|
|
|
|
:py:class:`CustomerHostingPackage`.
|
|
|
|
|
|
|
|
"""
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
def create_from_template(self, customer, template, name, **kwargs):
|
2015-01-18 13:37:21 +01:00
|
|
|
"""
|
|
|
|
Use this method to create a new :py:class:`CustomerHostingPackage` from
|
|
|
|
a :py:class:`HostingPackageTemplate`.
|
|
|
|
|
|
|
|
The method copies data from the template to the new
|
|
|
|
:py:class:`CustomerHostingPackage` instance.
|
|
|
|
|
|
|
|
:param customer: a Django user representing a customer
|
|
|
|
:param template: a :py:class:`HostingPackageTemplate`
|
|
|
|
:param str name: the name of the hosting package there must only be
|
|
|
|
one hosting package of this name for each customer
|
|
|
|
:return: customer hosting package
|
|
|
|
:rtype: :py:class:`CustomerHostingPackage`
|
|
|
|
|
|
|
|
"""
|
2015-01-18 13:22:06 +01:00
|
|
|
package = CustomerHostingPackage(
|
2019-01-30 21:27:25 +01:00
|
|
|
customer=customer, template=template, name=name
|
|
|
|
)
|
2015-01-22 00:19:30 +01:00
|
|
|
package.description = template.description
|
|
|
|
package.copy_template_attributes()
|
2019-01-30 21:27:25 +01:00
|
|
|
if "commit" in kwargs and kwargs["commit"] is True:
|
2015-01-18 13:22:06 +01:00
|
|
|
package.save(**kwargs)
|
|
|
|
return package
|
|
|
|
|
|
|
|
|
|
|
|
class CustomerHostingPackage(HostingPackageBase):
|
|
|
|
"""
|
|
|
|
This class defines customer specific hosting packages.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
customer = models.ForeignKey(
|
2019-01-30 21:27:25 +01:00
|
|
|
settings.AUTH_USER_MODEL, verbose_name=_("customer"), on_delete=models.CASCADE
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
template = models.ForeignKey(
|
2019-01-30 21:27:25 +01:00
|
|
|
HostingPackageTemplate,
|
|
|
|
verbose_name=_("hosting package template"),
|
2015-01-18 13:22:06 +01:00
|
|
|
help_text=_(
|
2019-01-30 21:27:25 +01:00
|
|
|
"The hosting package template that this hosting package is based" " on"
|
2018-11-19 23:28:40 +01:00
|
|
|
),
|
2019-01-30 21:27:25 +01:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
name = models.CharField(_("name"), max_length=128)
|
2015-01-18 13:22:06 +01:00
|
|
|
osuser = models.OneToOneField(
|
2019-01-30 21:27:25 +01:00
|
|
|
OsUser,
|
|
|
|
verbose_name=_("Operating system user"),
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
objects = CustomerHostingPackageManager()
|
|
|
|
|
|
|
|
class Meta:
|
2019-01-30 21:27:25 +01:00
|
|
|
unique_together = ["customer", "name"]
|
|
|
|
verbose_name = _("customer hosting package")
|
|
|
|
verbose_name_plural = _("customer hosting packages")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
2015-01-22 00:19:30 +01:00
|
|
|
def __str__(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
return _("{name} for {customer}").format(name=self.name, customer=self.customer)
|
2015-01-22 00:19:30 +01:00
|
|
|
|
2015-01-24 16:26:32 +01:00
|
|
|
def get_absolute_url(self):
|
2019-01-30 21:27:25 +01:00
|
|
|
return reverse(
|
|
|
|
"hosting_package_details",
|
|
|
|
kwargs={"user": self.customer.username, "pk": self.id},
|
|
|
|
)
|
2015-01-24 16:26:32 +01:00
|
|
|
|
2015-01-22 00:19:30 +01:00
|
|
|
def copy_template_attributes(self):
|
|
|
|
"""
|
|
|
|
Copy the attributes of the hosting package's template to the package.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
for attrname in ("diskspace", "diskspace_unit", "mailboxcount"):
|
2015-01-22 00:19:30 +01:00
|
|
|
setattr(self, attrname, getattr(self.template, attrname))
|
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
def get_hostingoptions(self):
|
|
|
|
opts = []
|
|
|
|
for opt_type in [
|
|
|
|
CustomerDiskSpaceOption,
|
|
|
|
CustomerMailboxOption,
|
2019-01-30 21:27:25 +01:00
|
|
|
CustomerUserDatabaseOption,
|
2015-01-24 22:29:57 +01:00
|
|
|
]:
|
|
|
|
opts.extend(opt_type.objects.filter(hosting_package=self))
|
|
|
|
return opts
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
hostingoptions = property(get_hostingoptions)
|
|
|
|
|
2015-01-22 00:19:30 +01:00
|
|
|
def get_disk_space(self, unit=None):
|
2015-01-18 16:14:47 +01:00
|
|
|
"""
|
|
|
|
Get the total disk space reserved for this hosting package and all its
|
|
|
|
additional disk space options.
|
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
:param unit: value from :py:data:`DISK_SPACE_UNITS` or :py:const:`None`
|
|
|
|
:return: disk space in unit or bytes (if parameter unit is
|
|
|
|
:py:const:`None`)
|
2015-01-18 16:14:47 +01:00
|
|
|
:rtype: int
|
|
|
|
|
|
|
|
"""
|
|
|
|
diskspace = self.diskspace
|
|
|
|
min_unit = self.diskspace_unit
|
|
|
|
|
|
|
|
options = CustomerDiskSpaceOption.objects.filter(hosting_package=self)
|
|
|
|
for option in options:
|
|
|
|
if option.diskspace_unit == min_unit:
|
2015-01-25 15:15:39 +01:00
|
|
|
diskspace += option.diskspace
|
2015-01-18 16:14:47 +01:00
|
|
|
elif option.diskspace_unit > min_unit:
|
|
|
|
diskspace += (
|
2019-01-30 21:27:25 +01:00
|
|
|
DISK_SPACE_FACTORS[option.diskspace_unit][min_unit]
|
|
|
|
* option.diskspace
|
|
|
|
)
|
2015-01-18 16:14:47 +01:00
|
|
|
else:
|
|
|
|
diskspace = (
|
2019-01-30 21:27:25 +01:00
|
|
|
DISK_SPACE_FACTORS[min_unit][option.diskspace_unit] * diskspace
|
|
|
|
) + option.diskspace
|
2015-01-18 16:14:47 +01:00
|
|
|
min_unit = option.diskspace_unit
|
2015-01-22 00:19:30 +01:00
|
|
|
if unit is None:
|
2023-02-18 22:46:48 +01:00
|
|
|
return DISK_SPACE_FACTORS[min_unit][0] * diskspace * 1024**2
|
2015-01-22 00:19:30 +01:00
|
|
|
if unit > min_unit:
|
|
|
|
return DISK_SPACE_FACTORS[unit][min_unit] * diskspace
|
|
|
|
return DISK_SPACE_FACTORS[min_unit][unit] * diskspace
|
|
|
|
|
2023-07-22 19:43:10 +02:00
|
|
|
disk_space = property(get_disk_space)
|
|
|
|
|
|
|
|
def get_used_disk_space_sum(self, unit=None):
|
|
|
|
"""
|
|
|
|
Get the used disk space of this hosting package from submitted disk space statistics.
|
|
|
|
|
|
|
|
:param unit: value from :py:data:`DISK_SPACE_UNITS` or :py:const:`None`
|
|
|
|
:return: disk space in unit or bytes (if parameter unit is :py:const:`None`)
|
|
|
|
:rtype: int
|
|
|
|
|
|
|
|
"""
|
|
|
|
sum = 0
|
|
|
|
for usage in self.customerpackagediskusage_set.all():
|
|
|
|
sum += usage.size_in_bytes
|
|
|
|
if unit is None:
|
|
|
|
return sum
|
|
|
|
return DISK_SPACE_FACTORS[0][unit] * sum
|
|
|
|
|
|
|
|
used_disk_space_sum = property(get_used_disk_space_sum)
|
|
|
|
|
|
|
|
def get_space_level(self):
|
|
|
|
return self.used_disk_space_sum / self.disk_space * 100.0
|
|
|
|
|
|
|
|
space_level = property(get_space_level)
|
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
def get_package_space(self, unit=None):
|
|
|
|
"""
|
|
|
|
Get the total disk space reserved for this package without looking at
|
|
|
|
any additional dis space options.
|
|
|
|
|
|
|
|
:param unit: value from :py:data:`DISK_SPACE_UNITS` or :py:const:`None`
|
|
|
|
:return: disk space in unit or bytes (if parameter unit is
|
|
|
|
:py:const:`None`)
|
|
|
|
:rtype: int
|
|
|
|
|
|
|
|
"""
|
|
|
|
if unit is None:
|
2019-01-30 21:27:25 +01:00
|
|
|
return (
|
2023-02-18 22:46:48 +01:00
|
|
|
DISK_SPACE_FACTORS[self.diskspace_unit][0] * self.diskspace * 1024**2
|
2019-01-30 21:27:25 +01:00
|
|
|
)
|
2015-01-24 22:29:57 +01:00
|
|
|
if unit > self.diskspace_unit:
|
2019-01-30 21:27:25 +01:00
|
|
|
return DISK_SPACE_FACTORS[unit][self.diskspace_unit] * self.diskspace
|
2015-01-24 22:29:57 +01:00
|
|
|
return DISK_SPACE_FACTORS[self.diskspace_unit][unit] * self.diskspace
|
|
|
|
|
2015-01-22 00:19:30 +01:00
|
|
|
def get_quota(self):
|
|
|
|
soft = 1024 * self.get_disk_space(DISK_SPACE_UNITS.M)
|
2019-01-30 21:27:25 +01:00
|
|
|
hard = soft * 105 // 100
|
2015-01-22 00:19:30 +01:00
|
|
|
return (soft, hard)
|
2015-01-18 16:14:47 +01:00
|
|
|
|
2015-01-24 22:26:27 +01:00
|
|
|
def get_mailboxes(self):
|
|
|
|
if self.osuser:
|
|
|
|
return Mailbox.objects.filter(osuser=self.osuser).all()
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-24 22:26:27 +01:00
|
|
|
mailboxes = property(get_mailboxes)
|
|
|
|
|
|
|
|
def get_used_mailbox_count(self):
|
2015-01-18 16:14:47 +01:00
|
|
|
"""
|
|
|
|
Get the number of used mailboxes for this hosting package.
|
|
|
|
|
|
|
|
"""
|
|
|
|
if self.osuser:
|
|
|
|
return Mailbox.objects.filter(osuser=self.osuser).count()
|
|
|
|
return 0
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-24 22:26:27 +01:00
|
|
|
used_mailbox_count = property(get_used_mailbox_count)
|
2015-01-18 16:14:47 +01:00
|
|
|
|
2015-01-24 22:26:27 +01:00
|
|
|
def get_mailbox_count(self):
|
2015-01-18 16:14:47 +01:00
|
|
|
"""
|
|
|
|
Get the number of mailboxes provided by this hosting package and all
|
|
|
|
of its mailbox options.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
result = CustomerMailboxOption.objects.filter(hosting_package=self).aggregate(
|
|
|
|
mailbox_sum=models.Sum("number")
|
2015-01-18 16:14:47 +01:00
|
|
|
)
|
2019-01-30 21:27:25 +01:00
|
|
|
return self.mailboxcount + (result["mailbox_sum"] or 0)
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-24 22:26:27 +01:00
|
|
|
mailbox_count = property(get_mailbox_count)
|
|
|
|
|
|
|
|
def may_add_mailbox(self):
|
|
|
|
return self.used_mailbox_count < self.mailbox_count
|
2015-01-18 16:14:47 +01:00
|
|
|
|
|
|
|
def get_databases(self):
|
|
|
|
"""
|
|
|
|
Get the number of user databases provided by user database hosting
|
|
|
|
options for this hosting package.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
return (
|
|
|
|
CustomerUserDatabaseOption.objects.values("db_type")
|
|
|
|
.filter(hosting_package=self)
|
|
|
|
.annotate(number=models.Sum("number"))
|
|
|
|
.all()
|
|
|
|
)
|
2015-01-18 16:14:47 +01:00
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
def get_databases_flat(self):
|
|
|
|
if self.osuser:
|
2019-01-30 21:27:25 +01:00
|
|
|
return UserDatabase.objects.filter(db_user__osuser=self.osuser).all()
|
2018-11-19 23:28:40 +01:00
|
|
|
|
2015-01-24 22:29:57 +01:00
|
|
|
databases = property(get_databases_flat)
|
|
|
|
|
|
|
|
def may_add_database(self):
|
|
|
|
return (
|
2019-01-30 21:27:25 +01:00
|
|
|
CustomerUserDatabaseOption.objects.filter(hosting_package=self).count()
|
|
|
|
> UserDatabase.objects.filter(db_user__osuser=self.osuser).count()
|
2015-01-24 22:29:57 +01:00
|
|
|
)
|
|
|
|
|
2015-01-22 00:19:30 +01:00
|
|
|
@transaction.atomic
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Save the hosting package to the database.
|
|
|
|
|
|
|
|
If this is a new hosting package a new operating system user is
|
|
|
|
created and assigned to the hosting package.
|
|
|
|
|
|
|
|
:param args: positional arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:param kwargs: keyword arguments to be passed on to
|
|
|
|
:py:meth:`django.db.Model.save`
|
|
|
|
:return: self
|
|
|
|
:rtype: :py:class:`CustomerHostingPackage`
|
|
|
|
|
|
|
|
"""
|
|
|
|
if self.pk is None:
|
|
|
|
self.copy_template_attributes()
|
|
|
|
self.osuser = OsUser.objects.create_user(self.customer)
|
|
|
|
for group in settings.OSUSER_DEFAULT_GROUPS:
|
2023-05-07 13:45:30 +02:00
|
|
|
try:
|
|
|
|
AdditionalGroup.objects.create(
|
|
|
|
user=self.osuser, group=Group.objects.get(groupname=group)
|
|
|
|
)
|
|
|
|
except Group.DoesNotExist as e:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
f"group {group} has not been defined"
|
|
|
|
) from e
|
2015-01-22 00:19:30 +01:00
|
|
|
return super(CustomerHostingPackage, self).save(*args, **kwargs)
|
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
|
2015-01-24 19:10:58 +01:00
|
|
|
class CustomerHostingPackageDomain(TimeStampedModel):
|
|
|
|
"""
|
|
|
|
This class defines the relationship from a hosting package to a hosting
|
|
|
|
domain.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-24 19:10:58 +01:00
|
|
|
hosting_package = models.ForeignKey(
|
2019-01-30 21:27:25 +01:00
|
|
|
CustomerHostingPackage,
|
|
|
|
verbose_name=_("hosting package"),
|
|
|
|
related_name="domains",
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-01-24 19:10:58 +01:00
|
|
|
domain = models.OneToOneField(
|
2019-01-30 21:27:25 +01:00
|
|
|
HostingDomain, verbose_name=_("hosting domain"), on_delete=models.CASCADE
|
|
|
|
)
|
2015-01-24 19:10:58 +01:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.domain.domain
|
|
|
|
|
|
|
|
def is_usable_for_email(self):
|
|
|
|
"""
|
|
|
|
Tells wether the related domain is usable for email addresses.
|
|
|
|
|
|
|
|
"""
|
|
|
|
return self.domain.maildomain is not None
|
|
|
|
|
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
class CustomerHostingPackageOption(TimeStampedModel):
|
|
|
|
"""
|
|
|
|
This class defines options for customer hosting packages.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
hosting_package = models.ForeignKey(
|
2019-01-30 21:27:25 +01:00
|
|
|
CustomerHostingPackage,
|
|
|
|
verbose_name=_("hosting package"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
class Meta:
|
2019-01-30 21:27:25 +01:00
|
|
|
verbose_name = _("customer hosting option")
|
|
|
|
verbose_name_plural = _("customer hosting options")
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
class CustomerDiskSpaceOption(DiskSpaceOptionBase, CustomerHostingPackageOption):
|
2015-01-18 13:22:06 +01:00
|
|
|
"""
|
|
|
|
This is a class for customer hosting package options adding additional disk
|
|
|
|
space to existing customer hosting package.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
template = models.ForeignKey(
|
|
|
|
DiskSpaceOption,
|
2019-01-30 21:27:25 +01:00
|
|
|
verbose_name=_("disk space option template"),
|
2015-01-18 13:22:06 +01:00
|
|
|
help_text=_(
|
2019-01-30 21:27:25 +01:00
|
|
|
"The disk space option template that this disk space option is" " based on"
|
2018-11-19 23:28:40 +01:00
|
|
|
),
|
2019-01-30 21:27:25 +01:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
class CustomerUserDatabaseOption(UserDatabaseOptionBase, CustomerHostingPackageOption):
|
2015-01-18 13:22:06 +01:00
|
|
|
"""
|
|
|
|
This is a class for customer hosting package options adding user databases
|
|
|
|
to existing customer hosting packages.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
template = models.ForeignKey(
|
|
|
|
UserDatabaseOption,
|
2019-01-30 21:27:25 +01:00
|
|
|
verbose_name=_("user database option template"),
|
2015-01-18 13:22:06 +01:00
|
|
|
help_text=_(
|
2019-01-30 21:27:25 +01:00
|
|
|
"The user database option template that this database option is" " based on"
|
2018-11-19 23:28:40 +01:00
|
|
|
),
|
2019-01-30 21:27:25 +01:00
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2015-01-18 13:22:06 +01:00
|
|
|
|
|
|
|
|
2019-01-30 21:27:25 +01:00
|
|
|
class CustomerMailboxOption(MailboxOptionBase, CustomerHostingPackageOption):
|
2015-01-18 13:22:06 +01:00
|
|
|
"""
|
|
|
|
This is a class for customer hosting package options adding additional
|
|
|
|
mailboxes to existing customer hosting packages.
|
|
|
|
|
|
|
|
"""
|
2019-01-30 21:27:25 +01:00
|
|
|
|
2015-01-18 13:22:06 +01:00
|
|
|
template = models.ForeignKey(
|
2015-01-18 15:58:58 +01:00
|
|
|
MailboxOption,
|
2019-01-30 21:27:25 +01:00
|
|
|
verbose_name=_("mailbox option template"),
|
|
|
|
help_text=_("The mailbox option template that this mailbox option is based on"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
2023-07-22 19:43:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CustomerPackageDiskUsage(TimeStampedModel):
|
|
|
|
"""
|
|
|
|
This class represents disk usage statistics for a customer hosting package.
|
|
|
|
"""
|
|
|
|
|
|
|
|
package = models.ForeignKey(
|
|
|
|
CustomerHostingPackage,
|
|
|
|
verbose_name=_("hosting package"),
|
|
|
|
help_text=_("The hosting package"),
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
)
|
|
|
|
source = models.CharField(
|
|
|
|
verbose_name=_("data source"),
|
|
|
|
choices=(("disk", _("disk")), ("mysql", _("mysql")), ("pgsql", _("pgsql"))),
|
|
|
|
)
|
|
|
|
item = models.CharField(verbose_name=_("data item"))
|
|
|
|
used_kb = models.PositiveBigIntegerField(
|
|
|
|
verbose_name=_("space used in KiB"), default=0
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
unique_together = ("package", "source", "item")
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s %s = %d KiB" % (self.source, self.item, self.used_kb)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def size_in_bytes(self):
|
|
|
|
return self.used_kb * 1024
|