From 4f14e0f425957d52ecb213880f37691d575e4165 Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Sat, 12 Dec 2020 01:19:22 +0100 Subject: [PATCH] fixes #125 --- src/documents/signals/handlers.py | 12 ++++++++---- src/documents/tests/test_management_retagger.py | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 4fbbe8f8a..27aa37908 100755 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -7,6 +7,7 @@ from django.contrib.admin.models import ADDITION, LogEntry from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.db import models, DatabaseError +from django.db.models import Q from django.dispatch import receiver from django.utils import timezone from filelock import FileLock @@ -121,11 +122,14 @@ def set_tags(sender, classifier=None, replace=False, **kwargs): + if replace: - document.tags.clear() - current_tags = set([]) - else: - current_tags = set(document.tags.all()) + document.tags.exclude( + Q(is_inbox_tag=True) | + (Q(match="") & ~Q(matching_algorithm=Tag.MATCH_AUTO)) + ).delete() + + current_tags = set(document.tags.all()) matched_tags = matching.match_tags(document.content, classifier) diff --git a/src/documents/tests/test_management_retagger.py b/src/documents/tests/test_management_retagger.py index 2346b6527..2397b0cc8 100644 --- a/src/documents/tests/test_management_retagger.py +++ b/src/documents/tests/test_management_retagger.py @@ -14,6 +14,11 @@ class TestRetagger(DirectoriesMixin, TestCase): self.tag_first = Tag.objects.create(name="tag1", match="first", matching_algorithm=Tag.MATCH_ANY) self.tag_second = Tag.objects.create(name="tag2", match="second", matching_algorithm=Tag.MATCH_ANY) + self.tag_inbox = Tag.objects.create(name="test", is_inbox_tag=True) + self.tag_no_match = Tag.objects.create(name="test2") + + self.d3.tags.add(self.tag_inbox) + self.d3.tags.add(self.tag_no_match) self.correspondent_first = Correspondent.objects.create( name="c1", match="first", matching_algorithm=Correspondent.MATCH_ANY) @@ -38,7 +43,7 @@ class TestRetagger(DirectoriesMixin, TestCase): self.assertEqual(d_first.tags.count(), 1) self.assertEqual(d_second.tags.count(), 1) - self.assertEqual(d_unrelated.tags.count(), 0) + self.assertEqual(d_unrelated.tags.count(), 2) self.assertEqual(d_first.tags.first(), self.tag_first) self.assertEqual(d_second.tags.first(), self.tag_second) @@ -56,3 +61,10 @@ class TestRetagger(DirectoriesMixin, TestCase): self.assertEqual(d_first.correspondent, self.correspondent_first) self.assertEqual(d_second.correspondent, self.correspondent_second) + + def test_force_preserve_inbox(self): + call_command('document_retagger', '--tags', '--overwrite') + + d_first, d_second, d_unrelated = self.get_updated_docs() + + self.assertCountEqual([tag.id for tag in d_unrelated.tags.all()], [self.tag_inbox.id, self.tag_no_match.id])