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

[geo] Restrict to points only #151 #175

Open
wants to merge 1 commit 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
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)