mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-17 05:41:25 +02:00
@@ -16,7 +16,7 @@ class DomainFactory(PermissionFactory):
|
||||
django_get_or_create = ("name", )
|
||||
|
||||
type = "domain"
|
||||
quota = 10
|
||||
default_mailbox_quota = 10
|
||||
enabled = True
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ def populate_database():
|
||||
|
||||
dom.add_admin(admin)
|
||||
|
||||
dom2 = DomainFactory.create(name='test2.com', quota=0)
|
||||
dom2 = DomainFactory.create(name='test2.com', default_mailbox_quota=0)
|
||||
admin = UserFactory.create(
|
||||
username='admin@test2.com', groups=('DomainAdmins',),
|
||||
password='{PLAIN}toto'
|
||||
|
||||
@@ -33,8 +33,8 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
|
||||
type = forms.ChoiceField(
|
||||
label=ugettext_lazy("Type"),
|
||||
)
|
||||
quota = forms.IntegerField(
|
||||
label=ugettext_lazy("Quota"),
|
||||
default_mailbox_quota = forms.IntegerField(
|
||||
label=ugettext_lazy("Default mailbox quota"),
|
||||
required=False,
|
||||
help_text=ugettext_lazy(
|
||||
"Default quota in MB applied to mailboxes. Leave empty to use the "
|
||||
@@ -53,7 +53,8 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
|
||||
class Meta:
|
||||
model = Domain
|
||||
fields = (
|
||||
"name", "type", "quota", "aliases", "enabled", "enable_dns_checks")
|
||||
"name", "type", "default_mailbox_quota", "aliases", "enabled",
|
||||
"enable_dns_checks")
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.oldname = None
|
||||
@@ -68,7 +69,7 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
|
||||
)
|
||||
self.fields["type"].choices = DOMAIN_TYPES + extra_domain_types
|
||||
self.field_widths = {
|
||||
"quota": 3
|
||||
"default_mailbox_quota": 3
|
||||
}
|
||||
|
||||
if len(args) and isinstance(args[0], QueryDict):
|
||||
@@ -79,13 +80,13 @@ class DomainFormGeneral(forms.ModelForm, DynamicForm):
|
||||
name = "aliases_%d" % (pos + 1)
|
||||
self._create_field(forms.CharField, name, dalias.name, 3)
|
||||
|
||||
def clean_quota(self):
|
||||
def clean_default_mailbox_quota(self):
|
||||
"""Check quota value."""
|
||||
if self.cleaned_data["quota"] is None:
|
||||
if self.cleaned_data["default_mailbox_quota"] is None:
|
||||
return param_tools.get_global_parameter("default_domain_quota")
|
||||
if self.cleaned_data["quota"] < 0:
|
||||
if self.cleaned_data["default_mailbox_quota"] < 0:
|
||||
raise forms.ValidationError(_("Must be a positive integer"))
|
||||
return self.cleaned_data["quota"]
|
||||
return self.cleaned_data["default_mailbox_quota"]
|
||||
|
||||
def clean(self):
|
||||
"""Custom fields validation.
|
||||
|
||||
@@ -27,7 +27,7 @@ def update_domain_mxs_and_mailboxes(sender, instance, **kwargs):
|
||||
if kwargs.get("created"):
|
||||
return
|
||||
instance.mailbox_set.filter(use_domain_quota=True).update(
|
||||
quota=instance.quota)
|
||||
quota=instance.default_mailbox_quota)
|
||||
if instance.old_mail_homes is None:
|
||||
return
|
||||
qset = (
|
||||
@@ -155,7 +155,8 @@ def account_auto_created(sender, user, **kwargs):
|
||||
domname, [(models.DomainAlias, _("domain alias"))])
|
||||
if label is not None:
|
||||
return
|
||||
domain = models.Domain(name=domname, enabled=True, quota=0)
|
||||
domain = models.Domain(
|
||||
name=domname, enabled=True, default_mailbox_quota=0)
|
||||
domain.save(creator=sadmins[0])
|
||||
for su in sadmins[1:]:
|
||||
permissions.grant_access_to_object(su, domain)
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-15 08:48
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('admin', '0008_domain_enable_dns_checks'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameField(
|
||||
model_name='domain',
|
||||
old_name='quota',
|
||||
new_name='default_mailbox_quota',
|
||||
),
|
||||
]
|
||||
@@ -42,7 +42,8 @@ class Domain(AdminObject):
|
||||
|
||||
name = models.CharField(ugettext_lazy('name'), max_length=100, unique=True,
|
||||
help_text=ugettext_lazy("The domain name"))
|
||||
quota = models.IntegerField()
|
||||
# quota = models.IntegerField()
|
||||
default_mailbox_quota = models.IntegerField()
|
||||
enabled = models.BooleanField(
|
||||
ugettext_lazy('enabled'),
|
||||
help_text=ugettext_lazy("Check to activate this domain"),
|
||||
@@ -210,7 +211,7 @@ class Domain(AdminObject):
|
||||
|
||||
The expected fields order is the following::
|
||||
|
||||
"domain", name, quota, enabled
|
||||
"domain", name, default_mailbox_quota, enabled
|
||||
|
||||
:param ``core.User`` user: user creating the domain
|
||||
:param str row: a list containing domain's definition
|
||||
@@ -221,15 +222,17 @@ class Domain(AdminObject):
|
||||
if Domain.objects.filter(name=self.name).exists():
|
||||
raise Conflict
|
||||
try:
|
||||
self.quota = int(row[2].strip())
|
||||
self.default_mailbox_quota = int(row[2].strip())
|
||||
except ValueError:
|
||||
raise BadRequest(
|
||||
_("Invalid quota value for domain '%s'") % self.name)
|
||||
_("Invalid default mailbox quota value for domain '%s'")
|
||||
% self.name)
|
||||
self.enabled = (row[3].strip() in ["True", "1", "yes", "y"])
|
||||
self.save(creator=user)
|
||||
|
||||
def to_csv(self, csvwriter):
|
||||
csvwriter.writerow(["domain", self.name, self.quota, self.enabled])
|
||||
csvwriter.writerow([
|
||||
"domain", self.name, self.default_mailbox_quot, self.enabled])
|
||||
for dalias in self.domainalias_set.all():
|
||||
dalias.to_csv(csvwriter)
|
||||
|
||||
|
||||
@@ -228,19 +228,20 @@ class Mailbox(AdminObject):
|
||||
:param integer value: the quota's value
|
||||
:param bool override_rules: allow to override defined quota rules
|
||||
"""
|
||||
default_mailbox_quota = self.domain.default_mailbox_quota
|
||||
if value is None:
|
||||
if self.use_domain_quota:
|
||||
self.quota = self.domain.quota
|
||||
self.quota = default_mailbox_quota
|
||||
else:
|
||||
self.quota = 0
|
||||
elif int(value) > self.domain.quota and not override_rules:
|
||||
elif int(value) > default_mailbox_quota and not override_rules:
|
||||
raise lib_exceptions.BadRequest(
|
||||
_("Quota is greater than the allowed domain's limit (%dM)") %
|
||||
self.domain.quota
|
||||
default_mailbox_quota
|
||||
)
|
||||
else:
|
||||
self.quota = value
|
||||
if not self.quota and self.domain.quota and not override_rules:
|
||||
if not self.quota and default_mailbox_quota and not override_rules:
|
||||
raise lib_exceptions.BadRequest(_("A quota is required"))
|
||||
|
||||
def get_quota(self):
|
||||
|
||||
@@ -23,7 +23,7 @@ class DomainSerializer(serializers.ModelSerializer):
|
||||
|
||||
class Meta:
|
||||
model = models.Domain
|
||||
fields = ("pk", "name", "quota", "enabled", "type", )
|
||||
fields = ("pk", "name", "default_mailbox_quota", "enabled", "type", )
|
||||
|
||||
def create(self, validated_data):
|
||||
"""Set permissions."""
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{% load i18n form_tags %}
|
||||
{% render_field form.name %}
|
||||
{% render_field form.type %}
|
||||
{% render_field form.quota appended_text=_("MB") %}
|
||||
{% render_field form.default_mailbox_quota appended_text=_("MB") %}
|
||||
{% render_fields_group form "aliases" %}
|
||||
{% render_field form.enabled %}
|
||||
{% render_field form.enable_dns_checks %}
|
||||
|
||||
@@ -43,18 +43,20 @@ class DomainAPITestCase(ModoAPITestCase):
|
||||
def test_create_domain(self):
|
||||
"""Check domain creation."""
|
||||
url = reverse("external_api:domain-list")
|
||||
response = self.client.post(url, {"name": "test3.com", "quota": 10})
|
||||
response = self.client.post(
|
||||
url, {"name": "test3.com", "default_mailbox_quota": 10})
|
||||
self.assertEqual(response.status_code, 201)
|
||||
self.assertTrue(
|
||||
models.Domain.objects.filter(name="test3.com").exists())
|
||||
response = self.client.post(url, {})
|
||||
self.assertEqual(response.status_code, 400)
|
||||
self.assertIn("name", response.data)
|
||||
self.assertIn("quota", response.data)
|
||||
self.assertIn("default_mailbox_quota", response.data)
|
||||
|
||||
self.client.credentials(
|
||||
HTTP_AUTHORIZATION='Token ' + self.da_token.key)
|
||||
response = self.client.post(url, {"name": "test4.com", "quota": 10})
|
||||
response = self.client.post(
|
||||
url, {"name": "test4.com", "default_mailbox_quota": 10})
|
||||
self.assertEqual(response.status_code, 403)
|
||||
|
||||
def test_update_domain(self):
|
||||
@@ -64,15 +66,17 @@ class DomainAPITestCase(ModoAPITestCase):
|
||||
domain__name="test.com", address="user").update(
|
||||
use_domain_quota=True)
|
||||
url = reverse("external_api:domain-detail", args=[domain.pk])
|
||||
response = self.client.put(url, {"name": "test.com", "quota": 1000})
|
||||
response = self.client.put(
|
||||
url, {"name": "test.com", "default_mailbox_quota": 1000})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
domain.refresh_from_db()
|
||||
self.assertEqual(domain.quota, 1000)
|
||||
self.assertEqual(domain.default_mailbox_quota, 1000)
|
||||
mb = models.Mailbox.objects.get(
|
||||
domain__name="test.com", address="user")
|
||||
self.assertEqual(mb.quota, 1000)
|
||||
|
||||
response = self.client.put(url, {"name": "test42.com", "quota": 1000})
|
||||
response = self.client.put(
|
||||
url, {"name": "test42.com", "default_mailbox_quota": 1000})
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(
|
||||
models.Mailbox.objects.filter(
|
||||
@@ -267,7 +271,8 @@ class AccountAPITestCase(ModoAPITestCase):
|
||||
user = core_models.User.objects.filter(pk=account["pk"]).first()
|
||||
self.assertIsNot(user, None)
|
||||
self.assertIsNot(user.mailbox, None)
|
||||
self.assertEqual(user.mailbox.quota, user.mailbox.domain.quota)
|
||||
self.assertEqual(
|
||||
user.mailbox.quota, user.mailbox.domain.default_mailbox_quota)
|
||||
|
||||
def test_create_existing_account(self):
|
||||
"""Check if unicity is respected."""
|
||||
|
||||
@@ -25,35 +25,35 @@ class DomainTestCase(ModoTestCase):
|
||||
def test_create(self):
|
||||
"""Test the creation of a domain."""
|
||||
values = {
|
||||
"name": "pouet.com", "quota": 100, "create_dom_admin": False,
|
||||
"type": "domain", "stepid": "step3"
|
||||
"name": "pouet.com", "default_mailbox_quota": 100,
|
||||
"create_dom_admin": False, "type": "domain", "stepid": "step3"
|
||||
}
|
||||
self.ajax_post(reverse("admin:domain_add"), values)
|
||||
dom = Domain.objects.get(name="pouet.com")
|
||||
self.assertEqual(dom.name, "pouet.com")
|
||||
self.assertEqual(dom.quota, 100)
|
||||
self.assertEqual(dom.default_mailbox_quota, 100)
|
||||
self.assertEqual(dom.enabled, False)
|
||||
self.assertFalse(dom.admins)
|
||||
|
||||
def test_create_utf8(self):
|
||||
"""Test the creation of a domain with non-ASCII characters."""
|
||||
values = {
|
||||
"name": "pouét.com", "quota": 100, "create_dom_admin": False,
|
||||
"type": "domain", "stepid": "step3"
|
||||
"name": "pouét.com", "default_mailbox_quota": 100,
|
||||
"create_dom_admin": False, "type": "domain", "stepid": "step3"
|
||||
}
|
||||
self.ajax_post(reverse("admin:domain_add"), values)
|
||||
dom = Domain.objects.get(name=u"pouét.com")
|
||||
self.assertEqual(dom.name, u"pouét.com")
|
||||
self.assertEqual(dom.quota, 100)
|
||||
self.assertEqual(dom.default_mailbox_quota, 100)
|
||||
self.assertEqual(dom.enabled, False)
|
||||
self.assertFalse(dom.admins)
|
||||
|
||||
def test_create_with_template(self):
|
||||
"""Test the creation of a domain with a template."""
|
||||
values = {
|
||||
"name": "pouet.com", "quota": 100, "create_dom_admin": True,
|
||||
"dom_admin_username": "toto", "create_aliases": True,
|
||||
"type": "domain", "stepid": 'step3'
|
||||
"name": "pouet.com", "default_mailbox_quota": 100,
|
||||
"create_dom_admin": True, "dom_admin_username": "toto",
|
||||
"create_aliases": True, "type": "domain", "stepid": 'step3'
|
||||
}
|
||||
self.ajax_post(reverse("admin:domain_add"), values)
|
||||
dom = Domain.objects.get(name="pouet.com")
|
||||
@@ -67,9 +67,9 @@ class DomainTestCase(ModoTestCase):
|
||||
self.assertTrue(da.can_access(al))
|
||||
|
||||
values = {
|
||||
"name": "pouet2.com", "quota": 100, "create_dom_admin": True,
|
||||
"dom_admin_username": "postmaster", "create_aliases": True,
|
||||
"type": "domain", "stepid": "step3"
|
||||
"name": "pouet2.com", "default_mailbox_quota": 100,
|
||||
"create_dom_admin": True, "dom_admin_username": "postmaster",
|
||||
"create_aliases": True, "type": "domain", "stepid": "step3"
|
||||
}
|
||||
self.ajax_post(reverse("admin:domain_add"), values)
|
||||
self.assertTrue(
|
||||
@@ -81,9 +81,9 @@ class DomainTestCase(ModoTestCase):
|
||||
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, "create_dom_admin": True,
|
||||
"dom_admin_username": "toto", "create_aliases": True,
|
||||
"type": "domain", "stepid": 'step3'
|
||||
"name": "pouet.com", "default_mailbox_quota": 0,
|
||||
"create_dom_admin": True, "dom_admin_username": "toto",
|
||||
"create_aliases": True, "type": "domain", "stepid": 'step3'
|
||||
}
|
||||
self.ajax_post(
|
||||
reverse("admin:domain_add"),
|
||||
@@ -104,9 +104,9 @@ class DomainTestCase(ModoTestCase):
|
||||
password = "Toto1000"
|
||||
self.set_global_parameter("default_password", password, app="core")
|
||||
values = {
|
||||
"name": "pouet.com", "quota": 0, "create_dom_admin": True,
|
||||
"dom_admin_username": "toto", "create_aliases": True,
|
||||
"type": "domain", "stepid": 'step3'
|
||||
"name": "pouet.com", "default_mailbox_quota": 0,
|
||||
"create_dom_admin": True, "dom_admin_username": "toto",
|
||||
"create_aliases": True, "type": "domain", "stepid": 'step3'
|
||||
}
|
||||
self.ajax_post(
|
||||
reverse("admin:domain_add"),
|
||||
@@ -136,7 +136,7 @@ class DomainTestCase(ModoTestCase):
|
||||
values
|
||||
)
|
||||
dom = Domain.objects.get(name="pouet.com")
|
||||
self.assertEqual(dom.quota, 50)
|
||||
self.assertEqual(dom.default_mailbox_quota, 50)
|
||||
da = User.objects.get(username="toto@pouet.com")
|
||||
self.assertEqual(da.mailbox.quota, 50)
|
||||
|
||||
@@ -146,8 +146,8 @@ class DomainTestCase(ModoTestCase):
|
||||
Rename 'test.com' domain to 'pouet.com'
|
||||
"""
|
||||
values = {
|
||||
"name": "pouet.com", "quota": 100, "type": "domain",
|
||||
"enabled": True
|
||||
"name": "pouet.com", "default_mailbox_quota": 100,
|
||||
"type": "domain", "enabled": True
|
||||
}
|
||||
dom = Domain.objects.get(name="test.com")
|
||||
self.ajax_post(
|
||||
|
||||
@@ -39,9 +39,9 @@ class DomainAliasTestCase(ModoTestCase):
|
||||
def test_form(self):
|
||||
dom = Domain.objects.get(name="test.com")
|
||||
values = dict(
|
||||
name=dom.name, quota=dom.quota, enabled=dom.enabled,
|
||||
aliases="domalias.net", aliases_1="domaliasé.com",
|
||||
type="domain"
|
||||
name=dom.name, default_mailbox_quota=dom.default_mailbox_quota,
|
||||
enabled=dom.enabled, aliases="domalias.net",
|
||||
aliases_1="domaliasé.com", type="domain"
|
||||
)
|
||||
self.ajax_post(
|
||||
reverse("admin:domain_change",
|
||||
|
||||
@@ -36,7 +36,7 @@ domainalias; domalias1.com; domain1.com; True
|
||||
)
|
||||
admin = User.objects.get(username="admin")
|
||||
dom = Domain.objects.get(name="domain1.com")
|
||||
self.assertEqual(dom.quota, 100)
|
||||
self.assertEqual(dom.default_mailbox_quota, 100)
|
||||
self.assertTrue(dom.enabled)
|
||||
self.assertTrue(admin.is_owner(dom))
|
||||
domalias = DomainAlias.objects.get(name="domalias1.com")
|
||||
@@ -44,7 +44,7 @@ domainalias; domalias1.com; domain1.com; True
|
||||
self.assertTrue(dom.enabled)
|
||||
self.assertTrue(admin.is_owner(domalias))
|
||||
dom = Domain.objects.get(name="domain2.com")
|
||||
self.assertEqual(dom.quota, 200)
|
||||
self.assertEqual(dom.default_mailbox_quota, 200)
|
||||
self.assertFalse(dom.enabled)
|
||||
self.assertTrue(admin.is_owner(dom))
|
||||
|
||||
@@ -200,7 +200,7 @@ account; user1@test.com; toto; User; One; True; SimpleUsers; user1@test.com
|
||||
account = User.objects.get(username="user1@test.com")
|
||||
self.assertEqual(
|
||||
account.mailbox.quota,
|
||||
account.mailbox.domain.quota
|
||||
account.mailbox.domain.default_mailbox_quota
|
||||
)
|
||||
|
||||
def test_import_duplicate(self):
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.10.5 on 2017-02-15 08:48
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('relaydomains', '0005_auto_20161105_1426'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='relaydomain',
|
||||
name='last_modification',
|
||||
field=models.DateTimeField(auto_now=True, default=django.utils.timezone.now),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user