Skip to content

Commit

Permalink
disable ia matching on tender admin
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienReuiller committed Nov 28, 2024
1 parent 1f132c9 commit 3b47e43
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 86 deletions.
1 change: 1 addition & 0 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
36 changes: 0 additions & 36 deletions lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
52 changes: 2 additions & 50 deletions lemarche/tenders/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 3b47e43

Please sign in to comment.