Skip to content

Commit

Permalink
Accept 3 around parts for dynamic radius
Browse files Browse the repository at this point in the history
  • Loading branch information
kmpoppe committed Sep 19, 2024
1 parent 83d1f76 commit 859c58c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
15 changes: 13 additions & 2 deletions osmcal/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@


class EventListTest(TestCase):
def test_location_out_of_range(self):
def test_location_out_of_range_low(self):
# Based on Sentry report OSM-CALENDAR-1W
c = Client()
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_around(self):
def test_location_out_of_range_high(self):
# Based on Sentry report OSM-CALENDAR-1W
c = Client()
resp = c.get("/events.ics?around=55,13,5,1") # The around parameter obviously doesn't make any sense.
self.assertEqual(resp.status_code, 400)

def test_location_around_50k(self):
c = Client()
resp = c.get("/events.ics?around=52,13")
self.assertEqual(resp.status_code, 200)

def test_location_around_dist(self):
c = Client()
resp = c.get("/events.ics?around=52,13,5")
self.assertEqual(resp.status_code, 200)
20 changes: 15 additions & 5 deletions osmcal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,22 @@ def get_queryset(self, params, after=None):
filter_around = params.get("around", None)
if filter_around:
filter_around = [float(x) for x in filter_around.split(",")]
if len(filter_around) < 2:
if len(filter_around) == 2:
lat = filter_around[0]
lon = filter_around[1]
dist = 50000
elif len(filter_around) == 3:
lat = filter_around[0]
lon = filter_around[1]
dist = filter_around[2] * 1000
else:
raise BadRequest("filter_around invalid")
pt = Point(filter_around[1], filter_around[0], srid=4326)
upcoming_events = upcoming_events.annotate(distance=Distance("location", pt)).filter(
distance__lte=50000
) # distance in meters
pt = Point(lon, lat, srid = 4326)
upcoming_events = upcoming_events.annotate(
distance = Distance("location", pt)
).filter(
distance__lte = dist # distance in meters
)

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

0 comments on commit 859c58c

Please sign in to comment.