gva/gnuviechadmin/hostingpackages/models.py
Jan Dittberner 4f2ebbae63 add hostingpackages api doc
- add to docs/code.rst toctree
- add new docs/code/hostingpackages.rst containing the autodoc directives
- document undocumented code in hostingpackages app
- update translation templates
2015-01-18 13:37:21 +01:00

262 lines
7.8 KiB
Python

"""
This module contains the hosting package models.
"""
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
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 osusers.models import User as OsUser
DISK_SPACE_UNITS = Choices(
(0, 'M', _('MiB')),
(1, 'G', _('GiB')),
(2, 'T', _('TiB')),
)
@python_2_unicode_compatible
class HostingPackageBase(TimeStampedModel):
description = models.TextField(_('description'), blank=True)
mailboxcount = models.PositiveIntegerField(_('mailbox count'))
diskspace = models.PositiveIntegerField(
_('disk space'), help_text=_('disk space for the hosting package'))
diskspace_unit = models.PositiveSmallIntegerField(
_('unit of disk space'), choices=DISK_SPACE_UNITS)
class Meta:
abstract = True
def __str__(self):
return self.name
class HostingPackageTemplate(HostingPackageBase):
name = models.CharField(_('name'), max_length=128, unique=True)
class Meta:
verbose_name = _('Hosting package')
verbose_name_plural = _('Hosting packages')
class HostingOption(TimeStampedModel):
"""
This is the base class for several types of hosting options.
"""
@python_2_unicode_compatible
class DiskSpaceOptionBase(models.Model):
diskspace = models.PositiveIntegerField(_('disk space'))
diskspace_unit = models.PositiveSmallIntegerField(
_('unit of disk space'), choices=DISK_SPACE_UNITS)
class Meta:
abstract = True
ordering = ['diskspace_unit', 'diskspace']
unique_together = ['diskspace', 'diskspace_unit']
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())
class DiskSpaceOption(DiskSpaceOptionBase, HostingOption):
"""
This is a class for hosting options adding additional disk space to
existing hosting packages.
"""
@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)
class Meta:
abstract = True
ordering = ['db_type', 'number']
unique_together = ['number', 'db_type']
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
)
class UserDatabaseOption(UserDatabaseOptionBase, HostingOption):
"""
This is a class for hosting options adding user databases to existing
hosting packages.
"""
@python_2_unicode_compatible
class MailboxOptionBase(models.Model):
"""
Base class for mailbox options.
"""
number = models.PositiveIntegerField(
_('number of mailboxes'), unique=True)
class Meta:
abstract = True
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
)
class MailboxOption(MailboxOptionBase, HostingOption):
"""
This is a class for hosting options adding more mailboxes to existing
hosting packages.
"""
class CustomerHostingPackageManager(models.Manager):
"""
This is the default manager implementation for
:py:class:`CustomerHostingPackage`.
"""
def create_from_template(self, customer, template, name, **kwargs):
"""
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`
"""
package = CustomerHostingPackage(
customer=customer, template=template, name=name)
for attrname in ('description', 'diskspace', 'diskspace_unit',
'mailboxcount'):
setattr(package, attrname, getattr(template, attrname))
if 'commit' in kwargs and kwargs['commit'] is True:
package.save(**kwargs)
return package
class CustomerHostingPackage(HostingPackageBase):
"""
This class defines customer specific hosting packages.
"""
customer = models.ForeignKey(
settings.AUTH_USER_MODEL, verbose_name=_('customer'))
template = models.ForeignKey(
HostingPackageTemplate, verbose_name=_('hosting package template'),
help_text=_(
'The hosting package template that this hosting package is based'
' on'
))
name = models.CharField(_('name'), max_length=128)
osuser = models.OneToOneField(
OsUser, verbose_name=_('Operating system user'),
blank=True, null=True)
objects = CustomerHostingPackageManager()
class Meta:
unique_together = ['customer', 'name']
verbose_name = _('customer hosting package')
verbose_name_plural = _('customer hosting packages')
class CustomerHostingPackageOption(TimeStampedModel):
"""
This class defines options for customer hosting packages.
"""
hosting_package = models.ForeignKey(
CustomerHostingPackage, verbose_name=_('hosting package'))
class Meta:
verbose_name = _('customer hosting option')
verbose_name_plural = _('customer hosting options')
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'),
help_text=_(
'The disk space option template that this disk space option is'
' based on'
))
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'),
help_text=_(
'The user database option template that this database option is'
' based on'
))
class CustomerMailboxOption(MailboxOptionBase,
CustomerHostingPackageOption):
"""
This is a class for customer hosting package options adding additional
mailboxes to existing customer hosting packages.
"""
template = models.ForeignKey(
UserDatabaseOption,
verbose_name=_('mailbox option template'),
help_text=_(
'The mailbox option template that this mailbox option is based on'
))