Skip to content

Commit

Permalink
Update radius handling
Browse files Browse the repository at this point in the history
Handle radius to contain km only, multiply by 1000 when setting filter.
Update tests
  • Loading branch information
kmpoppe committed Sep 24, 2024
1 parent 83519ef commit fa804a7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions osmcal/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ def test_location_out_of_range(self):
resp = c.get("/events.ics?around=5564") # The around parameter obviously doesn't make any sense.
self.assertEqual(resp.status_code, 400)

def test_location_radius_out_of_range(self):
c = Client()
resp = c.get("/events.ics?around=52,13&around_radius=260") # The around radius parameter is too large.
self.assertEqual(resp.status_code, 400)

def test_location_around_50k(self):
c = Client()
resp = c.get("/events.ics?around=52,13")
Expand Down
9 changes: 5 additions & 4 deletions osmcal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ def get_queryset(self, params, after=None):
if filter_around:
filter_around_radius = params.get("around_radius", None)
if filter_around_radius:
dist = filter_around_radius * 1000
else
dist = 50000
dist = float(filter_around_radius)
if dist == 0 or dist > 250: raise BadRequest("filter_around_radius invalid")
else:
dist = 50
filter_around = [float(x) for x in filter_around.split(",")]
if len(filter_around) == 2:
lat = filter_around[0]
Expand All @@ -81,7 +82,7 @@ def get_queryset(self, params, after=None):
upcoming_events = upcoming_events.annotate(
distance = Distance("location", pt)
).filter(
distance__lte = dist # distance in meters
distance__lte = dist * 1000 # dist variable contains km, filter must be in meters
)

days = params.get("days", None)
Expand Down

0 comments on commit fa804a7

Please sign in to comment.