diff --git a/src/coulobre/hosting/admin.py b/src/coulobre/hosting/admin.py index 75ac3f6..95d7887 100644 --- a/src/coulobre/hosting/admin.py +++ b/src/coulobre/hosting/admin.py @@ -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): diff --git a/src/coulobre/hosting/migrations/0021_mailbox_storage_limits.py b/src/coulobre/hosting/migrations/0021_mailbox_storage_limits.py new file mode 100644 index 0000000..adf82ff --- /dev/null +++ b/src/coulobre/hosting/migrations/0021_mailbox_storage_limits.py @@ -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)", + ), + ), + ] diff --git a/src/coulobre/hosting/models.py b/src/coulobre/hosting/models.py index 8f12bd8..18d5041 100644 --- a/src/coulobre/hosting/models.py +++ b/src/coulobre/hosting/models.py @@ -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): diff --git a/src/coulobre/hosting/resource_requests.py b/src/coulobre/hosting/resource_requests.py index 33884b3..811d033 100644 --- a/src/coulobre/hosting/resource_requests.py +++ b/src/coulobre/hosting/resource_requests.py @@ -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, + ) diff --git a/src/coulobre/hosting/templates/hosting/account_detail.html b/src/coulobre/hosting/templates/hosting/account_detail.html index 5fcfbca..64526dd 100644 --- a/src/coulobre/hosting/templates/hosting/account_detail.html +++ b/src/coulobre/hosting/templates/hosting/account_detail.html @@ -174,6 +174,7 @@ Email address + Storage limit {% if is_operator %}Deployment{% endif %} @@ -181,6 +182,7 @@ {% for mailbox in mailboxes %} {{ mailbox }} + {% if mailbox.quota_mib is not None %}{{ mailbox.quota_mib }} MiB{% else %}Unlimited{% endif %} {% if is_operator %}
diff --git a/src/coulobre/hosting/templates/hosting/hosting_plan_list.html b/src/coulobre/hosting/templates/hosting/hosting_plan_list.html index 9bf7016..080c886 100644 --- a/src/coulobre/hosting/templates/hosting/hosting_plan_list.html +++ b/src/coulobre/hosting/templates/hosting/hosting_plan_list.html @@ -17,6 +17,7 @@ Description Database limit Mailbox limit + Storage per new mailbox @@ -32,6 +33,7 @@ {{ hosting_plan.description|default:"—" }} {{ hosting_plan.database_limit|default_if_none:"Unlimited" }} {{ hosting_plan.mailbox_limit|default_if_none:"Unlimited" }} + {% if hosting_plan.mailbox_quota_mib is not None %}{{ hosting_plan.mailbox_quota_mib }} MiB{% else %}Unlimited{% endif %} {% endfor %} diff --git a/src/coulobre/hosting/test_hosting_plans.py b/src/coulobre/hosting/test_hosting_plans.py index 7ba51c8..72b0afe 100644 --- a/src/coulobre/hosting/test_hosting_plans.py +++ b/src/coulobre/hosting/test_hosting_plans.py @@ -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( diff --git a/src/coulobre/hosting/test_mailboxes.py b/src/coulobre/hosting/test_mailboxes.py index c0ed21e..4690fa6 100644 --- a/src/coulobre/hosting/test_mailboxes.py +++ b/src/coulobre/hosting/test_mailboxes.py @@ -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}) diff --git a/src/coulobre/hosting/views.py b/src/coulobre/hosting/views.py index 9fb6ce8..fc94ba9 100644 --- a/src/coulobre/hosting/views.py +++ b/src/coulobre/hosting/views.py @@ -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")