From defb934c95be1acf0265f13ab3ee752883bdf738 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Fri, 16 Jun 2017 15:52:05 +0200 Subject: [PATCH] Added new option to control mailbox creation. see #1138 --- modoboa/admin/forms/domain.py | 52 +++++++++++-------- modoboa/admin/static/admin/js/admin.js | 13 +++-- .../templates/admin/domain_options_form.html | 10 ++-- modoboa/admin/tests/test_domain.py | 28 ++++++++-- 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/modoboa/admin/forms/domain.py b/modoboa/admin/forms/domain.py index bac68ec72..843aeaf57 100644 --- a/modoboa/admin/forms/domain.py +++ b/modoboa/admin/forms/domain.py @@ -201,6 +201,15 @@ class DomainFormOptions(forms.Form): required=False ) + with_mailbox = YesNoField( + label=ugettext_lazy("With a mailbox"), + initial=True, + help_text=ugettext_lazy( + "Create a mailbox for the administrator." + ), + required=False + ) + create_aliases = YesNoField( label=ugettext_lazy("Create aliases"), initial=True, @@ -260,28 +269,29 @@ class DomainFormOptions(forms.Form): da.role = "DomainAdmins" da.post_create(user) - dom_admin_username = self.cleaned_data["dom_admin_username"] - mb = Mailbox( - address=dom_admin_username, domain=domain, - user=da, use_domain_quota=True - ) - mb.set_quota( - override_rules=user.has_perm("admin.change_domain")) - mb.save(creator=user) + if self.cleaned_data["with_mailbox"]: + dom_admin_username = self.cleaned_data["dom_admin_username"] + mb = Mailbox( + address=dom_admin_username, domain=domain, + user=da, use_domain_quota=True + ) + mb.set_quota( + override_rules=user.has_perm("admin.change_domain")) + mb.save(creator=user) - condition = ( - domain.type == "domain" and - self.cleaned_data["create_aliases"] and - dom_admin_username != "postmaster" - ) - if condition: - core_signals.can_create_object.send( - self.__class__, context=user, klass=Alias) - address = u"postmaster@{}".format(domain.name) - alias = Alias.objects.create( - address=address, domain=domain, enabled=True) - alias.set_recipients([mb.full_address]) - alias.post_create(user) + condition = ( + domain.type == "domain" and + self.cleaned_data["create_aliases"] and + dom_admin_username != "postmaster" + ) + if condition: + core_signals.can_create_object.send( + self.__class__, context=user, klass=Alias) + address = u"postmaster@{}".format(domain.name) + alias = Alias.objects.create( + address=address, domain=domain, enabled=True) + alias.set_recipients([mb.full_address]) + alias.post_create(user) domain.add_admin(da) diff --git a/modoboa/admin/static/admin/js/admin.js b/modoboa/admin/static/admin/js/admin.js index 81f0bc2e8..7df3c579e 100644 --- a/modoboa/admin/static/admin/js/admin.js +++ b/modoboa/admin/static/admin/js/admin.js @@ -7,9 +7,7 @@ * application. */ -/*global - $ Listing -*/ +/* global $ gettext Listing */ var Admin = function(options) { Listing.call(this, options); @@ -185,6 +183,7 @@ Domains.prototype = { change_inputs_state: function(value) { $("#id_dom_admin_username").attr("disabled", value); + $("input[name=with_mailbox]").attr("disabled", value); $("input[name=create_aliases]").attr("disabled", value); }, @@ -193,6 +192,12 @@ Domains.prototype = { this.change_inputs_state(($target.val() === "True") ? false : true); }, + withMailboxChanged: function(e) { + var $target = $(e.target); + $("input[name=create_aliases]").attr( + "disabled", $target.val() === "False"); + }, + /** * Initialize the main form contained in the domain edition modal. * @@ -214,6 +219,8 @@ Domains.prototype = { optionsform_init: function() { $("input[name=create_dom_admin]").click( $.proxy(this.create_dom_admin_changed, this)); + $("input[name=with_mailbox]").click( + $.proxy(this.withMailboxChanged, this)); this.change_inputs_state( $("input[name=create_dom_admin]:checked").val() === "True" ? false : true ); diff --git a/modoboa/admin/templates/admin/domain_options_form.html b/modoboa/admin/templates/admin/domain_options_form.html index 0993c11fa..aba4e73e6 100644 --- a/modoboa/admin/templates/admin/domain_options_form.html +++ b/modoboa/admin/templates/admin/domain_options_form.html @@ -1,8 +1,10 @@ {% load i18n form_tags %} + {% if form.fields %} -{% render_field form.create_dom_admin %} -{% render_field form.dom_admin_username appended_text="@" %} -{% render_field form.create_aliases %} + {% render_field form.create_dom_admin %} + {% render_field form.dom_admin_username appended_text="@" %} + {% render_field form.with_mailbox %} + {% render_field form.create_aliases %} {% else %} -
{% trans "You can't create a new domain administrator" %}
+
{% trans "You can't create a new domain administrator" %}
{% endif %} diff --git a/modoboa/admin/tests/test_domain.py b/modoboa/admin/tests/test_domain.py index f069ca13d..d9a91d992 100644 --- a/modoboa/admin/tests/test_domain.py +++ b/modoboa/admin/tests/test_domain.py @@ -55,7 +55,8 @@ class DomainTestCase(ModoTestCase): values = { "name": "pouet.com", "quota": 1000, "default_mailbox_quota": 100, "create_dom_admin": True, "dom_admin_username": "toto", - "create_aliases": True, "type": "domain", "stepid": 'step3' + "with_mailbox": True, "create_aliases": True, "type": "domain", + "stepid": "step3" } self.ajax_post(reverse("admin:domain_add"), values) dom = Domain.objects.get(name="pouet.com") @@ -71,7 +72,8 @@ class DomainTestCase(ModoTestCase): values = { "name": "pouet2.com", "quota": 1000, "default_mailbox_quota": 100, "create_dom_admin": True, "dom_admin_username": "postmaster", - "create_aliases": True, "type": "domain", "stepid": "step3" + "create_aliases": True, "type": "domain", "stepid": "step3", + "with_mailbox": True } self.ajax_post(reverse("admin:domain_add"), values) self.assertTrue( @@ -80,12 +82,29 @@ class DomainTestCase(ModoTestCase): Alias.objects.filter( address="postmaster@pouet2.com", internal=False).exists()) + def test_create_with_template_and_no_mailbox(self): + """Test domain creation with administrator but no mailbox.""" + values = { + "name": "pouet.com", "quota": 1000, "default_mailbox_quota": 100, + "create_dom_admin": True, "dom_admin_username": "toto", + "with_mailbox": False, "create_aliases": True, "type": "domain", + "stepid": "step3" + } + self.ajax_post(reverse("admin:domain_add"), values) + dom = Domain.objects.get(name="pouet.com") + da = User.objects.get(username="toto@pouet.com") + self.assertIn(da, dom.admins) + self.assertFalse(hasattr(da, "mailbox")) + self.assertFalse( + Alias.objects.filter(address="postmaster@pouet.com").exists()) + def test_create_with_template_and_empty_quota(self): """Test the creation of a domain with a template and no quota""" values = { "name": "pouet.com", "quota": 0, "default_mailbox_quota": 0, "create_dom_admin": True, "dom_admin_username": "toto", - "create_aliases": True, "type": "domain", "stepid": 'step3' + "create_aliases": True, "type": "domain", "stepid": "step3", + "with_mailbox": True } self.ajax_post( reverse("admin:domain_add"), @@ -108,7 +127,8 @@ class DomainTestCase(ModoTestCase): values = { "name": "pouet.com", "quota": 0, "default_mailbox_quota": 0, "create_dom_admin": True, "dom_admin_username": "toto", - "create_aliases": True, "type": "domain", "stepid": 'step3' + "create_aliases": True, "type": "domain", "stepid": "step3", + "with_mailbox": True } self.ajax_post( reverse("admin:domain_add"),