Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

Enhancement: unique mail rule names by owner

shamoon81e4092f53e7
diff --git a/src/documents/tests/test_migration_workflows.py b/src/documents/tests/test_migration_workflows.py
index 742757783..507cb0c18 100644
--- a/src/documents/tests/test_migration_workflows.py
+++ b/src/documents/tests/test_migration_workflows.py
@@ -5,9 +5,7 @@ from documents.tests.utils import TestMigrations
class TestMigrateWorkflow(TestMigrations):
migrate_from = "1043_alter_savedviewfilterrule_rule_type"
migrate_to = "1044_workflow_workflowaction_workflowtrigger_and_more"
- dependencies = (
- ("paperless_mail", "0023_remove_mailrule_filter_attachment_filename_and_more"),
- )
+ dependencies = (("paperless_mail", "0024_alter_mailrule_name_and_more"),)
def setUpBeforeMigration(self, apps):
User = apps.get_model("auth", "User")
diff --git a/src/paperless_mail/migrations/0024_alter_mailrule_name_and_more.py b/src/paperless_mail/migrations/0024_alter_mailrule_name_and_more.py
new file mode 100644
index 000000000..c2840d0e4
--- /dev/null
+++ b/src/paperless_mail/migrations/0024_alter_mailrule_name_and_more.py
@@ -0,0 +1,33 @@
+# Generated by Django 4.2.11 on 2024-06-05 16:51
+
+from django.db import migrations
+from django.db import models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("paperless_mail", "0023_remove_mailrule_filter_attachment_filename_and_more"),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name="mailrule",
+ name="name",
+ field=models.CharField(max_length=256, verbose_name="name"),
+ ),
+ migrations.AddConstraint(
+ model_name="mailrule",
+ constraint=models.UniqueConstraint(
+ fields=("name", "owner"),
+ name="paperless_mail_mailrule_unique_name_owner",
+ ),
+ ),
+ migrations.AddConstraint(
+ model_name="mailrule",
+ constraint=models.UniqueConstraint(
+ condition=models.Q(("owner__isnull", True)),
+ fields=("name",),
+ name="paperless_mail_mailrule_name_unique",
+ ),
+ ),
+ ]
diff --git a/src/paperless_mail/models.py b/src/paperless_mail/models.py
index 2343259a8..c53b16f1f 100644
--- a/src/paperless_mail/models.py
+++ b/src/paperless_mail/models.py
@@ -59,6 +59,17 @@ class MailRule(document_models.ModelWithOwner):
class Meta:
verbose_name = _("mail rule")
verbose_name_plural = _("mail rules")
+ constraints = [
+ models.UniqueConstraint(
+ fields=["name", "owner"],
+ name="%(app_label)s_%(class)s_unique_name_owner",
+ ),
+ models.UniqueConstraint(
+ name="%(app_label)s_%(class)s_name_unique",
+ fields=["name"],
+ condition=models.Q(owner__isnull=True),
+ ),
+ ]
class ConsumptionScope(models.IntegerChoices):
ATTACHMENTS_ONLY = 1, _("Only process attachments.")
@@ -93,7 +104,7 @@ class MailRule(document_models.ModelWithOwner):
FROM_NAME = 3, _("Use name (or mail address if not available)")
FROM_CUSTOM = 4, _("Use correspondent selected below")
- name = models.CharField(_("name"), max_length=256, unique=True)
+ name = models.CharField(_("name"), max_length=256)
order = models.IntegerField(_("order"), default=0)