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

Protect against related field issue #28

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 3 additions & 2 deletions gisserver/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __init__(
self.model_field = None
self.parent = parent
self.abstract = abstract

self.related_field = False
# Allow to override the class attribute on 'self',
# which avoids having to subclass this field class as well.
if xsd_class is not None:
Expand Down Expand Up @@ -252,6 +252,7 @@ def bind(
)

field = field.related_model._meta.get_field(name)
self.related_field = True
self.model_field = field
else:
self.model_field = self.model._meta.get_field(self.name)
Expand Down Expand Up @@ -593,7 +594,7 @@ def _local_model_field_names(self) -> list[str]:
return [
(ff.model_field.name if ff.model_field else ff.model_attribute)
for ff in self.fields
if not ff.model_field.many_to_many and not ff.model_field.one_to_many
if not ff.model_field.many_to_many and not ff.model_field.one_to_many and not ff.related_field
]

@cached_property
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def python_coordinates(db_coordinates):

@pytest.fixture()
def city() -> City:
return City.objects.create(name="CloudCity")
return City.objects.create(name="CloudCity", region="OurRegion")


@pytest.fixture()
Expand Down
4 changes: 3 additions & 1 deletion tests/gisserver/views/test_describefeaturetype.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def test_describe_flattened(self, client):
<element name="name" type="string" minOccurs="0" />
<element name="city-id" type="integer" minOccurs="0" nillable="true" />
<element name="city-name" type="string" minOccurs="0" nillable="true" />
<element name="city-region" type="string" minOccurs="0" nillable="true" />
<element name="location" type="gml:PointPropertyType" minOccurs="0" maxOccurs="1" nillable="true" />
<element name="rating" type="double" minOccurs="0" />
<element name="is_open" type="boolean" minOccurs="0" />
Expand All @@ -205,7 +206,8 @@ def test_describe_outputformat(self, client):
"""Test workaround for FME's outputformat."""
gml32 = urllib.parse.quote("application/gml+xml; version=3.2")
response = client.get(
f"/v1/wfs/?SERVICE=WFS&REQUEST=DescribeFeatureType&VERSION=2.0.0&TYPENAMES=restaurant&outputformat={gml32}"
f"/v1/wfs/?SERVICE=WFS&REQUEST=DescribeFeatureType&"
f"VERSION=2.0.0&TYPENAMES=restaurant&outputformat={gml32}"
)
content = response.content.decode()
assert response.status_code == 200, content
Expand Down
15 changes: 14 additions & 1 deletion tests/gisserver/views/test_getcapabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def test_get(self, client, restaurant, coordinates):
"""Prove that the happy flow works"""
gml32 = quote_plus("application/gml+xml; version=3.2")
response = client.get(
f"/v1/wfs/?SERVICE=WFS&REQUEST=GetCapabilities&ACCEPTVERSIONS=2.0.0&OUTPUTFORMAT={gml32}"
f"/v1/wfs/?SERVICE=WFS&REQUEST=GetCapabilities"
f"&ACCEPTVERSIONS=2.0.0&OUTPUTFORMAT={gml32}"
)
content = response.content.decode()
assert response["content-type"] == "text/xml; charset=utf-8", content
Expand Down Expand Up @@ -160,3 +161,15 @@ def test_get_invalid_version(self, client):
xml_doc = validate_xsd(response.content, WFS_20_XSD)
exception = xml_doc.find("ows:Exception", NAMESPACES)
assert exception.attrib["exceptionCode"] == "VersionNegotiationFailed"

def test_get_flattened(self, client, restaurant, coordinates):
gml32 = quote_plus("application/gml+xml; version=3.2")
response = client.get(
f"/v1/wfs-flattened/?SERVICE=WFS&REQUEST=GetCapabilities&"
f"ACCEPTVERSIONS=2.0.0&OUTPUTFORMAT={gml32}"
)
content = response.content.decode()
assert response["content-type"] == "text/xml; charset=utf-8", content
assert response.status_code == 200, content
assert "<ows:OperationsMetadata>" in content
assert '<ows:WGS84BoundingBox dimensions="2">' in content
10 changes: 7 additions & 3 deletions tests/gisserver/views/test_getfeature.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def test_get_flattened(self, client, restaurant, bad_restaurant, coordinates):
<app:name>Café Noir</app:name>
<app:city-id>{restaurant.city_id}</app:city-id>
<app:city-name>CloudCity</app:city-name>
<app:city-region>OurRegion</app:city-region>
<app:location>
<gml:Point gml:id="restaurant.{restaurant.id}.1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:pos srsDimension="2">{coordinates.point1_xml_wgs84}</gml:pos>
Expand All @@ -360,6 +361,7 @@ def test_get_flattened(self, client, restaurant, bad_restaurant, coordinates):
<app:name>Foo Bar</app:name>
<app:city-id xsi:nil="true" />
<app:city-name xsi:nil="true" />
<app:city-region xsi:nil="true" />
<app:location>
<gml:Point gml:id="restaurant.{bad_restaurant.id}.1" srsName="urn:ogc:def:crs:EPSG::4326">
<gml:pos srsDimension="2">{coordinates.point2_xml_wgs84}</gml:pos>
Expand Down Expand Up @@ -977,6 +979,7 @@ def test_get_geojson_flattened(
# City is flattened, following the type definition
"city-id": restaurant.city_id,
"city-name": "CloudCity",
"city-region": "OurRegion",
"rating": 5.0,
"is_open": True,
"created": "2020-04-05T12:11:10+00:00",
Expand All @@ -996,6 +999,7 @@ def test_get_geojson_flattened(
"name": "Foo Bar",
"city-id": None,
"city-name": None,
"city-region": None,
"rating": 1.0,
"is_open": False,
"created": "2020-04-05T20:11:10+00:00",
Expand Down Expand Up @@ -1079,9 +1083,9 @@ def test_get_csv_flattened(
assert response.status_code == 200, content

expect = f"""
"id","name","city-id","city-name","location","rating","is_open","created"
"{restaurant.id}","Café Noir","{restaurant.city_id}","CloudCity","{coordinates.point1_ewkt}","5.0","True","2020-04-05 12:11:10+00:00"
"{bad_restaurant.id}","Foo Bar","","","{coordinates.point2_ewkt}","1.0","False","2020-04-05 20:11:10+00:00"
"id","name","city-id","city-name","city-region","location","rating","is_open","created"
"{restaurant.id}","Café Noir","{restaurant.city_id}","CloudCity","OurRegion","{coordinates.point1_ewkt}","5.0","True","2020-04-05 12:11:10+00:00"
"{bad_restaurant.id}","Foo Bar","","","","{coordinates.point2_ewkt}","1.0","False","2020-04-05 20:11:10+00:00"
""".lstrip() # noqa: E501
assert content == expect
assert "SRID=4326;" in content
Expand Down
1 change: 1 addition & 0 deletions tests/test_gisserver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def current_datetime():

class City(models.Model):
name = models.CharField(max_length=200)
region = models.CharField(max_length=200)

def __str__(self):
return self.name
Expand Down
1 change: 1 addition & 0 deletions tests/test_gisserver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class FlattenedWFSView(PlacesWFSView):
"name",
field("city-id", model_attribute="city_id"),
field("city-name", model_attribute="city.name"),
field("city-region", model_attribute="city.region"),
"location",
"rating",
"is_open",
Expand Down
Loading