From 8615394c2f6c983247d2ca2b9538c3004ed83153 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sun, 25 Jan 2015 00:38:42 +0100 Subject: [PATCH] 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 --- gnuviechadmin/domains/forms.py | 51 +++++++++++++++++++++++++++++++++ gnuviechadmin/domains/models.py | 25 +++++++++++++++- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 gnuviechadmin/domains/forms.py diff --git a/gnuviechadmin/domains/forms.py b/gnuviechadmin/domains/forms.py new file mode 100644 index 0000000..ba2a654 --- /dev/null +++ b/gnuviechadmin/domains/forms.py @@ -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 diff --git a/gnuviechadmin/domains/models.py b/gnuviechadmin/domains/models.py index 7affeb6..c827f2c 100644 --- a/gnuviechadmin/domains/models.py +++ b/gnuviechadmin/domains/models.py @@ -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')