Enforce mailbox storage quotas from hosting plans #30

Merged
parkel merged 3 commits from feature/mailbox-storage-quotas into main 2026-11-21 13:22:31 +01:00
9 changed files with 142 additions and 6 deletions
Showing only changes of commit 6a1078b055 - Show all commits

No files matched your search

+7 -1
View File
@@ -46,7 +46,13 @@ class HostingAccountAdmin(admin.ModelAdmin):
@admin.register(HostingPlan)
class HostingPlanAdmin(admin.ModelAdmin):
list_display = ("name", "description", "database_limit", "mailbox_limit")
list_display = (
"name",
"description",
"database_limit",
"mailbox_limit",
"mailbox_quota_mib",
)
search_fields = ("name",)
def save_model(self, request, obj, form, change):
@@ -0,0 +1,43 @@
# Generated by Django 5.2.19 on 2026-11-21 11:56
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("hosting", "0020_account_backup_job"),
]
operations = [
migrations.AddField(
model_name="hostingmailbox",
name="quota_mib",
field=models.PositiveIntegerField(
blank=True,
null=True,
validators=[
django.core.validators.MinValueValidator(1),
django.core.validators.MaxValueValidator(2147483647),
],
verbose_name="storage limit (MiB)",
),
),
migrations.AddField(
model_name="hostingplan",
name="mailbox_quota_mib",
field=models.PositiveIntegerField(
blank=True,
help_text=(
"Applies to new mailbox requests. "
"Leave blank for unlimited."
),
null=True,
validators=[
django.core.validators.MinValueValidator(1),
django.core.validators.MaxValueValidator(2147483647),
],
verbose_name="mailbox storage limit (MiB)",
),
),
]
+34 -3
View File
@@ -1,7 +1,11 @@
import uuid
from django.conf import settings
from django.core.validators import RegexValidator
from django.core.validators import (
MaxValueValidator,
MinValueValidator,
RegexValidator,
)
from django.db import models
from django.urls import reverse
from django.utils import timezone
@@ -19,7 +23,11 @@ from coulobre.dns_node import (
MxRecord,
TxtRecord,
)
from coulobre.mail_node import LOCAL_PART_MAX_LENGTH, MailboxSpec
from coulobre.mail_node import (
LOCAL_PART_MAX_LENGTH,
MAX_MAILBOX_QUOTA_MIB,
MailboxSpec,
)
from .fields import DomainNameField
@@ -128,6 +136,18 @@ class HostingPlan(models.Model):
"Leave blank for unlimited. Enter 0 to block new mailbox requests."
),
)
mailbox_quota_mib = models.PositiveIntegerField(
"mailbox storage limit (MiB)",
blank=True,
null=True,
validators=[
MinValueValidator(1),
MaxValueValidator(MAX_MAILBOX_QUOTA_MIB),
],
help_text=(
"Applies to new mailbox requests. Leave blank for unlimited."
),
)
class Meta:
ordering = ("name", "pk")
@@ -252,6 +272,15 @@ class HostingMailbox(models.Model):
HostingMailDomain, on_delete=models.PROTECT, related_name="mailboxes"
)
local_part = models.CharField(max_length=LOCAL_PART_MAX_LENGTH)
quota_mib = models.PositiveIntegerField(
"storage limit (MiB)",
blank=True,
null=True,
validators=[
MinValueValidator(1),
MaxValueValidator(MAX_MAILBOX_QUOTA_MIB),
],
)
class Meta:
ordering = ("mail_domain__domain", "local_part")
@@ -267,7 +296,9 @@ class HostingMailbox(models.Model):
@property
def spec(self):
return MailboxSpec(self.mail_domain.domain, self.local_part)
return MailboxSpec(
self.mail_domain.domain, self.local_part, self.quota_mib
)
class WebProvisioningPlan(models.Model):
+4 -1
View File
@@ -65,4 +65,7 @@ def create_mailbox_request(account_id, local_part, *, customer_id=None):
raise ValidationError(
"This hosting account has reached its mailbox limit."
)
return mail_domain.mailboxes.create(local_part=spec.local_part)
return mail_domain.mailboxes.create(
local_part=spec.local_part,
quota_mib=plan.mailbox_quota_mib if plan is not None else None,
)
@@ -174,6 +174,7 @@
<thead>
<tr>
<th scope="col">Email address</th>
<th scope="col">Storage limit</th>
{% if is_operator %}<th scope="col">Deployment</th>{% endif %}
</tr>
</thead>
@@ -181,6 +182,7 @@
{% for mailbox in mailboxes %}
<tr>
<td class="domain">{{ mailbox }}</td>
<td>{% if mailbox.quota_mib is not None %}{{ mailbox.quota_mib }} MiB{% else %}Unlimited{% endif %}</td>
{% if is_operator %}
<td>
<div class="form-actions">
@@ -17,6 +17,7 @@
<th scope="col">Description</th>
<th scope="col">Database limit</th>
<th scope="col">Mailbox limit</th>
<th scope="col">Storage per new mailbox</th>
</tr>
</thead>
<tbody>
@@ -32,6 +33,7 @@
<td>{{ hosting_plan.description|default:"—" }}</td>
<td>{{ hosting_plan.database_limit|default_if_none:"Unlimited" }}</td>
<td>{{ hosting_plan.mailbox_limit|default_if_none:"Unlimited" }}</td>
<td>{% if hosting_plan.mailbox_quota_mib is not None %}{{ hosting_plan.mailbox_quota_mib }} MiB{% else %}Unlimited{% endif %}</td>
</tr>
{% endfor %}
</tbody>
@@ -35,12 +35,15 @@ class HostingPlanTests(TestCase):
"description": "Shared hosting for small sites",
"database_limit": 0,
"mailbox_limit": 5,
"mailbox_quota_mib": 256,
},
)
self.assertRedirects(response, list_url)
plan = HostingPlan.objects.get()
self.assertEqual(plan.database_limit, 0)
self.assertEqual(plan.mailbox_limit, 5)
self.assertEqual(plan.mailbox_quota_mib, 256)
self.assertContains(self.client.get(list_url), "256 MiB")
self.assertContains(
self.client.get(self.account.get_absolute_url()), "Unassigned"
@@ -64,12 +67,14 @@ class HostingPlanTests(TestCase):
"description": "Updated description",
"database_limit": "",
"mailbox_limit": 2,
"mailbox_quota_mib": "",
},
)
self.assertRedirects(response, list_url)
plan.refresh_from_db()
self.assertIsNone(plan.database_limit)
self.assertEqual(plan.mailbox_limit, 2)
self.assertIsNone(plan.mailbox_quota_mib)
self.assertContains(self.client.get(list_url), "Unlimited")
self.assertContains(
self.client.get(reverse("panel:accounts")), "Standard"
@@ -95,10 +100,12 @@ class HostingPlanTests(TestCase):
"name": "Invalid plan",
"database_limit": -1,
"mailbox_limit": -2,
"mailbox_quota_mib": 0,
},
)
self.assertIn("database_limit", response.context["form"].errors)
self.assertIn("mailbox_limit", response.context["form"].errors)
self.assertIn("mailbox_quota_mib", response.context["form"].errors)
self.assertFalse(HostingPlan.objects.exists())
def test_editor_without_plan_permission_cannot_assign_in_either_interface(
+36
View File
@@ -97,6 +97,36 @@ class MailboxRequestTests(TestCase):
)
self.assertEqual(HostingMailbox.objects.count(), 2)
def test_mailbox_storage_limit_is_captured_when_requested(self):
plan = HostingPlan.objects.create(name="Mail", mailbox_quota_mib=128)
HostingAccount.objects.filter(pk=self.account.pk).update(
hosting_plan=plan
)
original = self.request_mailbox("support")
HostingPlan.objects.filter(pk=plan.pk).update(mailbox_quota_mib=256)
self.assertRedirects(
self.client.post(self.request_url, {"local_part": "billing"}),
self.account.get_absolute_url(),
)
original.refresh_from_db()
self.assertEqual(original.quota_mib, 128)
self.assertEqual(
HostingMailbox.objects.get(local_part="billing").quota_mib, 256
)
response = self.client.get(
reverse("panel:mailbox_manifest", args=[original.pk])
)
self.assertEqual(response.json()["version"], 2)
self.assertEqual(response.json()["quota_mib"], 128)
self.assertEqual(
MailboxSpec.from_manifest(response.json()), original.spec
)
page = self.client.get(self.account.get_absolute_url())
self.assertContains(page, "128 MiB")
self.assertContains(page, "256 MiB")
def test_invalid_request_leaves_the_domain_available(self):
response = self.client.post(
self.request_url, {"local_part": "support+tag"}
@@ -250,3 +280,9 @@ class MailboxManifestTests(SimpleTestCase):
):
with self.subTest(change=change), self.assertRaises(ValueError):
MailboxSpec.from_manifest(manifest | change)
def test_quota_manifest_requires_a_positive_integer_limit(self):
manifest = MailboxSpec("example.net", "support", 128).manifest()
for size in (None, False, 0, -1, "128", 2**31):
with self.subTest(size=size), self.assertRaises(ValueError):
MailboxSpec.from_manifest(manifest | {"quota_mib": size})
+7 -1
View File
@@ -827,7 +827,13 @@ class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
class HostingPlanEditor:
model = HostingPlan
fields = ("name", "description", "database_limit", "mailbox_limit")
fields = (
"name",
"description",
"database_limit",
"mailbox_limit",
"mailbox_quota_mib",
)
template_name = "hosting/hosting_plan_form.html"
success_url = reverse_lazy("panel:hosting_plans")