diff --git a/requirements.txt b/requirements.txt index 666a37365..5354268cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,9 @@ python-dotenv>=0.3.0 python-gnupg>=0.3.8 pytz>=2015.7 gunicorn==19.6.0 + +# For the tests +pytest +pytest-django +pytest-sugar +tox diff --git a/src/documents/migrations/0015_add_insensitive_to_match.py b/src/documents/migrations/0015_add_insensitive_to_match.py new file mode 100644 index 000000000..34a570c6e --- /dev/null +++ b/src/documents/migrations/0015_add_insensitive_to_match.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.2 on 2016-10-05 21:38 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('documents', '0014_document_checksum'), + ] + + operations = [ + migrations.AddField( + model_name='correspondent', + name='is_insensitive', + field=models.BooleanField(default=True), + ), + migrations.AddField( + model_name='tag', + name='is_insensitive', + field=models.BooleanField(default=True), + ), + ] diff --git a/src/documents/models.py b/src/documents/models.py index d4659c57e..56c330f75 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -47,6 +47,8 @@ class MatchingModel(models.Model): ) ) + is_insensitive = models.BooleanField(default=True) + class Meta(object): abstract = True @@ -71,27 +73,36 @@ class MatchingModel(models.Model): def matches(self, text): + search_kwargs = {} + # Check that match is not empty if self.match.strip() == "": return False + if self.is_insensitive: + search_kwargs = {"flags": re.IGNORECASE} + if self.matching_algorithm == self.MATCH_ALL: for word in self.match.split(" "): - if not re.search(r"\b{}\b".format(word), text): + search_result = re.search( + r"\b{}\b".format(word), text, **search_kwargs) + if not search_result: return False return True if self.matching_algorithm == self.MATCH_ANY: for word in self.match.split(" "): - if re.search(r"\b{}\b".format(word), text): + if re.search(r"\b{}\b".format(word), text, **search_kwargs): return True return False if self.matching_algorithm == self.MATCH_LITERAL: - return bool(re.search(r"\b{}\b".format(self.match), text)) + return bool(re.search( + r"\b{}\b".format(self.match), text, **search_kwargs)) if self.matching_algorithm == self.MATCH_REGEX: - return bool(re.search(re.compile(self.match), text)) + return bool(re.search( + re.compile(self.match, **search_kwargs), text)) raise NotImplementedError("Unsupported matching algorithm")