From 7665d64979ccd94a599412e032d5004625ff0a91 Mon Sep 17 00:00:00 2001 From: "madjid.asa" Date: Mon, 13 Nov 2023 17:12:01 +0100 Subject: [PATCH] fix country area and perimeters to filter siae with tender --- lemarche/siaes/models.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/lemarche/siaes/models.py b/lemarche/siaes/models.py index d36d7394c..a9d6bc51c 100644 --- a/lemarche/siaes/models.py +++ b/lemarche/siaes/models.py @@ -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. @@ -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): @@ -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)