Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept dynamic radius #180

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion osmcal/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ 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_around(self):
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")
self.assertEqual(resp.status_code, 200)

def test_location_around_dist(self):
c = Client()
resp = c.get("/events.ics?around=52,13&around_radius=5")
self.assertEqual(resp.status_code, 200)
18 changes: 14 additions & 4 deletions osmcal/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ def get_queryset(self, params, after=None):

filter_around = params.get("around", None)
if filter_around:
filter_around_radius = params.get("around_radius", None)
if filter_around_radius:
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:
if len(filter_around) == 2:
lat = filter_around[0]
lon = filter_around[1]
else:
raise BadRequest("filter_around invalid")
pt = Point(filter_around[1], filter_around[0], srid=4326)
pt = Point(lon, lat, srid=4326)
upcoming_events = upcoming_events.annotate(distance=Distance("location", pt)).filter(
distance__lte=50000
) # distance in meters
distance__lte=dist * 1000 # dist variable contains km, filter must be in meters
)

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