From 9b872f5e30b66698f0ff2f76104714094d30519a Mon Sep 17 00:00:00 2001 From: Lindsey Jacks Date: Mon, 20 Jun 2016 13:07:38 -0400 Subject: [PATCH] Fixes #213: Points are now centered on the map. added logic into model to check if project extent is within the map bounds added bounding box for project_add_extents map --- cadasta/core/static/js/map_utils.js | 1 + cadasta/organization/models.py | 30 +++++++++++++++++++ cadasta/organization/tests/test_models.py | 13 ++++++++ .../organization/project_add_extents.html | 3 +- functional_tests/pages/ProjectAdd.py | 2 -- 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/cadasta/core/static/js/map_utils.js b/cadasta/core/static/js/map_utils.js index d0c86e0c3..de3ee274b 100644 --- a/cadasta/core/static/js/map_utils.js +++ b/cadasta/core/static/js/map_utils.js @@ -34,4 +34,5 @@ function add_map_controls(map) { }); map.addControl(new Geolocate()); + return map } diff --git a/cadasta/organization/models.py b/cadasta/organization/models.py index 21e18acd2..e55ec86fa 100644 --- a/cadasta/organization/models.py +++ b/cadasta/organization/models.py @@ -234,6 +234,36 @@ def public(self): return self.access == 'public' +def reassign_project_extent(instance): + if instance.extent: + points = [list(x) for x in list(instance.extent.boundary.coords)] + extent_visible = True + for point in points: + if point[0] < -180 or point[0] > 180: + extent_visible = False + break + if extent_visible: + return + extent = '((' + for i, point in enumerate(points): + while point[0] < -180: + point[0] += 360 + while point[0] > 180: + point[0] -= 360 + extent += ' '.join([str(point[0]), str(point[1])]) + if i != len(points) - 1: + extent += ',' + polygon = str(instance.extent).split(' ')[0] + new_extent = ('{} {}))'.format(polygon, extent)) + instance.extent = new_extent + + +@receiver(models.signals.pre_save, sender=Project) +def check_extent(sender, instance, **kwargs): + if instance.extent: + reassign_project_extent(instance) + + class ProjectRole(RandomIDModel): project = models.ForeignKey(Project) user = models.ForeignKey('accounts.User') diff --git a/cadasta/organization/tests/test_models.py b/cadasta/organization/tests/test_models.py index 17fd56d56..419e5af68 100644 --- a/cadasta/organization/tests/test_models.py +++ b/cadasta/organization/tests/test_models.py @@ -114,6 +114,19 @@ def test_country_assignment_for_invalid_geometry(self): ) assert project.country == '' + def test_reassign_extent(self): + project = ProjectFactory.create( + extent='SRID=4326;POLYGON((' + '211.36667 47.25000, ' + '211.41667 47.25000, ' + '211.41667 47.28333, ' + '211.36667 47.28333, ' + '211.36667 47.25000))' + ) + assert project.extent.boundary.coords == ( + (-148.63333, 47.25), (-148.58333, 47.25), (-148.58333, 47.28333), + (-148.63333, 47.28333), (-148.63333, 47.25)) + def test_defaults_to_public(self): project = ProjectFactory.create() assert project.public() diff --git a/cadasta/templates/organization/project_add_extents.html b/cadasta/templates/organization/project_add_extents.html index be6f8cf09..1e1923faa 100644 --- a/cadasta/templates/organization/project_add_extents.html +++ b/cadasta/templates/organization/project_add_extents.html @@ -16,7 +16,8 @@ diff --git a/functional_tests/pages/ProjectAdd.py b/functional_tests/pages/ProjectAdd.py index b6fe06822..34511ccee 100644 --- a/functional_tests/pages/ProjectAdd.py +++ b/functional_tests/pages/ProjectAdd.py @@ -92,8 +92,6 @@ def set_geometry(self, country): zoom_in = self.BY_CLASS('leaflet-control-zoom-in') zoom_in.click() # Zoom 1 time.sleep(0.5) # Give zoom animation time to settle - zoom_in.click() # Zoom 2 - time.sleep(0.5) # Give zoom animation time to settle if country == 'AU': self.draw_rectangle(840, 210, 20, 20)