Skip to content

Commit

Permalink
refactor(Périmètres): Pouvoir aussi filtrer les post_codes sur le ins…
Browse files Browse the repository at this point in the history
…ee_code (#1269)
  • Loading branch information
raphodn authored Jun 18, 2024
1 parent 608a2b8 commit f697a9b
Showing 2 changed files with 8 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lemarche/perimeters/models.py
Original file line number Diff line number Diff line change
@@ -27,12 +27,13 @@ def name_search(self, value):
.order_by("-similarity")
)

def post_code_search(self, value):
def post_code_search(self, value, include_insee_code=False):
# city post_code
if len(value) == 5:
qs = self.filter(post_codes__contains=[value])
# if we wanted to allow search on insee_code as well
# return queryset.filter(Q(insee_code=value) | Q(post_codes__contains=[value]))
if include_insee_code:
qs = self.filter(Q(insee_code=value) | Q(post_codes__contains=[value]))
else:
qs = self.filter(post_codes__contains=[value])
# department code or beginning of city post_code
elif len(value) == 2:
qs = self.filter(Q(insee_code=value) | Q(post_codes__0__startswith=value))
3 changes: 3 additions & 0 deletions lemarche/perimeters/tests.py
Original file line number Diff line number Diff line change
@@ -88,3 +88,6 @@ def test_post_code_search(self):
self.assertEqual(qs.first(), self.perimeter_city)
qs = Perimeter.objects.post_code_search("38")
self.assertEqual(qs.count(), 2)
qs = Perimeter.objects.post_code_search("38185", include_insee_code=True)
self.assertEqual(qs.count(), 1)
self.assertEqual(qs.first(), self.perimeter_city)

0 comments on commit f697a9b

Please sign in to comment.