add more properties to CustomerHostingPackage

- add get_hostingoptions method and property hostingoptions
- add get_package_space method to determine disk space provided by the
  hosting package itself
- add get_databases_flat and property databases to get a list of databases
  assigned to the hosting package
- add may_add_database to determine whether additional databases are allowed
  by the hosting package's options
This commit is contained in:
Jan Dittberner 2015-01-24 22:29:57 +01:00
parent 2c170ebcc1
commit 0def7e3a7b

View file

@ -21,7 +21,10 @@ from osusers.models import (
Group, Group,
User as OsUser, User as OsUser,
) )
from userdbs.models import DB_TYPES from userdbs.models import (
DB_TYPES,
UserDatabase,
)
DISK_SPACE_UNITS = Choices( DISK_SPACE_UNITS = Choices(
@ -236,12 +239,25 @@ class CustomerHostingPackage(HostingPackageBase):
for attrname in ('diskspace', 'diskspace_unit', 'mailboxcount'): for attrname in ('diskspace', 'diskspace_unit', 'mailboxcount'):
setattr(self, attrname, getattr(self.template, attrname)) setattr(self, attrname, getattr(self.template, attrname))
def get_hostingoptions(self):
opts = []
for opt_type in [
CustomerDiskSpaceOption,
CustomerMailboxOption,
CustomerUserDatabaseOption
]:
opts.extend(opt_type.objects.filter(hosting_package=self))
return opts
hostingoptions = property(get_hostingoptions)
def get_disk_space(self, unit=None): def get_disk_space(self, unit=None):
""" """
Get the total disk space reserved for this hosting package and all its Get the total disk space reserved for this hosting package and all its
additional disk space options. additional disk space options.
:return: disk space :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 :rtype: int
""" """
@ -267,6 +283,25 @@ class CustomerHostingPackage(HostingPackageBase):
return DISK_SPACE_FACTORS[unit][min_unit] * diskspace return DISK_SPACE_FACTORS[unit][min_unit] * diskspace
return DISK_SPACE_FACTORS[min_unit][unit] * diskspace return DISK_SPACE_FACTORS[min_unit][unit] * diskspace
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:
return (DISK_SPACE_FACTORS[self.diskspace_unit][0] *
self.diskspace * 1024**2)
if unit > self.diskspace_unit:
return (DISK_SPACE_FACTORS[unit][self.diskspace_unit] *
self.diskspace)
return DISK_SPACE_FACTORS[self.diskspace_unit][unit] * self.diskspace
def get_quota(self): def get_quota(self):
soft = 1024 * self.get_disk_space(DISK_SPACE_UNITS.M) soft = 1024 * self.get_disk_space(DISK_SPACE_UNITS.M)
hard = soft * 105 / 100 hard = soft * 105 / 100
@ -316,6 +351,21 @@ class CustomerHostingPackage(HostingPackageBase):
number=models.Sum('number') number=models.Sum('number')
).all() ).all()
def get_databases_flat(self):
if self.osuser:
return UserDatabase.objects.filter(
db_user__osuser=self.osuser).all()
databases = property(get_databases_flat)
def may_add_database(self):
return (
CustomerUserDatabaseOption.objects.filter(
hosting_package=self).count()
>
UserDatabase.objects.filter(
db_user__osuser=self.osuser).count()
)
@transaction.atomic @transaction.atomic
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" """