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

[Dépôt de besoin] Fix du bug de périmètre et France entière #975

Merged
merged 2 commits into from
Nov 21, 2023
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
20 changes: 14 additions & 6 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def address_in_perimeter_list(self, perimeters: models.QuerySet):
conditions |= get_region_filter(perimeter)
return self.filter(conditions)

def geo_range_in_perimeter_list(self, perimeters: models.QuerySet, with_country=False):
def geo_range_in_perimeter_list(self, perimeters: models.QuerySet, with_country=False, include_country_area=False):
"""
Method to filter the Siaes depending on the perimeter filter.
We filter on the Siae's address & geo_range fields.
Expand All @@ -300,6 +300,8 @@ def geo_range_in_perimeter_list(self, perimeters: models.QuerySet, with_country=
conditions |= get_department_filter(perimeter)
if perimeter.kind == Perimeter.KIND_REGION:
conditions |= get_region_filter(perimeter)
if include_country_area:
conditions = Q(geo_range=siae_constants.GEO_RANGE_COUNTRY) | conditions
return self.filter(conditions)

def within(self, point, distance_km=0):
Expand Down Expand Up @@ -341,13 +343,19 @@ def filter_with_tender(self, tender):
if tender.sectors.count():
qs = qs.filter_sectors(tender.sectors.all())
# filter by perimeters
if tender.is_country_area:
if tender.is_country_area: # for all country
qs = qs.with_country_geo_range()
else:
if tender.perimeters.count():
qs = qs.geo_range_in_perimeter_list(tender.perimeters.all(), with_country=True)
if not tender.include_country_area:
qs = qs.exclude_country_geo_range()
if 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)
Expand Down
25 changes: 25 additions & 0 deletions lemarche/www/tenders/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,31 @@ def test_matching_siae_perimeters_custom(self):
siae_found_list = Siae.objects.filter_with_tender(tender_2)
self.assertEqual(len(siae_found_list), 2 + 1)

def test_matching_siae_country(self):
# add Siae with geo_range_country
siae_country = SiaeFactory(is_active=True, geo_range=siae_constants.GEO_RANGE_COUNTRY)
siae_country_2 = SiaeFactory(is_active=True, geo_range=siae_constants.GEO_RANGE_COUNTRY)
siae_country.sectors.add(self.sectors[0])
siae_country_2.sectors.add(self.sectors[0])
# tender perimeter custom with is_country_area = False
tender_1 = TenderFactory(sectors=self.sectors, is_country_area=True)
siae_found_list_1 = Siae.objects.filter_with_tender(tender_1)
self.assertEqual(len(siae_found_list_1), 2)
# tender perimeter custom with include_country_area = True
tender_2 = TenderFactory(sectors=self.sectors, include_country_area=True)
siae_found_list_2 = Siae.objects.filter_with_tender(tender_2)
# we should have the same length of structures
self.assertEqual(len(siae_found_list_1), len(siae_found_list_2))
# add perimeters
tender_2.perimeters.set(self.perimeters)
siae_found_list_2 = Siae.objects.filter_with_tender(tender_2)
self.assertEqual(len(siae_found_list_2), 2 + 2)
tender_2.is_country_area = True
tender_2.save()
siae_found_list_2 = Siae.objects.filter_with_tender(tender_2)
# we should have only siaes with country geo range
self.assertEqual(len(siae_found_list_2), 2 + 0)

def test_matching_siae_perimeters_custom_2(self):
# add Siae with geo_range_department (75)
siae_department = SiaeFactory(is_active=True, department="75", geo_range=siae_constants.GEO_RANGE_DEPARTMENT)
Expand Down