add domain creation form and model code

- implement domains.forms.CreateHostingDomainForm
- implement domains.models.HostingDomainManager.create_for_hosting_package that
  takes care of creating the necessary database objects for hosting domains
  assigned to a hosting package
This commit is contained in:
Jan Dittberner 2015-01-25 00:38:42 +01:00
parent 110b3d03f2
commit 8615394c2f
2 changed files with 75 additions and 1 deletions

View file

@ -0,0 +1,51 @@
"""
This module defines form classes for domain editing.
"""
from __future__ import absolute_import, unicode_literals
from django import forms
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import (
Layout,
Submit,
)
from .models import HostingDomain
class CreateHostingDomainForm(forms.ModelForm):
"""
This form is used to create new HostingDomain instances.
"""
class Meta:
model = HostingDomain
fields = ['domain']
def __init__(self, instance, *args, **kwargs):
self.hosting_package = kwargs.pop('hostingpackage')
super(CreateHostingDomainForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_action = reverse(
'create_hosting_domain', kwargs={
'package': self.hosting_package.id
})
self.helper.layout = Layout(
'domain',
Submit('submit', _('Add Hosting Domain')),
)
def clean(self):
self.cleaned_data = super(CreateHostingDomainForm, self).clean()
self.cleaned_data['hosting_package'] = self.hosting_package
def save(self, commit=True):
return HostingDomain.objects.create_for_hosting_package(
commit=commit, **self.cleaned_data)
def save_m2m(self):
pass

View file

@ -4,7 +4,7 @@ This module contains models related to domain names.
"""
from __future__ import absolute_import, unicode_literals
from django.db import models
from django.db import models, transaction
from django.conf import settings
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext as _
@ -54,6 +54,27 @@ class MailDomain(DomainBase):
mailaddresses = property(get_mailaddresses)
class HostingDomainManager(models.Manager):
"""
Default Manager for :py:class:`HostingDomain`.
"""
@transaction.atomic
def create_for_hosting_package(
self, hosting_package, domain, commit, **kwargs
):
from hostingpackages.models import CustomerHostingPackageDomain
hostingdomain = self.create(
customer=hosting_package.customer, domain=domain, **kwargs)
hostingdomain.maildomain = MailDomain.objects.create(
customer=hosting_package.customer, domain=domain)
custdomain = CustomerHostingPackageDomain.objects.create(
hosting_package=hosting_package, domain=hostingdomain)
if commit:
hostingdomain.save()
custdomain.save()
return hostingdomain
@python_2_unicode_compatible
class HostingDomain(DomainBase):
@ -67,6 +88,8 @@ class HostingDomain(DomainBase):
help_text=_('assigned mail domain for this domain')
)
objects = HostingDomainManager()
class Meta:
verbose_name = _('Hosting domain')
verbose_name_plural = _('Hosting domains')