Skip to content

Commit

Permalink
[geo] Restrict to points only #151
Browse files Browse the repository at this point in the history
Fixed the issue by adding a clean method
to the location model. This method
ensures that when the geometry is not
None, it should be an instance of a Point
Geometry else a ValidationError is raised

Fixes #151
  • Loading branch information
NoumbissiValere committed Mar 24, 2020
1 parent 11ca7f7 commit 78a49f6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions openwisp_controller/geo/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from django.contrib.gis.db import models
from django.contrib.gis.geos import Point
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from django_loci.base.models import AbstractFloorPlan, AbstractLocation, AbstractObjectLocation

from openwisp_users.mixins import OrgMixin, ValidateOrgMixin
Expand All @@ -8,6 +11,11 @@ class Location(OrgMixin, AbstractLocation):
class Meta(AbstractLocation.Meta):
abstract = False

def clean(self):
if self.geometry is not None and not isinstance(self.geometry, Point):
raise ValidationError({'geometry': _('Only point geometry is allowed')})
super().clean()


class FloorPlan(OrgMixin, AbstractFloorPlan):
location = models.ForeignKey(Location, models.CASCADE)
Expand Down
19 changes: 19 additions & 0 deletions openwisp_controller/geo/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.gis.geos import LineString, Point, Polygon
from django.core.exceptions import ValidationError
from django.test import TestCase
from django_loci.tests.base.test_models import BaseTestModels
Expand All @@ -23,3 +24,21 @@ def test_floorplan_location_validation(self):
self.assertIn('location', e.message_dict)
else:
self.fail('ValidationError not raised')

def test_add_location_with_point_geometry(self):
self._create_location(geometry=Point(0, 0, srid=4326), name='point')
obj = self.location_model.objects.get(name='point')
self.assertEqual(obj.name, 'point')

def test_add_location_with_line_geometry(self):
with self.assertRaisesMessage(ValidationError, 'Only point geometry is allowed'):
self._create_location(geometry=LineString((0, 0), (1, 1), srid=4326), name='line')
obj = self.location_model.objects.filter(name='line')
self.assertEqual(obj.count(), 0)

def tes_add_location_with_polygon_geometry(self):
with self.assertRaisesMessage(ValidationError, 'Only point geometry is allowed'):
poly = Polygon((0, 0), (0, 1), (1, 1), (1, 0), (0, 0), srid=4326)
self._create_location(geometry=poly, name='poly')
obj = self.location_model.objects.filter(name='poly')
self.assertEqual(obj.count(), 0)

0 comments on commit 78a49f6

Please sign in to comment.