From a6e08d3cbaad639b6f99909faf555dee23b2f41d Mon Sep 17 00:00:00 2001
From: Bryan Brancotte <bryan.brancotte@pasteur.fr>
Date: Wed, 23 Nov 2022 15:14:40 +0100
Subject: [PATCH] allows to add integer field to index, prevent substring that
 are integer to be added

---
 autocomplete_multi_models/business_process.py      |  5 +++--
 .../tests/test_business_process.py                 | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/autocomplete_multi_models/business_process.py b/autocomplete_multi_models/business_process.py
index aa00784..a2aa29b 100644
--- a/autocomplete_multi_models/business_process.py
+++ b/autocomplete_multi_models/business_process.py
@@ -80,7 +80,7 @@ def _add_instance_to_index(instance, field_names: List[str], objects: list, curs
     if f and not f():
         return
     for field_name in field_names:
-        _add_text_to_index(getattr(instance, field_name), objects, cursor)
+        _add_text_to_index(str(getattr(instance, field_name)), objects, cursor)
 
 
 def add_text_to_index(value: str):
@@ -120,9 +120,10 @@ def _purge_banned_words():
 def _add_text_to_index(value: str, objects: list, cursor):
     if value is None or value == '':
         return
+    len_value = len(value)
     for word in split_string(value):
         len_word = len(word)
-        if len_word < _AUTOCOMPLETE_MIN_LENGTH or word.isdecimal() or len_word > 64:
+        if len_word < _AUTOCOMPLETE_MIN_LENGTH or (len_word != len_value and word.isdecimal()) or len_word > 64:
             continue
         cursor.execute("SELECT UPPER(UNACCENT(%s)) as value", [word])
         ac_word = cursor.fetchone()[0]
diff --git a/autocomplete_multi_models/tests/test_business_process.py b/autocomplete_multi_models/tests/test_business_process.py
index 78f263f..c8c6eb9 100644
--- a/autocomplete_multi_models/tests/test_business_process.py
+++ b/autocomplete_multi_models/tests/test_business_process.py
@@ -135,6 +135,20 @@ class BannedWord(test_helpers.ChangeAutoCompleteSettingsTestCase):
         self.assertEqual(models.IndexedWord.objects.count(), 3)
 
 
+@override_settings(AUTOCOMPLETE_MIN_LENGTH=1)
+class SubIntNotAdded(test_helpers.ChangeAutoCompleteSettingsTestCase):
+    def test_it(self):
+        business_process.add_text_to_index("00001101 AAAA ZZZZ EEEE")
+        self.assertEqual(models.IndexedWord.objects.count(), 3)
+
+
+@override_settings(AUTOCOMPLETE_MIN_LENGTH=1)
+class CompleteIntAdded(test_helpers.ChangeAutoCompleteSettingsTestCase):
+    def test_it(self):
+        business_process.add_text_to_index("0000110")
+        self.assertEqual(models.IndexedWord.objects.count(), 1)
+
+
 @override_settings(AUTOCOMPLETE_PERSISTENT_VARIABLE_GETTER_SETTER=None)
 class NeedRebuildDefaultBehaviorTestCase(test_helpers.ChangeAutoCompleteSettingsTestCase):
     def test_it(self):
-- 
GitLab