From 0def7e3a7b0b3a8ce505d290ee2c1e3d1458da79 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 24 Jan 2015 22:29:57 +0100 Subject: [PATCH] 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 --- gnuviechadmin/hostingpackages/models.py | 54 ++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/gnuviechadmin/hostingpackages/models.py b/gnuviechadmin/hostingpackages/models.py index ed9dc40..d45405e 100644 --- a/gnuviechadmin/hostingpackages/models.py +++ b/gnuviechadmin/hostingpackages/models.py @@ -21,7 +21,10 @@ from osusers.models import ( Group, User as OsUser, ) -from userdbs.models import DB_TYPES +from userdbs.models import ( + DB_TYPES, + UserDatabase, +) DISK_SPACE_UNITS = Choices( @@ -236,12 +239,25 @@ class CustomerHostingPackage(HostingPackageBase): for attrname in ('diskspace', 'diskspace_unit', 'mailboxcount'): 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): """ Get the total disk space reserved for this hosting package and all its 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 """ @@ -267,6 +283,25 @@ class CustomerHostingPackage(HostingPackageBase): return DISK_SPACE_FACTORS[unit][min_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): soft = 1024 * self.get_disk_space(DISK_SPACE_UNITS.M) hard = soft * 105 / 100 @@ -316,6 +351,21 @@ class CustomerHostingPackage(HostingPackageBase): number=models.Sum('number') ).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 def save(self, *args, **kwargs): """