mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Allow setting more than one tag in mail rules
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.
This commit is contained in:
		@@ -82,7 +82,7 @@ class MailRuleAdmin(admin.ModelAdmin):
 | 
			
		||||
                ),
 | 
			
		||||
                "fields": (
 | 
			
		||||
                    "assign_title_from",
 | 
			
		||||
                    "assign_tag",
 | 
			
		||||
                    "assign_tags",
 | 
			
		||||
                    "assign_document_type",
 | 
			
		||||
                    "assign_correspondent_from",
 | 
			
		||||
                    "assign_correspondent",
 | 
			
		||||
 
 | 
			
		||||
@@ -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],
 | 
			
		||||
                )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								src/paperless_mail/migrations/0009_mailrule_assign_tags.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/paperless_mail/migrations/0009_mailrule_assign_tags.py
									
									
									
									
									
										Normal file
									
								
							@@ -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",
 | 
			
		||||
            ),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										26
									
								
								src/paperless_mail/migrations/0010_auto_20220311_1602.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								src/paperless_mail/migrations/0010_auto_20220311_1602.py
									
									
									
									
									
										Normal file
									
								
							@@ -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),
 | 
			
		||||
    ]
 | 
			
		||||
@@ -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",
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
@@ -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"),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user