mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	Merge pull request #270 from jonasc/dev
Allow setting more than one tag in mail rules
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",
 | 
			
		||||
 
 | 
			
		||||
@@ -266,7 +266,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
 | 
			
		||||
@@ -329,7 +329,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",
 | 
			
		||||
            ),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										40
									
								
								src/paperless_mail/migrations/0010_auto_20220311_1602.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/paperless_mail/migrations/0010_auto_20220311_1602.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
# Generated by Django 3.2.12 on 2022-03-11 15:02
 | 
			
		||||
 | 
			
		||||
from django.db import migrations
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def migrate_tag_to_tags(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()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def migrate_tags_to_tag(apps, schema_editor):
 | 
			
		||||
    # Manual data migration, see
 | 
			
		||||
    # https://docs.djangoproject.com/en/3.2/topics/migrations/#data-migrations
 | 
			
		||||
    #
 | 
			
		||||
    # Copy the unique value in the assign_tags set to the old assign_tag property.
 | 
			
		||||
    # Do nothing if the tag is not unique.
 | 
			
		||||
    MailRule = apps.get_model("paperless_mail", "MailRule")
 | 
			
		||||
    for mail_rule in MailRule.objects.all():
 | 
			
		||||
        tags = mail_rule.assign_tags.all()
 | 
			
		||||
        if len(tags) == 1:
 | 
			
		||||
            mail_rule.assign_tag = tags[0]
 | 
			
		||||
            mail_rule.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ("paperless_mail", "0009_mailrule_assign_tags"),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.RunPython(migrate_tag_to_tags, migrate_tags_to_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",
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
@@ -0,0 +1,20 @@
 | 
			
		||||
# Generated by Django 3.2.12 on 2022-03-11 16:21
 | 
			
		||||
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ("paperless_mail", "0011_remove_mailrule_assign_tag"),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.AlterField(
 | 
			
		||||
            model_name="mailrule",
 | 
			
		||||
            name="assign_tags",
 | 
			
		||||
            field=models.ManyToManyField(
 | 
			
		||||
                blank=True, to="documents.Tag", verbose_name="assign this tag"
 | 
			
		||||
            ),
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										13
									
								
								src/paperless_mail/migrations/0013_merge_20220412_1051.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/paperless_mail/migrations/0013_merge_20220412_1051.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
# Generated by Django 4.0.4 on 2022-04-12 08:51
 | 
			
		||||
 | 
			
		||||
from django.db import migrations
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ("paperless_mail", "0009_alter_mailrule_action_alter_mailrule_folder"),
 | 
			
		||||
        ("paperless_mail", "0012_alter_mailrule_assign_tags"),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = []
 | 
			
		||||
@@ -169,11 +169,9 @@ 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,
 | 
			
		||||
        verbose_name=_("assign this tag"),
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -308,10 +308,12 @@ class TestMail(DirectoriesMixin, TestCase):
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        account = MailAccount()
 | 
			
		||||
        account.save()
 | 
			
		||||
        rule = MailRule(
 | 
			
		||||
            assign_title_from=MailRule.TitleSource.FROM_FILENAME,
 | 
			
		||||
            account=account,
 | 
			
		||||
        )
 | 
			
		||||
        rule.save()
 | 
			
		||||
 | 
			
		||||
        result = self.mail_account_handler.handle_message(message, rule)
 | 
			
		||||
 | 
			
		||||
@@ -355,10 +357,12 @@ class TestMail(DirectoriesMixin, TestCase):
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        account = MailAccount()
 | 
			
		||||
        account.save()
 | 
			
		||||
        rule = MailRule(
 | 
			
		||||
            assign_title_from=MailRule.TitleSource.FROM_FILENAME,
 | 
			
		||||
            account=account,
 | 
			
		||||
        )
 | 
			
		||||
        rule.save()
 | 
			
		||||
 | 
			
		||||
        result = self.mail_account_handler.handle_message(message, rule)
 | 
			
		||||
 | 
			
		||||
@@ -381,10 +385,12 @@ class TestMail(DirectoriesMixin, TestCase):
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        account = MailAccount()
 | 
			
		||||
        account.save()
 | 
			
		||||
        rule = MailRule(
 | 
			
		||||
            assign_title_from=MailRule.TitleSource.FROM_FILENAME,
 | 
			
		||||
            account=account,
 | 
			
		||||
        )
 | 
			
		||||
        rule.save()
 | 
			
		||||
 | 
			
		||||
        result = self.mail_account_handler.handle_message(message, rule)
 | 
			
		||||
 | 
			
		||||
@@ -406,11 +412,13 @@ class TestMail(DirectoriesMixin, TestCase):
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        account = MailAccount()
 | 
			
		||||
        account.save()
 | 
			
		||||
        rule = MailRule(
 | 
			
		||||
            assign_title_from=MailRule.TitleSource.FROM_FILENAME,
 | 
			
		||||
            account=account,
 | 
			
		||||
            attachment_type=MailRule.AttachmentProcessing.EVERYTHING,
 | 
			
		||||
        )
 | 
			
		||||
        rule.save()
 | 
			
		||||
 | 
			
		||||
        result = self.mail_account_handler.handle_message(message, rule)
 | 
			
		||||
 | 
			
		||||
@@ -440,12 +448,15 @@ class TestMail(DirectoriesMixin, TestCase):
 | 
			
		||||
        for (pattern, matches) in tests:
 | 
			
		||||
            matches.sort()
 | 
			
		||||
            self.async_task.reset_mock()
 | 
			
		||||
            account = MailAccount()
 | 
			
		||||
            account = MailAccount(name=str(uuid.uuid4()))
 | 
			
		||||
            account.save()
 | 
			
		||||
            rule = MailRule(
 | 
			
		||||
                name=str(uuid.uuid4()),
 | 
			
		||||
                assign_title_from=MailRule.TitleSource.FROM_FILENAME,
 | 
			
		||||
                account=account,
 | 
			
		||||
                filter_attachment_filename=pattern,
 | 
			
		||||
            )
 | 
			
		||||
            rule.save()
 | 
			
		||||
 | 
			
		||||
            result = self.mail_account_handler.handle_message(message, rule)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user