add CustomerHostingPackage information aggration methods

- add get_disk_space, get_used_mailboxes, get_mailboxes and get_database
  methods for hostingpackages.models.CustomerHostingPackage
This commit is contained in:
Jan Dittberner 2015-01-18 16:14:47 +01:00
parent 95e15806c6
commit 0e32aff544

View file

@ -12,8 +12,9 @@ from django.utils.translation import ugettext_lazy as _, ungettext
from model_utils import Choices
from model_utils.models import TimeStampedModel
from userdbs.models import DB_TYPES
from managemails.models import Mailbox
from osusers.models import User as OsUser
from userdbs.models import DB_TYPES
DISK_SPACE_UNITS = Choices(
@ -22,6 +23,12 @@ DISK_SPACE_UNITS = Choices(
(2, 'T', _('TiB')),
)
DISK_SPACE_FACTORS = (
(1, None, None),
(1024, 1, None),
(1024 * 1024, 1024, 1),
)
@python_2_unicode_compatible
class HostingPackageBase(TimeStampedModel):
@ -201,6 +208,67 @@ class CustomerHostingPackage(HostingPackageBase):
verbose_name = _('customer hosting package')
verbose_name_plural = _('customer hosting packages')
def get_disk_space(self):
"""
Get the total disk space reserved for this hosting package and all its
additional disk space options.
:return: disk space
: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:
diskspace += option.disk_space
elif option.diskspace_unit > min_unit:
diskspace += (
DISK_SPACE_FACTORS[option.diskspace_unit][min_unit] *
option.diskspace)
else:
diskspace = (
DISK_SPACE_FACTORS[min_unit][option.diskspace_unit] *
diskspace) + option.diskspace
min_unit = option.diskspace_unit
return DISK_SPACE_FACTORS[min_unit][0] * diskspace * 1024**2
def get_used_mailboxes(self):
"""
Get the number of used mailboxes for this hosting package.
"""
if self.osuser:
return Mailbox.objects.filter(osuser=self.osuser).count()
return 0
def get_mailboxes(self):
"""
Get the number of mailboxes provided by this hosting package and all
of its mailbox options.
"""
result = CustomerMailboxOption.objects.filter(
hosting_package=self
).aggregate(
mailbox_sum=models.Sum('number')
)
return self.mailboxcount + result['mailbox_sum']
def get_databases(self):
"""
Get the number of user databases provided by user database hosting
options for this hosting package.
"""
return CustomerUserDatabaseOption.objects.values(
'db_type'
).filter(hosting_package=self).annotate(
number=models.Sum('number')
).all()
class CustomerHostingPackageOption(TimeStampedModel):
"""