From 2c8e2661810d26264466fd9924787b7ce92f7fa0 Mon Sep 17 00:00:00 2001 From: Cku Date: Wed, 5 Oct 2016 23:43:55 +0200 Subject: [PATCH 1/3] Allow to create case sensitive matches --- .../0015_add_insensitive_to_match.py | 25 +++++++++++++++++++ src/documents/models.py | 15 ++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 src/documents/migrations/0015_add_insensitive_to_match.py 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..c65e5f351 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,32 @@ 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): + if not re.search(r"\b{}\b".format(word), text, **search_kwargs): 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), text, **search_kwargs)) raise NotImplementedError("Unsupported matching algorithm") From 875ad09b000b624fc33d1716a2a8304a597b42a4 Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Wed, 26 Oct 2016 10:08:04 +0000 Subject: [PATCH 2/3] Fixt a ValueError in .matches() + pep8 --- src/documents/models.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/documents/models.py b/src/documents/models.py index c65e5f351..56c330f75 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -84,7 +84,9 @@ class MatchingModel(models.Model): if self.matching_algorithm == self.MATCH_ALL: for word in self.match.split(" "): - if not re.search(r"\b{}\b".format(word), text, **search_kwargs): + search_result = re.search( + r"\b{}\b".format(word), text, **search_kwargs) + if not search_result: return False return True @@ -95,10 +97,12 @@ class MatchingModel(models.Model): return False if self.matching_algorithm == self.MATCH_LITERAL: - return bool(re.search(r"\b{}\b".format(self.match), text, **search_kwargs)) + 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, **search_kwargs)) + return bool(re.search( + re.compile(self.match, **search_kwargs), text)) raise NotImplementedError("Unsupported matching algorithm") From a5f10cd6b3fd6ff5ef9969abb872658a39ea41db Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Wed, 26 Oct 2016 10:08:21 +0000 Subject: [PATCH 3/3] Added the testing requirements --- requirements.txt | 6 ++++++ 1 file changed, 6 insertions(+) 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