]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Allow setting more than one tag in mail rules
authorjonasc <github@bcdf.eu>
Fri, 11 Mar 2022 15:15:42 +0000 (16:15 +0100)
committerjonasc <github@bcdf.eu>
Tue, 12 Apr 2022 08:41:08 +0000 (10:41 +0200)
The three migrations do the following to preserve existing data in
assign_tag:
1. Add the new many-to-many field assign_tags.
2. Copy existing data from the assign_tag field to the assign_tags.
3. Delete the existing assign_tag field.

src/paperless_mail/admin.py
src/paperless_mail/mail.py
src/paperless_mail/migrations/0009_mailrule_assign_tags.py [new file with mode: 0644]
src/paperless_mail/migrations/0010_auto_20220311_1602.py [new file with mode: 0644]
src/paperless_mail/migrations/0011_remove_mailrule_assign_tag.py [new file with mode: 0644]
src/paperless_mail/models.py

index b56bc0727e89ca1d348b237eae72df5be7ea3c2b..3b488b153410653eb6790b709aecc03e67a3c008 100644 (file)
@@ -82,7 +82,7 @@ class MailRuleAdmin(admin.ModelAdmin):
                 ),
                 "fields": (
                     "assign_title_from",
-                    "assign_tag",
+                    "assign_tags",
                     "assign_document_type",
                     "assign_correspondent_from",
                     "assign_correspondent",
index 5df29e2b0d4425150993dda47b64b6a790007201..3295f88ca21001ee0f9406d62385e76a1d38f7e3 100644 (file)
@@ -265,7 +265,7 @@ class MailAccountHandler(LoggingMixin):
         )
 
         correspondent = self.get_correspondent(message, rule)
-        tag = rule.assign_tag
+        tag_ids = [tag.id for tag in rule.assign_tags.all()]
         doc_type = rule.assign_document_type
 
         processed_attachments = 0
@@ -328,7 +328,7 @@ class MailAccountHandler(LoggingMixin):
                     if correspondent
                     else None,
                     override_document_type_id=doc_type.id if doc_type else None,
-                    override_tag_ids=[tag.id] if tag else None,
+                    override_tag_ids=tag_ids,
                     task_name=att.filename[:100],
                 )
 
diff --git a/src/paperless_mail/migrations/0009_mailrule_assign_tags.py b/src/paperless_mail/migrations/0009_mailrule_assign_tags.py
new file mode 100644 (file)
index 0000000..fe2447e
--- /dev/null
@@ -0,0 +1,23 @@
+# Generated by Django 3.2.12 on 2022-03-11 15:00
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("paperless_mail", "0008_auto_20210516_0940"),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name="mailrule",
+            name="assign_tags",
+            field=models.ManyToManyField(
+                blank=True,
+                related_name="mail_rules_multi",
+                to="documents.Tag",
+                verbose_name="assign this tag",
+            ),
+        ),
+    ]
diff --git a/src/paperless_mail/migrations/0010_auto_20220311_1602.py b/src/paperless_mail/migrations/0010_auto_20220311_1602.py
new file mode 100644 (file)
index 0000000..bf04813
--- /dev/null
@@ -0,0 +1,26 @@
+# Generated by Django 3.2.12 on 2022-03-11 15:02
+
+from django.db import migrations
+
+
+def migrate_tag(apps, schema_editor):
+    # Manual data migration, see
+    # https://docs.djangoproject.com/en/3.2/topics/migrations/#data-migrations
+    #
+    # Copy the assign_tag property to the new assign_tags set if it exists.
+    MailRule = apps.get_model("paperless_mail", "MailRule")
+    for mail_rule in MailRule.objects.all():
+        if mail_rule.assign_tag:
+            mail_rule.assign_tags.add(mail_rule.assign_tag)
+            mail_rule.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("paperless_mail", "0009_mailrule_assign_tags"),
+    ]
+
+    operations = [
+        migrations.RunPython(migrate_tag),
+    ]
diff --git a/src/paperless_mail/migrations/0011_remove_mailrule_assign_tag.py b/src/paperless_mail/migrations/0011_remove_mailrule_assign_tag.py
new file mode 100644 (file)
index 0000000..ce3c249
--- /dev/null
@@ -0,0 +1,17 @@
+# Generated by Django 3.2.12 on 2022-03-11 15:18
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("paperless_mail", "0010_auto_20220311_1602"),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name="mailrule",
+            name="assign_tag",
+        ),
+    ]
index 2c7b9fb6dc3d8b6047323e3a3aa82502c0c0b2ae..4f2038d4f114b6de6ec5d78865383cb9f16b9c00 100644 (file)
@@ -169,11 +169,10 @@ class MailRule(models.Model):
         default=TitleSource.FROM_SUBJECT,
     )
 
-    assign_tag = models.ForeignKey(
+    assign_tags = models.ManyToManyField(
         document_models.Tag,
-        null=True,
         blank=True,
-        on_delete=models.SET_NULL,
+        related_name="mail_rules_multi",
         verbose_name=_("assign this tag"),
     )