From e9cc941add88e0eefbd57a1bfcdf9e42f382c1a7 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Tue, 26 Nov 2024 12:57:46 -0500 Subject: [PATCH] Allowed location excludes invalid child locations --- location/models.py | 1 + location/tests/test.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/location/models.py b/location/models.py index a4ebfaf..d66716b 100644 --- a/location/models.py +++ b/location/models.py @@ -105,6 +105,7 @@ def allowed(self, user_id, loc_types=["R", "D", "W", "V"], strict=True, qs=False "tblLocations" child INNER JOIN CTE_PARENTS leaf ON child."ParentLocationId" = leaf."LocationId" + WHERE child."ValidityTo" is NULL ) SELECT DISTINCT "LocationId" FROM CTE_PARENTS WHERE "LocationType" in ('{"','".join(loc_types)}') """ diff --git a/location/tests/test.py b/location/tests/test.py index 8b80e30..f1fdef7 100644 --- a/location/tests/test.py +++ b/location/tests/test.py @@ -121,6 +121,18 @@ def test_allowed_location(self): districts = UserDistrict.get_user_districts(self.test_user) self.assertIsNotNone(districts) + def test_allowed_location_excludes_invalid(self): + invalid_village = create_test_village({'name': 'Invalid Vilalge', 'code': 'IV2020'}) + invalid_village.validity_to = '2020-02-20' + invalid_village.parent = self.test_village.parent + invalid_village.save() + + allowed = LocationManager().allowed( + self.test_user._u.id, loc_types=["V"] + ) + self.assertEqual(len(allowed), 1) + self.assertEqual(allowed.first().id, self.test_village.id) + def test_cache_invalidation(self): LocationManager().is_allowed(self.test_user, []) cached = caches["location"].get(f"user_locations_{self.test_user._u.id}")