Fix tests for Python 3

- drop Python 2 __future__ imports
- fix tests to handle new Django and Python 3 module names
- reformat changed files with black
This commit is contained in:
Jan Dittberner 2019-01-30 21:27:25 +01:00
parent ddec6b4184
commit 3d18392b67
32 changed files with 2707 additions and 2675 deletions

View file

@ -16,37 +16,24 @@ from model_utils.models import TimeStampedModel
from domains.models import HostingDomain
from managemails.models import Mailbox
from osusers.models import (
AdditionalGroup,
Group,
User as OsUser,
)
from userdbs.models import (
DB_TYPES,
UserDatabase,
)
from osusers.models import AdditionalGroup, Group, User as OsUser
from userdbs.models import DB_TYPES, UserDatabase
DISK_SPACE_UNITS = Choices(
(0, 'M', _('MiB')),
(1, 'G', _('GiB')),
(2, 'T', _('TiB')),
)
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),
)
DISK_SPACE_FACTORS = ((1, None, None), (1024, 1, None), (1024 * 1024, 1024, 1))
@python_2_unicode_compatible
class HostingPackageBase(TimeStampedModel):
description = models.TextField(_('description'), blank=True)
mailboxcount = models.PositiveIntegerField(_('mailbox count'))
description = models.TextField(_("description"), blank=True)
mailboxcount = models.PositiveIntegerField(_("mailbox count"))
diskspace = models.PositiveIntegerField(
_('disk space'), help_text=_('disk space for the hosting package'))
_("disk space"), help_text=_("disk space for the hosting package")
)
diskspace_unit = models.PositiveSmallIntegerField(
_('unit of disk space'), choices=DISK_SPACE_UNITS)
_("unit of disk space"), choices=DISK_SPACE_UNITS
)
class Meta:
abstract = True
@ -56,11 +43,11 @@ class HostingPackageBase(TimeStampedModel):
class HostingPackageTemplate(HostingPackageBase):
name = models.CharField(_('name'), max_length=128, unique=True)
name = models.CharField(_("name"), max_length=128, unique=True)
class Meta:
verbose_name = _('Hosting package')
verbose_name_plural = _('Hosting packages')
verbose_name = _("Hosting package")
verbose_name_plural = _("Hosting packages")
class HostingOption(TimeStampedModel):
@ -72,19 +59,21 @@ class HostingOption(TimeStampedModel):
@python_2_unicode_compatible
class DiskSpaceOptionBase(models.Model):
diskspace = models.PositiveIntegerField(_('disk space'))
diskspace = models.PositiveIntegerField(_("disk space"))
diskspace_unit = models.PositiveSmallIntegerField(
_('unit of disk space'), choices=DISK_SPACE_UNITS)
_("unit of disk space"), choices=DISK_SPACE_UNITS
)
class Meta:
abstract = True
ordering = ['diskspace_unit', 'diskspace']
verbose_name = _('Disk space option')
verbose_name_plural = _('Disk space options')
ordering = ["diskspace_unit", "diskspace"]
verbose_name = _("Disk space option")
verbose_name_plural = _("Disk space options")
def __str__(self):
return _("Additional disk space {space} {unit}").format(
space=self.diskspace, unit=self.get_diskspace_unit_display())
space=self.diskspace, unit=self.get_diskspace_unit_display()
)
class DiskSpaceOption(DiskSpaceOptionBase, HostingOption):
@ -95,30 +84,24 @@ class DiskSpaceOption(DiskSpaceOptionBase, HostingOption):
"""
class Meta:
unique_together = ['diskspace', 'diskspace_unit']
unique_together = ["diskspace", "diskspace_unit"]
@python_2_unicode_compatible
class UserDatabaseOptionBase(models.Model):
number = models.PositiveIntegerField(
_('number of databases'), default=1)
db_type = models.PositiveSmallIntegerField(
_('database type'), choices=DB_TYPES)
number = models.PositiveIntegerField(_("number of databases"), default=1)
db_type = models.PositiveSmallIntegerField(_("database type"), choices=DB_TYPES)
class Meta:
abstract = True
ordering = ['db_type', 'number']
verbose_name = _('Database option')
verbose_name_plural = _('Database options')
ordering = ["db_type", "number"]
verbose_name = _("Database option")
verbose_name_plural = _("Database options")
def __str__(self):
return ungettext(
'{type} database',
'{count} {type} databases',
self.number
).format(
type=self.get_db_type_display(), count=self.number
)
"{type} database", "{count} {type} databases", self.number
).format(type=self.get_db_type_display(), count=self.number)
class UserDatabaseOption(UserDatabaseOptionBase, HostingOption):
@ -129,7 +112,7 @@ class UserDatabaseOption(UserDatabaseOptionBase, HostingOption):
"""
class Meta:
unique_together = ['number', 'db_type']
unique_together = ["number", "db_type"]
@python_2_unicode_compatible
@ -138,23 +121,19 @@ class MailboxOptionBase(models.Model):
Base class for mailbox options.
"""
number = models.PositiveIntegerField(
_('number of mailboxes'), unique=True)
number = models.PositiveIntegerField(_("number of mailboxes"), unique=True)
class Meta:
abstract = True
ordering = ['number']
verbose_name = _('Mailbox option')
verbose_name_plural = _('Mailbox options')
ordering = ["number"]
verbose_name = _("Mailbox option")
verbose_name_plural = _("Mailbox options")
def __str__(self):
return ungettext(
'{count} additional mailbox',
'{count} additional mailboxes',
self.number
).format(
count=self.number
)
"{count} additional mailbox", "{count} additional mailboxes", self.number
).format(count=self.number)
class MailboxOption(MailboxOptionBase, HostingOption):
@ -189,10 +168,11 @@ class CustomerHostingPackageManager(models.Manager):
"""
package = CustomerHostingPackage(
customer=customer, template=template, name=name)
customer=customer, template=template, name=name
)
package.description = template.description
package.copy_template_attributes()
if 'commit' in kwargs and kwargs['commit'] is True:
if "commit" in kwargs and kwargs["commit"] is True:
package.save(**kwargs)
return package
@ -203,45 +183,49 @@ class CustomerHostingPackage(HostingPackageBase):
This class defines customer specific hosting packages.
"""
customer = models.ForeignKey(
settings.AUTH_USER_MODEL, verbose_name=_('customer'),
on_delete=models.CASCADE)
settings.AUTH_USER_MODEL, verbose_name=_("customer"), on_delete=models.CASCADE
)
template = models.ForeignKey(
HostingPackageTemplate, verbose_name=_('hosting package template'),
HostingPackageTemplate,
verbose_name=_("hosting package template"),
help_text=_(
'The hosting package template that this hosting package is based'
' on'
"The hosting package template that this hosting package is based" " on"
),
on_delete=models.CASCADE)
name = models.CharField(_('name'), max_length=128)
on_delete=models.CASCADE,
)
name = models.CharField(_("name"), max_length=128)
osuser = models.OneToOneField(
OsUser, verbose_name=_('Operating system user'),
blank=True, null=True, on_delete=models.CASCADE)
OsUser,
verbose_name=_("Operating system user"),
blank=True,
null=True,
on_delete=models.CASCADE,
)
objects = CustomerHostingPackageManager()
class Meta:
unique_together = ['customer', 'name']
verbose_name = _('customer hosting package')
verbose_name_plural = _('customer hosting packages')
unique_together = ["customer", "name"]
verbose_name = _("customer hosting package")
verbose_name_plural = _("customer hosting packages")
def __str__(self):
return _("{name} for {customer}").format(
name=self.name, customer=self.customer
)
return _("{name} for {customer}").format(name=self.name, customer=self.customer)
def get_absolute_url(self):
return reverse('hosting_package_details', kwargs={
'user': self.customer.username,
'pk': self.id,
})
return reverse(
"hosting_package_details",
kwargs={"user": self.customer.username, "pk": self.id},
)
def copy_template_attributes(self):
"""
Copy the attributes of the hosting package's template to the package.
"""
for attrname in ('diskspace', 'diskspace_unit', 'mailboxcount'):
for attrname in ("diskspace", "diskspace_unit", "mailboxcount"):
setattr(self, attrname, getattr(self.template, attrname))
def get_hostingoptions(self):
@ -249,7 +233,7 @@ class CustomerHostingPackage(HostingPackageBase):
for opt_type in [
CustomerDiskSpaceOption,
CustomerMailboxOption,
CustomerUserDatabaseOption
CustomerUserDatabaseOption,
]:
opts.extend(opt_type.objects.filter(hosting_package=self))
return opts
@ -276,13 +260,13 @@ class CustomerHostingPackage(HostingPackageBase):
diskspace += option.diskspace
elif option.diskspace_unit > min_unit:
diskspace += (
DISK_SPACE_FACTORS[option.diskspace_unit][min_unit] *
option.diskspace)
DISK_SPACE_FACTORS[option.diskspace_unit][min_unit]
* option.diskspace
)
else:
diskspace = (
DISK_SPACE_FACTORS[min_unit][
option.diskspace_unit] *
diskspace) + option.diskspace
DISK_SPACE_FACTORS[min_unit][option.diskspace_unit] * diskspace
) + option.diskspace
min_unit = option.diskspace_unit
if unit is None:
return DISK_SPACE_FACTORS[min_unit][0] * diskspace * 1024 ** 2
@ -302,16 +286,16 @@ class CustomerHostingPackage(HostingPackageBase):
"""
if unit is None:
return (DISK_SPACE_FACTORS[self.diskspace_unit][0] *
self.diskspace * 1024 ** 2)
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[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
hard = soft * 105 // 100
return (soft, hard)
def get_mailboxes(self):
@ -337,12 +321,10 @@ class CustomerHostingPackage(HostingPackageBase):
of its mailbox options.
"""
result = CustomerMailboxOption.objects.filter(
hosting_package=self
).aggregate(
mailbox_sum=models.Sum('number')
result = CustomerMailboxOption.objects.filter(hosting_package=self).aggregate(
mailbox_sum=models.Sum("number")
)
return self.mailboxcount + (result['mailbox_sum'] or 0)
return self.mailboxcount + (result["mailbox_sum"] or 0)
mailbox_count = property(get_mailbox_count)
@ -355,25 +337,23 @@ class CustomerHostingPackage(HostingPackageBase):
options for this hosting package.
"""
return CustomerUserDatabaseOption.objects.values(
'db_type'
).filter(hosting_package=self).annotate(
number=models.Sum('number')
).all()
return (
CustomerUserDatabaseOption.objects.values("db_type")
.filter(hosting_package=self)
.annotate(number=models.Sum("number"))
.all()
)
def get_databases_flat(self):
if self.osuser:
return UserDatabase.objects.filter(
db_user__osuser=self.osuser).all()
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()
CustomerUserDatabaseOption.objects.filter(hosting_package=self).count()
> UserDatabase.objects.filter(db_user__osuser=self.osuser).count()
)
@transaction.atomic
@ -409,12 +389,16 @@ class CustomerHostingPackageDomain(TimeStampedModel):
domain.
"""
hosting_package = models.ForeignKey(
CustomerHostingPackage, verbose_name=_('hosting package'),
related_name='domains', on_delete=models.CASCADE)
CustomerHostingPackage,
verbose_name=_("hosting package"),
related_name="domains",
on_delete=models.CASCADE,
)
domain = models.OneToOneField(
HostingDomain, verbose_name=_('hosting domain'),
on_delete=models.CASCADE)
HostingDomain, verbose_name=_("hosting domain"), on_delete=models.CASCADE
)
def __str__(self):
return self.domain.domain
@ -432,60 +416,62 @@ class CustomerHostingPackageOption(TimeStampedModel):
This class defines options for customer hosting packages.
"""
hosting_package = models.ForeignKey(
CustomerHostingPackage, verbose_name=_('hosting package'),
on_delete=models.CASCADE)
CustomerHostingPackage,
verbose_name=_("hosting package"),
on_delete=models.CASCADE,
)
class Meta:
verbose_name = _('customer hosting option')
verbose_name_plural = _('customer hosting options')
verbose_name = _("customer hosting option")
verbose_name_plural = _("customer hosting options")
class CustomerDiskSpaceOption(DiskSpaceOptionBase,
CustomerHostingPackageOption):
class CustomerDiskSpaceOption(DiskSpaceOptionBase, CustomerHostingPackageOption):
"""
This is a class for customer hosting package options adding additional disk
space to existing customer hosting package.
"""
template = models.ForeignKey(
DiskSpaceOption,
verbose_name=_('disk space option template'),
verbose_name=_("disk space option template"),
help_text=_(
'The disk space option template that this disk space option is'
' based on'
"The disk space option template that this disk space option is" " based on"
),
on_delete=models.CASCADE)
on_delete=models.CASCADE,
)
class CustomerUserDatabaseOption(UserDatabaseOptionBase,
CustomerHostingPackageOption):
class CustomerUserDatabaseOption(UserDatabaseOptionBase, CustomerHostingPackageOption):
"""
This is a class for customer hosting package options adding user databases
to existing customer hosting packages.
"""
template = models.ForeignKey(
UserDatabaseOption,
verbose_name=_('user database option template'),
verbose_name=_("user database option template"),
help_text=_(
'The user database option template that this database option is'
' based on'
"The user database option template that this database option is" " based on"
),
on_delete=models.CASCADE)
on_delete=models.CASCADE,
)
class CustomerMailboxOption(MailboxOptionBase,
CustomerHostingPackageOption):
class CustomerMailboxOption(MailboxOptionBase, CustomerHostingPackageOption):
"""
This is a class for customer hosting package options adding additional
mailboxes to existing customer hosting packages.
"""
template = models.ForeignKey(
MailboxOption,
verbose_name=_('mailbox option template'),
help_text=_(
'The mailbox option template that this mailbox option is based on'
),
on_delete=models.CASCADE)
verbose_name=_("mailbox option template"),
help_text=_("The mailbox option template that this mailbox option is based on"),
on_delete=models.CASCADE,
)

View file

@ -5,10 +5,7 @@ Test for models.
from django.test import TestCase
from hostingpackages.models import (
DISK_SPACE_UNITS,
CustomerHostingPackage,
)
from hostingpackages.models import DISK_SPACE_UNITS, CustomerHostingPackage
class CustomerHostingPackageTest(TestCase):
@ -16,7 +13,7 @@ class CustomerHostingPackageTest(TestCase):
package = CustomerHostingPackage(
diskspace=10, diskspace_unit=DISK_SPACE_UNITS.G
)
self.assertEqual(package.get_disk_space(), 10 * 1024 * 1024**2)
self.assertEqual(package.get_disk_space(), 10 * 1024 ** 3)
def test_get_disk_space_mib(self):
package = CustomerHostingPackage(