Merge branch 'CkuT-case_sensitive_matches'

This commit is contained in:
Daniel Quinn 2016-10-26 10:08:42 +00:00
commit 6d310f006c
3 changed files with 46 additions and 4 deletions

@ -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

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

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