Skip to content

Commit

Permalink
N'afficher que les départements relatifs à la région sélectionnée, si…
Browse files Browse the repository at this point in the history
… pertinent #502
  • Loading branch information
tut-tuuut authored Jan 19, 2022
2 parents 95bc8a6 + a0a60fa commit 63fb5cd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
24 changes: 19 additions & 5 deletions aidants_connect/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,28 @@ class DepartmentFilter(SimpleListFilter):
filter_parameter_name = "zipcode"

@classmethod
def generate_filter_list(cls):
def generate_filter_list(cls, region=None):
if not region:
return [
(d.normalize_zipcode(), f"{d.dep_name} ({d.normalize_zipcode()})")
for d in DatavizDepartment.objects.all().order_by("dep_name")
] + [("other", "Autre")]
return [
(d.normalize_zipcode(), f"{d.dep_name} ({d.normalize_zipcode()})")
for d in DatavizDepartment.objects.all().order_by("dep_name")
] + [("other", "Autre")]
(
d2r.department.normalize_zipcode(),
f"{d2r.department.dep_name} ({d2r.department.normalize_zipcode()})",
)
for d2r in DatavizDepartmentsToRegion.objects.filter(
region=region
).order_by("department__dep_name")
]

def lookups(self, request, model_admin):
return self.generate_filter_list()
region = None
region_id = request.GET.get("region", "other")
if region_id != "other":
region = DatavizRegion.objects.get(id=region_id)
return self.generate_filter_list(region=region)

def queryset(self, request, queryset):
department_value = self.value()
Expand Down
26 changes: 20 additions & 6 deletions aidants_connect_web/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import tag, TestCase
from django.test.client import RequestFactory

from aidants_connect.admin import DepartmentFilter, RegionFilter
from aidants_connect_web.admin import OrganisationAdmin
Expand All @@ -8,39 +9,52 @@

@tag("admin")
class DepartmentFilterTests(TestCase):
@classmethod
def setUpTestData(cls):
cls.rf = RequestFactory()

def test_generate_filter_list(self):
result = DepartmentFilter.generate_filter_list()
self.assertEqual(len(result), 102)
self.assertEqual(("01", "Ain (01)"), result[0])
self.assertEqual(("20", "Corse-du-Sud (20)"), result[19])

def test_lookup(self):
dep_filter = DepartmentFilter(None, {}, Organisation, OrganisationAdmin)
self.assertEqual(len(dep_filter.lookups(None, {})), 102)
request = self.rf.get("/")
dep_filter = DepartmentFilter(request, {}, Organisation, OrganisationAdmin)
self.assertEqual(len(dep_filter.lookups(request, {})), 102)

def test_lookups_with_selected_region(self):
params = {"region": "6"}
request = self.rf.get("/", params)
dep_filter = DepartmentFilter(request, params, Organisation, OrganisationAdmin)
self.assertEqual(len(dep_filter.lookups(request, {})), 10)

def test_queryset(self):
OrganisationFactory(zipcode="13013")
OrganisationFactory(zipcode="13013")
OrganisationFactory(zipcode="20000")
OrganisationFactory(zipcode="0")
corse_filter = DepartmentFilter(
None, {"department": "20"}, Organisation, OrganisationAdmin
self.rf.get("/"), {"department": "20"}, Organisation, OrganisationAdmin
)
queryset_corse = corse_filter.queryset(None, Organisation.objects.all())
self.assertEqual(1, queryset_corse.count())
self.assertEqual("20000", queryset_corse[0].zipcode)

bdc_filter = DepartmentFilter(
None, {"department": "13"}, Organisation, OrganisationAdmin
self.rf.get("/"), {"department": "13"}, Organisation, OrganisationAdmin
)
queryset_bdc = bdc_filter.queryset(None, Organisation.objects.all())
self.assertEqual(2, queryset_bdc.count())
self.assertEqual("13013", queryset_bdc[0].zipcode)

other_filter = DepartmentFilter(
None, {"department": "other"}, Organisation, OrganisationAdmin
self.rf.get("/"), {"department": "other"}, Organisation, OrganisationAdmin
)
queryset_other = other_filter.queryset(
self.rf.get("/"), Organisation.objects.all()
)
queryset_other = other_filter.queryset(None, Organisation.objects.all())
self.assertEqual(1, queryset_other.count())
self.assertEqual("0", queryset_other[0].zipcode)

Expand Down

0 comments on commit 63fb5cd

Please sign in to comment.