create system user when creating a new hosting package

This commit is contained in:
Jan Dittberner 2015-01-22 00:19:30 +01:00
parent d4f68a155c
commit 888a2463c4
6 changed files with 110 additions and 10 deletions

View file

@ -5,6 +5,7 @@ This module contains the hosting package models.
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.db import transaction
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _, ungettext
@ -13,7 +14,11 @@ from model_utils import Choices
from model_utils.models import TimeStampedModel
from managemails.models import Mailbox
from osusers.models import User as OsUser
from osusers.models import (
AdditionalGroup,
Group,
User as OsUser,
)
from userdbs.models import DB_TYPES
@ -175,14 +180,14 @@ class CustomerHostingPackageManager(models.Manager):
"""
package = CustomerHostingPackage(
customer=customer, template=template, name=name)
for attrname in ('description', 'diskspace', 'diskspace_unit',
'mailboxcount'):
setattr(package, attrname, getattr(template, attrname))
package.description = template.description
package.copy_template_attributes()
if 'commit' in kwargs and kwargs['commit'] is True:
package.save(**kwargs)
return package
@python_2_unicode_compatible
class CustomerHostingPackage(HostingPackageBase):
"""
This class defines customer specific hosting packages.
@ -208,7 +213,20 @@ class CustomerHostingPackage(HostingPackageBase):
verbose_name = _('customer hosting package')
verbose_name_plural = _('customer hosting packages')
def get_disk_space(self):
def __str__(self):
return _("{name} for {customer}").format(
name=self.name, customer=self.customer
)
def copy_template_attributes(self):
"""
Copy the attributes of the hosting package's template to the package.
"""
for attrname in ('diskspace', 'diskspace_unit', 'mailboxcount'):
setattr(self, attrname, getattr(self.template, attrname))
def get_disk_space(self, unit=None):
"""
Get the total disk space reserved for this hosting package and all its
additional disk space options.
@ -233,7 +251,16 @@ class CustomerHostingPackage(HostingPackageBase):
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
if unit is None:
return DISK_SPACE_FACTORS[min_unit][0] * diskspace * 1024**2
if unit > min_unit:
return DISK_SPACE_FACTORS[unit][min_unit] * diskspace
return DISK_SPACE_FACTORS[min_unit][unit] * diskspace
def get_quota(self):
soft = 1024 * self.get_disk_space(DISK_SPACE_UNITS.M)
hard = soft * 105 / 100
return (soft, hard)
def get_used_mailboxes(self):
"""
@ -269,6 +296,31 @@ class CustomerHostingPackage(HostingPackageBase):
number=models.Sum('number')
).all()
@transaction.atomic
def save(self, *args, **kwargs):
"""
Save the hosting package to the database.
If this is a new hosting package a new operating system user is
created and assigned to the hosting package.
:param args: positional arguments to be passed on to
:py:meth:`django.db.Model.save`
:param kwargs: keyword arguments to be passed on to
:py:meth:`django.db.Model.save`
:return: self
:rtype: :py:class:`CustomerHostingPackage`
"""
if self.pk is None:
self.copy_template_attributes()
self.osuser = OsUser.objects.create_user(self.customer)
for group in settings.OSUSER_DEFAULT_GROUPS:
AdditionalGroup.objects.create(
user=self.osuser, group=Group.objects.get(groupname=group)
)
return super(CustomerHostingPackage, self).save(*args, **kwargs)
class CustomerHostingPackageOption(TimeStampedModel):
"""