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..b910934cc 100644 --- a/modoboa/admin/tests/test_domain.py +++ b/modoboa/admin/tests/test_domain.py @@ -1,9 +1,9 @@ # coding: utf-8 -from __future__ import unicode_literals - """Domain related test cases.""" +from __future__ import unicode_literals + from django.core.urlresolvers import reverse from modoboa.core.models import User @@ -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"), diff --git a/modoboa/limits/tests/test_user_limits.py b/modoboa/limits/tests/test_user_limits.py index e57618755..004a0f585 100644 --- a/modoboa/limits/tests/test_user_limits.py +++ b/modoboa/limits/tests/test_user_limits.py @@ -81,7 +81,7 @@ class ResourceTestCase(lib_tests.ModoTestCase): def _create_domain(self, name, status=200, withtpl=False, **kwargs): values = { "name": name, "quota": 100, "default_mailbox_quota": 10, - "create_dom_admin": False, + "create_dom_admin": False, "with_mailbox": True, "create_aliases": False, "stepid": "step3", "type": "domain" } if withtpl: