Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Activités des structures): Passage au matching sur les activités #1506

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 0 additions & 71 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,77 +353,6 @@ def with_in_user_favorite_list_stats(self, user):
def has_contact_email(self):
return self.exclude(contact_email__isnull=True).exclude(contact_email__exact="")

def filter_with_tender(self, tender, tendersiae_status=None): # noqa C901
"""
Filter Siaes with tenders:
- first we filter the Siae that are live + can be contacted
- then we filter on the sectors
- then we filter on the perimeters:
- if tender is made for country area, we filter with siae_geo_range=country
- else we filter on the perimeters
- then we filter on presta_type
- then we filter on kind
- finally we filter with the tendersiae_status passed as a parameter

Args:
tender (Tender): Tender used to make the matching
"""
qs = self.tender_matching_query_set()
# filter by sectors
if tender.sectors.count():
qs = qs.filter_sectors(tender.sectors.all())
# filter by perimeters
if tender.is_country_area: # for all country
qs = qs.with_country_geo_range()
else:
# filter by tender.distance_location km around the given city in location
if (
tender.location
and tender.location.kind == Perimeter.KIND_CITY
and tender.distance_location
and tender.distance_location > 0
):
qs = qs.within(tender.location.coords, tender.distance_location, tender.include_country_area)
elif tender.perimeters.count() and tender.include_country_area: # perimeters and all country
qs = qs.geo_range_in_perimeter_list(
tender.perimeters.all(), with_country=False, include_country_area=True
)
elif tender.perimeters.count(): # only perimeters
qs = qs.geo_range_in_perimeter_list(
tender.perimeters.all(), with_country=True
).exclude_country_geo_range()
elif tender.include_country_area:
qs = qs.with_country_geo_range()
# filter by presta_type
if len(tender.presta_type):
qs = qs.filter(presta_type__overlap=tender.presta_type)
# filter by siae_kind
if len(tender.siae_kind):
qs = qs.filter(kind__in=tender.siae_kind)

# tender status
if tendersiae_status == "INTERESTED":
qs = qs.filter(tendersiae__tender=tender, tendersiae__detail_contact_click_date__isnull=False)
qs = qs.order_by("-tendersiae__detail_contact_click_date")
elif tendersiae_status == "VIEWED":
qs = qs.filter(
Q(tendersiae__tender=tender)
& (
Q(tendersiae__email_link_click_date__isnull=False)
| Q(tendersiae__detail_display_date__isnull=False)
)
)
qs = qs.order_by("-tendersiae__email_link_click_date")
elif tendersiae_status == "COCONTRACTED":
qs = qs.filter(tendersiae__tender=tender, tendersiae__detail_cocontracting_click_date__isnull=False)
qs = qs.order_by("-tendersiae__detail_cocontracting_click_date")
elif tendersiae_status == "ALL":
# why need to filter more ?
qs = qs.filter(tendersiae__tender=tender, tendersiae__email_send_date__isnull=False)
qs = qs.order_by("-tendersiae__email_send_date")

return qs.distinct()

def filter_with_tender_through_activities(self, tender, tendersiae_status=None):
"""
Filter Siaes with tenders:
Expand Down
2 changes: 1 addition & 1 deletion lemarche/tenders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ def set_siae_found_list(self):
"""
Where the Tender-Siae matching magic happens!
"""
siae_found_list = Siae.objects.filter_with_tender(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:
Expand Down
23 changes: 16 additions & 7 deletions lemarche/tenders/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from lemarche.sectors.factories import SectorFactory
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.factories import SiaeFactory
from lemarche.siaes.factories import SiaeActivityFactory, SiaeFactory
from lemarche.tenders.factories import TenderFactory
from lemarche.tenders.models import TenderSiae
from lemarche.users.factories import UserFactory
Expand All @@ -22,26 +22,35 @@ def setUpTestData(cls):
cls.siae1 = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_AI,
)
siae1_activity = SiaeActivityFactory(
siae=cls.siae1,
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_COUNTRY,
with_country_perimeter=True,
)
cls.siae1.sectors.add(cls.sector)
siae1_activity.sectors.add(cls.sector)

cls.siae2 = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_AI,
)
siae2_activity = SiaeActivityFactory(
siae=cls.siae2,
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_COUNTRY,
with_country_perimeter=True,
)
cls.siae2.sectors.add(cls.sector)
siae2_activity.sectors.add(cls.sector)

cls.siae3 = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_AI,
)
siae3_activity = SiaeActivityFactory(
siae=cls.siae3,
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_COUNTRY,
with_country_perimeter=True,
)
cls.siae3.sectors.add(cls.sector)
siae3_activity.sectors.add(cls.sector)

cls.author = UserFactory(kind=User.KIND_BUYER)
cls.tender_before = TenderFactory(
Expand Down
36 changes: 25 additions & 11 deletions lemarche/tenders/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from lemarche.perimeters.models import Perimeter
from lemarche.sectors.factories import SectorFactory, SectorGroupFactory
from lemarche.siaes import constants as siae_constants
from lemarche.siaes.factories import SiaeFactory
from lemarche.siaes.factories import SiaeActivityFactory, SiaeFactory
from lemarche.siaes.models import Siae
from lemarche.tenders import constants as tender_constants
from lemarche.tenders.admin import TenderAdmin
Expand Down Expand Up @@ -194,19 +194,25 @@ def setUpTestData(cls):
cls.siae_one = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_AI,
)
siae_one_activity = SiaeActivityFactory(
siae=cls.siae_one,
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_COUNTRY,
with_country_perimeter=True,
)
cls.siae_one.sectors.add(cls.sector)
siae_one_activity.sectors.add(cls.sector)

# siae found by presta_type and semantic search
cls.siae_two = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_ESAT,
)
siae_two_activity = SiaeActivityFactory(
siae=cls.siae_two,
presta_type=[siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_COUNTRY,
with_country_perimeter=True,
)
cls.siae_two.sectors.add(cls.sector)
siae_two_activity.sectors.add(cls.sector)

# siaes found by mocked semantic search
cls.siae_three = SiaeFactory()
Expand All @@ -226,7 +232,7 @@ def test_set_siae_found_list_without_semantic_search(self):
validated_at=None,
)

siaes_found = Siae.objects.filter_with_tender(tender)
siaes_found = Siae.objects.filter_with_tender_through_activities(tender)
tender.set_siae_found_list()
tender.refresh_from_db()
self.assertEqual(list(siaes_found), list(tender.siaes.all()))
Expand All @@ -246,7 +252,7 @@ def test_set_siae_found_list_with_semantic_search(self):
validated_at=None,
)

siaes_found = Siae.objects.filter_with_tender(tender)
siaes_found = Siae.objects.filter_with_tender_through_activities(tender)
tender.set_siae_found_list()
tender.refresh_from_db()

Expand Down Expand Up @@ -925,22 +931,30 @@ def setUp(cls):
siae_one = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_AI,
coords=coords_paris,
)
siae_one_activity = SiaeActivityFactory(
siae=siae_one,
presta_type=[siae_constants.PRESTA_PREST, siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_CUSTOM,
coords=coords_paris,
geo_range_custom_distance=100,
)

siae_two = SiaeFactory(
is_active=True,
kind=siae_constants.KIND_ESAT,
coords=coords_paris,
)
siae_two_activity = SiaeActivityFactory(
siae=siae_two,
presta_type=[siae_constants.PRESTA_BUILD],
geo_range=siae_constants.GEO_RANGE_CUSTOM,
coords=coords_paris,
geo_range_custom_distance=10,
)

for i in range(5):
siae_one.sectors.add(cls.sectors[i])
siae_two.sectors.add(cls.sectors[i + 5])
siae_one_activity.sectors.add(cls.sectors[i])
siae_two_activity.sectors.add(cls.sectors[i + 5])

cls.tender = TenderFactory(
sectors=cls.sectors[6:8],
Expand Down
2 changes: 1 addition & 1 deletion lemarche/www/siaes/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def filter_queryset(self, qs=None): # noqa C901
tender = self.cleaned_data.get("tender", None)
if tender:
tendersiae_status = self.cleaned_data.get("tendersiae_status", "ALL")
qs = qs.filter_with_tender(tender=tender, tendersiae_status=tendersiae_status)
qs = qs.filter_with_tender_through_activities(tender=tender, tendersiae_status=tendersiae_status)

locations = self.cleaned_data.get("locations", None)
if locations:
Expand Down
Loading
Loading