From 918ed198001361c5b235d12271ced1c1b2089615 Mon Sep 17 00:00:00 2001 From: Dimas Ciputra Date: Fri, 28 Jul 2023 09:29:02 +0700 Subject: [PATCH] Save centroid of geometry when the geometry is not a point (#3423) * Save centroid of geometry when the geometry is not a point * Fix test --- bims/models/location_site.py | 17 ++++++++++++----- .../static/js/forms/location-site-admin-form.js | 11 +++++------ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/bims/models/location_site.py b/bims/models/location_site.py index 209181d65..c5353c4af 100644 --- a/bims/models/location_site.py +++ b/bims/models/location_site.py @@ -185,7 +185,10 @@ def data_name(self): def get_centroid(self): """ Getting centroid of location site """ - if self.geometry_point: + if ( + self.geometry_point and + self.location_type.allowed_geometry == 'POINT' + ): return self.geometry_point else: if self.get_geometry(): @@ -198,10 +201,11 @@ def get_geometry(self): geometry = None validation_error = ValidationError('Only one geometry allowed.') - if self.geometry_point: - if geometry: - raise validation_error - geometry = self.geometry_point + if ( + self.geometry_point and + self.location_type.allowed_geometry == 'POINT' + ): + return self.geometry_point if self.geometry_polygon: if geometry: @@ -403,6 +407,9 @@ def save(self, *args, **kwargs): if not lon_lat_changed: self.geometry_point.x = self.longitude self.geometry_point.y = self.latitude + if self.location_type.allowed_geometry != 'POINT': + self.geometry_point = self.get_centroid() + super(LocationSite, self).save(*args, **kwargs) self.__original_centroid = self.get_centroid() else: diff --git a/bims/static/js/forms/location-site-admin-form.js b/bims/static/js/forms/location-site-admin-form.js index 357d4f7f2..7c1619080 100644 --- a/bims/static/js/forms/location-site-admin-form.js +++ b/bims/static/js/forms/location-site-admin-form.js @@ -1,10 +1,9 @@ (function ($) { $(document).ready(function () { - var $geometryInput = $('[class*="geometry_"]'); + var $geometryInput = $('[class*="geometry_"]').not(".geometry_point"); var init = true; $geometryInput.hide(); function resetAllGeometries() { - geodjango_geometry_point.clearFeatures(); geodjango_geometry_line.clearFeatures(); geodjango_geometry_polygon.clearFeatures(); geodjango_geometry_multipolygon.clearFeatures(); @@ -18,14 +17,14 @@ if ($(this).val()) { $.get("/api/location-type/" + $(this).val() + '/allowed-geometry/', function (data) { var className = 'geometry_' + data.toLowerCase(); - $geometryInput.not('.' + className).hide(); + $geometryInput.not('.' + className).not(".geometry_point").hide(); if (!$('.' + className).is(":visible")) { $('.' + className).show(); } }) - .fail(function () { - console.log('fail'); - }) + .fail(function () { + console.log('fail'); + }) } else { $geometryInput.hide(); }