diff --git a/src/documents/consumer.py b/src/documents/consumer.py index 5ca42813b..98fedde09 100644 --- a/src/documents/consumer.py +++ b/src/documents/consumer.py @@ -273,11 +273,7 @@ def get_tags(tags): def _store(self, text, doc): sender, title, tags, file_type = self._guess_attributes_from_name(doc) - tags = list(tags) - - lower_text = text.lower() - relevant_tags = set( - [t for t in Tag.objects.all() if t.matches(lower_text)] + tags) + relevant_tags = set(list(Tag.match_all(text)) + list(tags)) stats = os.stat(doc) diff --git a/src/documents/management/commands/document_retagger.py b/src/documents/management/commands/document_retagger.py index d7519f53b..09a3fb917 100644 --- a/src/documents/management/commands/document_retagger.py +++ b/src/documents/management/commands/document_retagger.py @@ -23,9 +23,10 @@ def handle(self, *args, **options): self.verbosity = options["verbosity"] for document in Document.objects.all(): + tags = Tag.objects.exclude( pk__in=document.tags.values_list("pk", flat=True)) - for tag in tags: - if tag.matches(document.content): - print('Tagging {} with "{}"'.format(document, tag)) - document.tags.add(tag) + + for tag in Tag.match_all(document.content, tags): + print('Tagging {} with "{}"'.format(document, tag)) + document.tags.add(tag) diff --git a/src/documents/models.py b/src/documents/models.py index 447beaa66..03758eff5 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -86,7 +86,19 @@ def conditions(self): return "{}: \"{}\" ({})".format( self.name, self.match, self.get_matching_algorithm_display()) + @classmethod + def match_all(cls, text, tags=None): + + if tags is None: + tags = cls.objects.all() + + text = text.lower() + for tag in tags: + if tag.matches(text): + yield tag + def matches(self, text): + # Check that match is not empty if self.match.strip() == "": return False