-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add openapi schema generation for filters * improve schema generation for filters * add code to generate schema from serializer * rename some variables * override schema of pagination * handle GeometrySerializerMethodField * fix invalid schema for id field * add test models * add test serializer * add initial test case for schema generation * add model migration * rename files * fix handling of GeometrySerializerMethodField * handle DRF version >= 3.12 * add DEFAULT_SCHEMA_CLASS * upgrade djangorestframework to 3.11 * fix _map_serializer * revert DRF version to 3.10 * fix schema for polygon * fix enum for polygon * add test cases for polygon * remove min items and change example generation logic * add test cases for multi polygon * add test case for multi line string model * add test case for multi point model * add test cases for pagination schema * add test cases for filter schemas * do not handle source attribute * add missing dependency * add number as type of array member for bbox * add test case fox auto bbox * fix bbox_geo_field handling * add test case for schema generation of bbox_geo_field * add support for DRF 3.12 * change DRF version to 3.12 * fix schema generation for 3.12 * remove support for DRF < 3.12 * fix support for distance to point filter * fix linting issues * add name to AUTHORS * fix linting issues * fix linting issues using black * [fix] Conflicts between black and flake8 * [fix] Postgresql Setup * [fix] revert unnecessary changes made by black * [fix] Wrong call to _map_serializer * [change] Update documentation * add packaging dependency * fix has_geometry_distance * run schema generation tests only if DRF >= 3.12 * remove unnecessary check for DRF < 3.9 * [fix] Fix tox.ini * [fix] Fix .travis.yml * [fix] Fix linting issues * [fix] revert unnecessary changes. * [fix] Linting issues * [fix] Support for Django 2.1 * [fix] travis env * [fix] Fix tox.ini * [Merge] Merge remote-tracking branch 'upstream/master' * [ci] Fix .travis.yml * [code] Add support for GeometryField and GeometryCollectionField * [docs] improve docs Co-authored-by: Federico Capoano <federico.capoano@gmail.com> * [ci] Remove support for Django 2.1 and support for Python 3.9 * [lint] Fix linting issues Co-authored-by: Federico Capoano <federico.capoano@gmail.com>
- Loading branch information
1 parent
496d958
commit 4be23ef
Showing
14 changed files
with
1,229 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ django-filter>=2.0 | |
contexttimer | ||
# QA checks | ||
openwisp-utils[qa]~=0.6 | ||
packaging~=20.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import warnings | ||
|
||
from django.contrib.gis.db import models | ||
from rest_framework.schemas.openapi import AutoSchema | ||
from rest_framework.utils import model_meta | ||
|
||
from rest_framework_gis.fields import GeometrySerializerMethodField | ||
from rest_framework_gis.serializers import ( | ||
GeoFeatureModelListSerializer, | ||
GeoFeatureModelSerializer, | ||
) | ||
|
||
|
||
class GeoFeatureAutoSchema(AutoSchema): | ||
COORDINATES_SCHEMA_FOR_POINT = { | ||
"type": "array", | ||
"items": {"type": "number", "format": "float"}, | ||
"example": [12.9721, 77.5933], | ||
"minItems": 2, | ||
"maxItems": 3, | ||
} | ||
|
||
COORDINATES_SCHEMA_FOR_LINE_STRING = { | ||
"type": "array", | ||
"items": COORDINATES_SCHEMA_FOR_POINT, | ||
"example": [[22.4707, 70.0577], [12.9721, 77.5933]], | ||
"minItems": 2, | ||
} | ||
|
||
GEO_FIELD_TO_SCHEMA = { | ||
models.PointField: { | ||
"type": {"type": "string", "enum": ["Point"]}, | ||
"coordinates": COORDINATES_SCHEMA_FOR_POINT, | ||
}, | ||
models.LineStringField: { | ||
"type": {"type": "string", "enum": ["LineString"]}, | ||
"coordinates": COORDINATES_SCHEMA_FOR_LINE_STRING, | ||
}, | ||
models.PolygonField: { | ||
"type": {"type": "string", "enum": ["Polygon"]}, | ||
"coordinates": { | ||
"type": "array", | ||
"items": {**COORDINATES_SCHEMA_FOR_LINE_STRING, "minItems": 4}, | ||
"example": [ | ||
[0.0, 0.0], | ||
[0.0, 50.0], | ||
[50.0, 50.0], | ||
[50.0, 0.0], | ||
[0.0, 0.0], | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
GEO_FIELD_TO_SCHEMA[models.GeometryField] = { | ||
'type': {'type': 'string'}, | ||
'coordinates': { | ||
'oneOf': [ # If you have custom subclass of GeometryField, Override `oneOf` property. | ||
GEO_FIELD_TO_SCHEMA[models.PointField], | ||
GEO_FIELD_TO_SCHEMA[models.LineStringField], | ||
GEO_FIELD_TO_SCHEMA[models.PolygonField], | ||
], | ||
'example': GEO_FIELD_TO_SCHEMA[models.PolygonField]['coordinates'][ | ||
'example' | ||
], | ||
}, | ||
} | ||
|
||
MULTI_FIELD_MAPPING = { | ||
models.PointField: models.MultiPointField, | ||
models.LineStringField: models.MultiLineStringField, | ||
models.PolygonField: models.MultiPolygonField, | ||
models.GeometryField: models.GeometryCollectionField, | ||
} | ||
|
||
for singular_field, multi_field in MULTI_FIELD_MAPPING.items(): | ||
GEO_FIELD_TO_SCHEMA[multi_field] = { | ||
"type": {"type": "string", "enum": [multi_field.geom_class.__name__]}, | ||
"coordinates": { | ||
"type": "array", | ||
"items": GEO_FIELD_TO_SCHEMA[singular_field]["coordinates"], | ||
"example": [ | ||
GEO_FIELD_TO_SCHEMA[singular_field]["coordinates"]["example"] | ||
], | ||
}, | ||
} | ||
|
||
def _map_geo_field(self, serializer, geo_field_name): | ||
field = serializer.fields[geo_field_name] | ||
if isinstance(field, GeometrySerializerMethodField): | ||
warnings.warn( | ||
"Geometry generation for GeometrySerializerMethodField is not supported." | ||
) | ||
return {} | ||
|
||
model_field_name = geo_field_name | ||
|
||
geo_field = model_meta.get_field_info(serializer.Meta.model).fields[ | ||
model_field_name | ||
] | ||
try: | ||
return self.GEO_FIELD_TO_SCHEMA[geo_field.__class__] | ||
except KeyError: | ||
warnings.warn( | ||
"Geometry generation for {field} is not supported.".format(field=field) | ||
) | ||
return {} | ||
|
||
def map_field(self, field): | ||
if isinstance(field, GeoFeatureModelListSerializer): | ||
return self._map_geo_feature_model_list_serializer(field) | ||
|
||
return super().map_field(field) | ||
|
||
def _map_geo_feature_model_list_serializer(self, serializer): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"type": {"type": "string", "enum": ["FeatureCollection"]}, | ||
"features": { | ||
"type": "array", | ||
"items": self.map_serializer(serializer.child), | ||
}, | ||
}, | ||
} | ||
|
||
def _map_geo_feature_model_serializer(self, serializer): | ||
schema = super().map_serializer(serializer) | ||
|
||
geo_json_schema = { | ||
"type": "object", | ||
"properties": {"type": {"type": "string", "enum": ["Feature"]}}, | ||
} | ||
|
||
if serializer.Meta.id_field: | ||
geo_json_schema["properties"]["id"] = schema["properties"].pop( | ||
serializer.Meta.id_field | ||
) | ||
|
||
geo_field = serializer.Meta.geo_field | ||
geo_json_schema["properties"]["geometry"] = { | ||
"type": "object", | ||
"properties": self._map_geo_field(serializer, geo_field), | ||
} | ||
schema["properties"].pop(geo_field) | ||
|
||
if serializer.Meta.auto_bbox or serializer.Meta.bbox_geo_field: | ||
geo_json_schema["properties"]["bbox"] = { | ||
"type": "array", | ||
"items": {"type": "number"}, | ||
"minItems": 4, | ||
"maxItems": 4, | ||
"example": [12.9721, 77.5933, 12.9721, 77.5933], | ||
} | ||
if serializer.Meta.bbox_geo_field in schema["properties"]: | ||
schema["properties"].pop(serializer.Meta.bbox_geo_field) | ||
|
||
geo_json_schema["properties"]["properties"] = schema | ||
|
||
return geo_json_schema | ||
|
||
def map_serializer(self, serializer): | ||
if isinstance(serializer, GeoFeatureModelListSerializer): | ||
return self._map_geo_feature_model_list_serializer(serializer) | ||
|
||
if isinstance(serializer, GeoFeatureModelSerializer): | ||
return self._map_geo_feature_model_serializer(serializer) | ||
|
||
return super().map_serializer(serializer) |
Oops, something went wrong.