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

[feat] add support for dist parameter on DistanceToPointOrderingFilter #274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ contexttimer
# QA checks
openwisp-utils[qa]~=0.7.0
packaging~=20.4
coreapi==2.3.3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coreapi is planned to be deprecated

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@auvipy great point, thanks for pointing that out! Do you have suggestions about the best way to handle this?

coreschema==0.0.4
16 changes: 16 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
autopep8==1.6.0
certifi==2021.10.8
distlib==0.3.4
filelock==3.6.0
packaging==21.3
platformdirs==2.5.1
pluggy==1.0.0
py==1.11.0
pycodestyle==2.8.0
pyparsing==3.0.7
six==1.16.0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why six?

toml==0.10.2
tox==3.24.5
virtualenv==20.13.4
coreapi==2.3.3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coreapi is scheduled to be deprecated

coreschema==0.0.4
70 changes: 67 additions & 3 deletions rest_framework_gis/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from math import cos, pi
import coreapi
import coreschema

from django.contrib.gis import forms
from django.contrib.gis.db import models
Expand Down Expand Up @@ -287,13 +289,73 @@ def filter_queryset(self, request, queryset, view):

order = request.query_params.get(self.order_param)
if order == 'desc':
return queryset.order_by(-GeometryDistance(filter_field, point))
queryset = queryset.order_by(-GeometryDistance(filter_field, point))
else:
return queryset.order_by(GeometryDistance(filter_field, point))
queryset = queryset.order_by(GeometryDistance(filter_field, point))

# distance in meters
dist_string = request.query_params.get(self.dist_param, 1000)
try:
dist = float(dist_string)
except ValueError:
raise ParseError(
"Invalid distance string supplied for parameter {}".format(
self.dist_param,
),
)

convert_distance_input = getattr(view, "distance_filter_convert_meters", False)
if convert_distance_input:
# Warning: assumes that the point is (lon,lat)
dist = self.dist_to_deg(dist, point[1])
geoDjango_filter = "dwithin" # use dwithin for points
return queryset.filter(
Q(**{f"{filter_field}__{geoDjango_filter}": (point, dist)}),
)

def get_schema_fields(self, view):
params = super().get_schema_fields(view)
params.extend(
[
coreapi.Field(
name=self.point_param,
required=False,
location="query",
schema=coreschema.Array(
title="point parameter title",
description="Point represented in **longitude,latitude** format.",
items={"type": "float"},
min_items=2,
max_items=2,
),
),
coreapi.Field(
name=self.dist_param,
required=False,
location="query",
schema=coreschema.Number(
title="distance parameter",
description="Distance from the point (in meters) to limit the search area.",
),
),
coreapi.Field(
name=self.order_param,
required=False,
location="query",
schema=coreschema.Enum(
enum=("asc", "desc"),
title="order direction",
description="The direction to sort results when comparing distance.",
),
),
],
)

return params

def get_schema_operation_parameters(self, view):
params = super().get_schema_operation_parameters(view)
params.append(
params.extend([
{
"name": self.order_param,
"required": False,
Expand All @@ -307,4 +369,6 @@ def get_schema_operation_parameters(self, view):
"style": "form",
"explode": False,
}
]
)
return params
12 changes: 11 additions & 1 deletion tests/django_restframework_gis_tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ def test_DistanceToPointFilter_filtering(self):
def test_DistanceToPointOrderingFilter_filtering(self):
"""
Checks that the DistanceOrderingFilter returns the objects in the correct order
given the geometry defined by the URL parameters
given the geometry defined by the URL parameters. Also checks that the
DistanceToPointOrderingFilter can accept a dist parameter
"""
self.assertEqual(Location.objects.count(), 0)

Expand Down Expand Up @@ -431,6 +432,15 @@ def test_DistanceToPointOrderingFilter_filtering(self):
],
)

# Test dist parameter
distance = 5
url_params = '?point=%i,%i&dist=%i&format=json' % (point[0], point[1], distance)
response = self.client.get(
'%s%s' % (self.location_order_distance_to_point, url_params)
)
self.assertEqual(len(response.data['features']), 1)
self.assertEqual(response.data['features'][0]['properties']['name'], 'Chicago')

@skipIf(
has_spatialite,
'Skipped test for spatialite backend: missing feature "contains_properly"',
Expand Down
53 changes: 53 additions & 0 deletions tests/django_restframework_gis_tests/test_schema_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
GeojsonLocationContainedInBBoxList,
GeojsonLocationContainedInTileList,
GeojsonLocationWithinDistanceOfPointList,
GeojsonLocationOrderDistanceToPointList,
ModelViewWithPolygon,
geojson_location_list,
)
Expand Down Expand Up @@ -663,3 +664,55 @@ def test_geometry_field(self):
"required": ["random_field1", "random_field2", "polygon"],
},
)

def test_distance_to_point_ordering_filter(self):
path = "/"
method = "GET"
view = create_view(
GeojsonLocationOrderDistanceToPointList, "GET", create_request("/")
)
inspector = GeoFeatureAutoSchema()
inspector.view = view
generated_schema = inspector.get_filter_parameters(path, method)
self.assertListEqual(
generated_schema,
[
{
"name": "dist",
"required": False,
"in": "query",
"schema": {"type": "number", "format": "float", "default": 1000},
"description": "Represents **Distance** in **Distance to point** filter. "
"Default value is used only if ***point*** is passed.",
},
{
"name": "point",
"required": False,
"in": "query",
"description": "Point represented in **x,y** format. "
"Represents **point** in **Distance to point filter**",
"schema": {
"type": "array",
"items": {"type": "float"},
"minItems": 2,
"maxItems": 2,
"example": [0, 10],
},
"style": "form",
"explode": False,
},
{
"name": "order",
"required": False,
"in": "query",
"description": "",
"schema": {
"type": "enum",
"items": {"type": "string", "enum": ["asc", "desc"]},
"example": "desc",
},
"style": "form",
"explode": False,
}
],
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ deps =
djangorestframework312: djangorestframework~=3.12.0
djangorestframework313: djangorestframework~=3.13.0
-rrequirements-test.txt
-rrequirements.txt
pytest: pytest
pytest: pytest-django