Merge pull request #270 from jonasc/dev

Allow setting more than one tag in mail rules
This commit is contained in:
shamoon 2022-04-13 03:11:38 -07:00 committed by GitHub
commit 95e94618d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 129 additions and 7 deletions

View File

@ -82,7 +82,7 @@ class MailRuleAdmin(admin.ModelAdmin):
), ),
"fields": ( "fields": (
"assign_title_from", "assign_title_from",
"assign_tag", "assign_tags",
"assign_document_type", "assign_document_type",
"assign_correspondent_from", "assign_correspondent_from",
"assign_correspondent", "assign_correspondent",

View File

@ -266,7 +266,7 @@ class MailAccountHandler(LoggingMixin):
) )
correspondent = self.get_correspondent(message, rule) 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 doc_type = rule.assign_document_type
processed_attachments = 0 processed_attachments = 0
@ -329,7 +329,7 @@ class MailAccountHandler(LoggingMixin):
if correspondent if correspondent
else None, else None,
override_document_type_id=doc_type.id if doc_type 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], task_name=att.filename[:100],
) )

View 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",
),
),
]

View 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),
]

View File

@ -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",
),
]

View File

@ -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"
),
),
]

View 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 = []

View File

@ -169,11 +169,9 @@ class MailRule(models.Model):
default=TitleSource.FROM_SUBJECT, default=TitleSource.FROM_SUBJECT,
) )
assign_tag = models.ForeignKey( assign_tags = models.ManyToManyField(
document_models.Tag, document_models.Tag,
null=True,
blank=True, blank=True,
on_delete=models.SET_NULL,
verbose_name=_("assign this tag"), verbose_name=_("assign this tag"),
) )

View File

@ -308,10 +308,12 @@ class TestMail(DirectoriesMixin, TestCase):
) )
account = MailAccount() account = MailAccount()
account.save()
rule = MailRule( rule = MailRule(
assign_title_from=MailRule.TitleSource.FROM_FILENAME, assign_title_from=MailRule.TitleSource.FROM_FILENAME,
account=account, account=account,
) )
rule.save()
result = self.mail_account_handler.handle_message(message, rule) result = self.mail_account_handler.handle_message(message, rule)
@ -355,10 +357,12 @@ class TestMail(DirectoriesMixin, TestCase):
) )
account = MailAccount() account = MailAccount()
account.save()
rule = MailRule( rule = MailRule(
assign_title_from=MailRule.TitleSource.FROM_FILENAME, assign_title_from=MailRule.TitleSource.FROM_FILENAME,
account=account, account=account,
) )
rule.save()
result = self.mail_account_handler.handle_message(message, rule) result = self.mail_account_handler.handle_message(message, rule)
@ -381,10 +385,12 @@ class TestMail(DirectoriesMixin, TestCase):
) )
account = MailAccount() account = MailAccount()
account.save()
rule = MailRule( rule = MailRule(
assign_title_from=MailRule.TitleSource.FROM_FILENAME, assign_title_from=MailRule.TitleSource.FROM_FILENAME,
account=account, account=account,
) )
rule.save()
result = self.mail_account_handler.handle_message(message, rule) result = self.mail_account_handler.handle_message(message, rule)
@ -406,11 +412,13 @@ class TestMail(DirectoriesMixin, TestCase):
) )
account = MailAccount() account = MailAccount()
account.save()
rule = MailRule( rule = MailRule(
assign_title_from=MailRule.TitleSource.FROM_FILENAME, assign_title_from=MailRule.TitleSource.FROM_FILENAME,
account=account, account=account,
attachment_type=MailRule.AttachmentProcessing.EVERYTHING, attachment_type=MailRule.AttachmentProcessing.EVERYTHING,
) )
rule.save()
result = self.mail_account_handler.handle_message(message, rule) result = self.mail_account_handler.handle_message(message, rule)
@ -440,12 +448,15 @@ class TestMail(DirectoriesMixin, TestCase):
for (pattern, matches) in tests: for (pattern, matches) in tests:
matches.sort() matches.sort()
self.async_task.reset_mock() self.async_task.reset_mock()
account = MailAccount() account = MailAccount(name=str(uuid.uuid4()))
account.save()
rule = MailRule( rule = MailRule(
name=str(uuid.uuid4()),
assign_title_from=MailRule.TitleSource.FROM_FILENAME, assign_title_from=MailRule.TitleSource.FROM_FILENAME,
account=account, account=account,
filter_attachment_filename=pattern, filter_attachment_filename=pattern,
) )
rule.save()
result = self.mail_account_handler.handle_message(message, rule) result = self.mail_account_handler.handle_message(message, rule)