Allow domain administrators to set nul mailbox quota when the domain quota is nul.

fixes #486
This commit is contained in:
Antoine Nguyen
2013-10-12 20:10:06 +02:00
parent 1db6c6373c
commit f148eb2968
5 changed files with 51 additions and 11 deletions
+9 -7
View File
@@ -1,5 +1,3 @@
import factory
from modoboa.core.models import User
from modoboa.core.factories import PermissionFactory, UserFactory
from . import models
@@ -14,8 +12,7 @@ class DomainFactory(PermissionFactory):
class MailboxFactory(PermissionFactory):
FACTORY_FOR = models.Mailbox
quota = 0
use_domain_quota = True
quota = 10
class AliasFactory(PermissionFactory):
@@ -29,11 +26,11 @@ def populate_database():
"""
dom = DomainFactory.create(name="test.com")
admin = UserFactory(
username="admin@test.com", groups=('DomainAdmins',)
username="admin@test.com", groups=('DomainAdmins', ), password='{PLAIN}toto'
)
MailboxFactory(address='admin', domain=dom, user=admin)
account = UserFactory.create(
username="user@test.com", groups=('SimpleUsers',)
username="user@test.com", groups=('SimpleUsers',),
)
MailboxFactory.create(address='user', domain=dom, user=account)
@@ -49,8 +46,13 @@ def populate_database():
)
dom.add_admin(admin)
dom2 = DomainFactory.create(name='test2.com')
dom2 = DomainFactory.create(name='test2.com', quota=0)
admin = UserFactory.create(
username='admin@test2.com', groups=('DomainAdmins',), password='{PLAIN}toto'
)
MailboxFactory.create(address='admin', domain=dom2, user=admin)
u = UserFactory.create(
username='user@test2.com', groups=('SimpleUsers',)
)
MailboxFactory.create(address='user', domain=dom2, user=u)
dom2.add_admin(admin)
+3 -1
View File
@@ -187,7 +187,9 @@ class AccountFormMail(forms.Form, DynamicForm):
self.mb.rename(local_part, domain)
self.mb.use_domain_quota = self.cleaned_data["quota_act"]
override_rules = True if not self.mb.quota or user.has_perm("admin.add_domain") else False
override_rules = True \
if not self.mb.quota or user.has_perm("admin.add_domain") \
else False
self.mb.set_quota(self.cleaned_data["quota"], override_rules)
self.mb.save()
+14 -1
View File
@@ -144,6 +144,11 @@ class Mailbox(DatesAware):
def set_quota(self, value=None, override_rules=False):
"""Set or update quota's value for this mailbox.
A value equal to 0 means the mailbox won't have any quota. The
following cases allow people to define such behaviour:
* The domain has no quota
* :keyword:`override_rules` is True
:param integer value: the quota's value
:param bool override_rules: allow to override defined quota rules
"""
@@ -158,14 +163,22 @@ class Mailbox(DatesAware):
)
else:
self.quota = value
if not self.quota and not override_rules:
if not self.quota and self.domain.quota and not override_rules:
raise AdminError(_("A quota is required"))
def get_quota(self):
"""Get quota limit.
:rtype: int
"""
q = Quota.objects.get(username=self.full_address)
return int(q.bytes / 1048576)
def get_quota_in_percent(self):
"""Get current quota usage.
:rtype: int
"""
if not self.quota:
return 0
q = Quota.objects.get(username=self.full_address)
+23
View File
@@ -52,6 +52,29 @@ class AccountTestCase(ModoTestCase):
{}
)
def _set_quota(self, email, value, expected_status="ok"):
account = User.objects.get(username=email)
values = {
"username": email, "role": "SimpleUsers", "quota_act": False,
"is_active": True, "quota": value, "email": email
}
self.check_ajax_post(
reverse("modoboa.extensions.admin.views.identity.editaccount",
args=[account.id]),
values, status=expected_status
)
def test_set_nul_quota_as_superadmin(self):
self._set_quota("user@test.com", 0)
def test_set_nul_quota_as_domainadmin(self):
self.clt.logout()
self.assertTrue(self.clt.login(username="admin@test.com", password="toto"))
self._set_quota("user@test.com", 0, "ko")
self.clt.logout()
self.assertTrue(self.clt.login(username="admin@test2.com", password="toto"))
self._set_quota("user@test2.com", 0)
class PermissionsTestCase(ModoTestCase):
fixtures = ["initial_users.json"]
+2 -2
View File
@@ -22,7 +22,7 @@ class ExportTestCase(ModoTestCase):
def test_export_identities(self):
response = self.__export_identities()
self.assertEqual(response.content, "account;admin;{CRYPT}dTTsGDkA5ZHKg;;;True;SuperAdmins;\r\naccount;admin@test.com;{PLAIN}toto;;;True;DomainAdmins;admin@test.com;test.com\r\naccount;user@test.com;{PLAIN}toto;;;True;SimpleUsers;user@test.com\r\naccount;user@test2.com;{PLAIN}toto;;;True;SimpleUsers;user@test2.com\r\nalias;alias@test.com;False;user@test.com\r\nforward;forward@test.com;False;user@external.com\r\ndlist;postmaster@test.com;False;toto@titi.com;test@truc.fr\r\n")
self.assertEqual(response.content, "account;admin;{CRYPT}dTTsGDkA5ZHKg;;;True;SuperAdmins;\r\naccount;admin@test.com;{PLAIN}toto;;;True;DomainAdmins;admin@test.com;test.com\r\naccount;admin@test2.com;{PLAIN}toto;;;True;DomainAdmins;admin@test2.com;test2.com\r\naccount;user@test.com;{PLAIN}toto;;;True;SimpleUsers;user@test.com\r\naccount;user@test2.com;{PLAIN}toto;;;True;SimpleUsers;user@test2.com\r\nalias;alias@test.com;False;user@test.com\r\nforward;forward@test.com;False;user@external.com\r\ndlist;postmaster@test.com;False;toto@titi.com;test@truc.fr\r\n")
def test_export_simpleusers(self):
response = self.__export_identities(
@@ -48,7 +48,7 @@ class ExportTestCase(ModoTestCase):
)
self.assertEqual(
response.content.strip(),
"account;admin@test.com;{PLAIN}toto;;;True;DomainAdmins;admin@test.com;test.com"
"account;admin@test.com;{PLAIN}toto;;;True;DomainAdmins;admin@test.com;test.com\r\naccount;admin@test2.com;{PLAIN}toto;;;True;DomainAdmins;admin@test2.com;test2.com"
)
def test_export_aliases(self):