From 3b47e4333ff74eb7295c95e5c7c5fc4f58741bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Reuiller?= Date: Thu, 28 Nov 2024 17:17:06 +0100 Subject: [PATCH] disable ia matching on tender admin --- lemarche/tenders/admin.py | 1 + lemarche/tenders/models.py | 36 ------------------- lemarche/tenders/tests/test_models.py | 52 ++------------------------- 3 files changed, 3 insertions(+), 86 deletions(-) diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index 9b518ef71..b103d3f6b 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -345,6 +345,7 @@ class TenderAdmin(FieldsetsInlineMixin, admin.ModelAdmin): "extra_data_display", "import_raw_object_display", "logs_display", + "with_ai_matching", ] formfield_overrides = { models.TextField: {"widget": CKEditorWidget}, diff --git a/lemarche/tenders/models.py b/lemarche/tenders/models.py index 582364653..d9ce7eb0b 100644 --- a/lemarche/tenders/models.py +++ b/lemarche/tenders/models.py @@ -36,7 +36,6 @@ from lemarche.tenders.enums import SurveyDoesNotExistQuestionChoices, SurveyScaleQuestionChoices from lemarche.tenders.utils import find_amount_ranges from lemarche.users.models import User -from lemarche.utils.apis import api_elasticsearch from lemarche.utils.constants import ( ADMIN_FIELD_HELP_TEXT, AUTO_FIELD_HELP_TEXT, @@ -748,41 +747,6 @@ def set_siae_found_list(self): siae_found_list = Siae.objects.filter_with_tender_through_activities(self) self.siaes.set(siae_found_list, clear=False) - if self.with_ai_matching and self.validated_at is None: - if ( - self.location - and self.location.kind == Perimeter.KIND_CITY - and self.distance_location - and self.distance_location > 0 - ): - # with geo distance - siae_ids = api_elasticsearch.siaes_similarity_search_with_geo_distance( - self.description, - geo_distance=self.distance_location, - geo_lat=self.location.coords.y, - geo_lon=self.location.coords.x, - siae_kinds=self.siae_kind, - ) - else: - siae_ids = api_elasticsearch.siaes_similarity_search(self.description, siae_kinds=self.siae_kind) - - siaes_had_found_by_ia = Siae.objects.filter(id__in=siae_ids) - siaes_had_found_by_ia_too = [] - for siae in siaes_had_found_by_ia: - if siae not in siae_found_list: - self.siaes.add( - siae, - through_defaults={ - "source": tender_constants.TENDER_SIAE_SOURCE_AI, - "found_with_ai": True, - }, - ) - else: - siaes_had_found_by_ia_too.append(siae) - - # keep the info that the AI also found those siaes - TenderSiae.objects.filter(tender_id=self.id, siae__in=siaes_had_found_by_ia_too).update(found_with_ai=True) - def save(self, *args, **kwargs): """ - update the "last_updated" fields diff --git a/lemarche/tenders/tests/test_models.py b/lemarche/tenders/tests/test_models.py index 0ff4d5f93..21e182eb8 100644 --- a/lemarche/tenders/tests/test_models.py +++ b/lemarche/tenders/tests/test_models.py @@ -202,7 +202,7 @@ def setUpTestData(cls): ) siae_one_activity.sectors.add(cls.sector) - # siae found by presta_type and semantic search + # siae found by presta_type cls.siae_two = SiaeFactory( is_active=True, kind=siae_constants.KIND_ESAT, @@ -214,14 +214,10 @@ def setUpTestData(cls): ) siae_two_activity.sectors.add(cls.sector) - # siaes found by mocked semantic search - cls.siae_three = SiaeFactory() - cls.siae_four = SiaeFactory() - # siae not found cls.siae_five = SiaeFactory() - def test_set_siae_found_list_without_semantic_search(self): + def test_set_siae_found_list(self): with mock.patch( "lemarche.tenders.models.api_elasticsearch.siaes_similarity_search" ) as mock_siaes_similarity_search: @@ -239,50 +235,6 @@ def test_set_siae_found_list_without_semantic_search(self): mock_siaes_similarity_search.assert_not_called() - def test_set_siae_found_list_with_semantic_search(self): - with mock.patch( - "lemarche.tenders.models.api_elasticsearch.siaes_similarity_search" - ) as mock_siaes_similarity_search: - mock_siaes_similarity_search.return_value = [self.siae_two.pk, self.siae_three.pk, self.siae_four.pk] - tender = TenderFactory( - presta_type=[siae_constants.PRESTA_BUILD], - sectors=[self.sector], - is_country_area=True, - with_ai_matching=True, - validated_at=None, - ) - - siaes_found = Siae.objects.filter_with_tender_through_activities(tender) - tender.set_siae_found_list() - tender.refresh_from_db() - - self.assertEqual(tender.siaes.count(), 4) - for siae in list(siaes_found) + [self.siae_three, self.siae_four]: - with self.subTest(siae=siae): - self.assertIn(siae, tender.siaes.all()) - - # test found_with_ai field value - self.assertEqual(TenderSiae.objects.get(tender=tender, siae=self.siae_one).found_with_ai, False) - tender_siae_two = TenderSiae.objects.get(tender=tender, siae=self.siae_two) - self.assertEqual(tender_siae_two.found_with_ai, True) - for siae in [self.siae_two, self.siae_three, self.siae_four]: - with self.subTest(siae=siae): - self.assertEqual(TenderSiae.objects.get(tender=tender, siae=siae).found_with_ai, True) - - # test source - for siae in [self.siae_one, self.siae_two]: - with self.subTest(siae=siae): - self.assertEqual( - TenderSiae.objects.get(tender=tender, siae=siae).source, - tender_constants.TENDER_SIAE_SOURCE_EMAIL, - ) - for siae in [self.siae_three, self.siae_four]: - with self.subTest(siae=siae): - self.assertEqual( - TenderSiae.objects.get(tender=tender, siae=siae).source, tender_constants.TENDER_SIAE_SOURCE_AI - ) - mock_siaes_similarity_search.assert_called_once() - class TenderModelQuerysetTest(TestCase): @classmethod