Skip to content

Commit

Permalink
Merge pull request #152 from weilu/bug/allowed-invalid
Browse files Browse the repository at this point in the history
Allowed location excludes invalid child locations
  • Loading branch information
delcroip authored Nov 27, 2024
2 parents b3203db + e9cc941 commit a0257ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions location/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')
"""
Expand Down
12 changes: 12 additions & 0 deletions location/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit a0257ff

Please sign in to comment.