Skip to content

Commit

Permalink
[feature] Allow geometry-less models #282
Browse files Browse the repository at this point in the history
fixes #282
  • Loading branch information
3nids committed Oct 6, 2023
1 parent f743b1c commit 3903921
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 231 deletions.
32 changes: 32 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
repos:
# Remove unused imports/variables
- repo: https://github.com/myint/autoflake
rev: v2.2.0
hooks:
- id: autoflake
args:
- "--in-place"
- "--remove-all-unused-imports"
- "--remove-unused-variables"

- repo: https://github.com/pycqa/flake8
rev: "6.1.0"
hooks:
- id: flake8
exclude: |
./tests/.*settings.*.py
docs/.*
args:
- '--max-line-length=110'
- '--ignore=W605,W503,W504'

- repo: https://github.com/pycqa/isort
rev: "5.12.0"
hooks:
- id: isort
args:
- 'multi_line_output=3'
- 'use_parentheses=True'
- 'include_trailing_comma=True'
- 'force_grid_wrap=0'
- 'line_length=88'
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ to be serialized as the "geometry". For example:
# as with a ModelSerializer.
fields = ('id', 'address', 'city', 'state')
If your model is geometry-less, you can set ``geo_field`` to ``None``
and a null geometry will be produced.

Using GeometrySerializerMethodField as "geo_field"
##################################################

Expand Down
2 changes: 1 addition & 1 deletion rest_framework_gis/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def to_internal_value(self, value):
value = json.dumps(value)
try:
return GEOSGeometry(value)
except (GEOSException):
except GEOSException:
raise ValidationError(
_(
'Invalid format: string or unicode input unrecognized as GeoJSON, WKT EWKT or HEXEWKB.'
Expand Down
223 changes: 0 additions & 223 deletions rest_framework_gis/serializers.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

dependencies = [
('django_restframework_gis_tests', '0001_initial'),
]
Expand Down
7 changes: 3 additions & 4 deletions tests/django_restframework_gis_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class BaseModel(models.Model):
name = models.CharField(max_length=32)
slug = models.SlugField(max_length=128, unique=True, blank=True)
timestamp = models.DateTimeField(null=True, blank=True)
geometry = models.GeometryField()

class Meta:
abstract = True
Expand All @@ -45,14 +44,14 @@ def save(self, *args, **kwargs):


class Location(BaseModel):
pass
geometry = models.GeometryField()


class LocatedFile(BaseModel):
class LocatedFile(Location):
file = models.FileField(upload_to='located_files', blank=True, null=True)


class BoxedLocation(BaseModel):
class BoxedLocation(Location):
bbox_geometry = models.PolygonField()


Expand Down
7 changes: 7 additions & 0 deletions tests/django_restframework_gis_tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ class Meta:
fields = ['name', 'slug', 'id']


class NoGeoFeatureMethodSerializer(gis_serializers.GeoFeatureModelSerializer):
class Meta:
model = Location
geo_field = None
fields = ['name', 'slug', 'id']


class PointSerializer(gis_serializers.GeoFeatureModelSerializer):
class Meta:
model = PointModel
Expand Down
10 changes: 10 additions & 0 deletions tests/django_restframework_gis_tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,16 @@ def test_geometry_serializer_method_field_none(self):
self.assertEqual(response.data['properties']['name'], 'None value')
self.assertEqual(response.data['geometry'], None)

def test_geometry_serializer_method_field_nogeo(self):
location = Location.objects.create(name='No geometry value')
location_loaded = Location.objects.get(pk=location.id)
self.assertEqual(location_loaded.name, "No geometry value")
url = reverse('api_geojson_location_details_nogeo', args=[location.id])
response = self.client.generic('GET', url, content_type='application/json')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['properties']['name'], 'No geometry value')
self.assertEqual(response.data['geometry'], None)

def test_nullable_empty_geometry(self):
empty = Nullable(name='empty', geometry='POINT EMPTY')
empty.full_clean()
Expand Down
10 changes: 10 additions & 0 deletions tests/django_restframework_gis_tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
LocationGeoFeatureSlugSerializer,
LocationGeoFeatureWritableIdSerializer,
LocationGeoSerializer,
NoGeoFeatureMethodSerializer,
NoneGeoFeatureMethodSerializer,
PaginatedLocationGeoSerializer,
PolygonModelSerializer,
Expand Down Expand Up @@ -167,6 +168,15 @@ class GeojsonLocationDetailsNone(generics.RetrieveUpdateDestroyAPIView):
geojson_location_details_none = GeojsonLocationDetailsNone.as_view()


class GeojsonLocationDetailsNoGeo(generics.RetrieveUpdateDestroyAPIView):
model = Location
serializer_class = NoGeoFeatureMethodSerializer
queryset = Location.objects.all()


geojson_location_details_nogeo = GeojsonLocationDetailsNoGeo.as_view()


class GeojsonLocationSlugDetails(generics.RetrieveUpdateDestroyAPIView):
model = Location
lookup_field = 'slug'
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@

# local settings must be imported before test runner otherwise they'll be ignored
try:
from local_settings import *
pass
except ImportError:
pass

0 comments on commit 3903921

Please sign in to comment.