diff options
Diffstat (limited to 'ishtar_common')
-rw-r--r-- | ishtar_common/models_common.py | 13 | ||||
-rw-r--r-- | ishtar_common/tests.py | 52 |
2 files changed, 56 insertions, 9 deletions
diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index adf1c1516..0c763965b 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -2626,15 +2626,10 @@ def geodata_attached_remove(model, instance, pk_set=None, clear=False): return # use a cache to manage during geodata attach - if not hasattr(instance, "_geodata"): - instance._geodata = [] if instance.main_geodata_id in item_pks: instance.main_geodata_id = None instance.skip_history_when_saving = True instance._no_move = True - if not hasattr(instance, "_geodata"): - instance._geodata = [] - instance._geodata += [pk for pk in item_pks if pk not in instance._geodata] instance.save() # for all sub item verify that the geo items are present @@ -2658,6 +2653,9 @@ def geodata_attached_remove(model, instance, pk_set=None, clear=False): def geodata_attached_changed(sender, **kwargs): # manage main geoitem and cascade association + profile = get_current_profile() + if not profile.mapping: + return instance = kwargs.get("instance", None) model = kwargs.get("model", None) pk_set = kwargs.get("pk_set", None) @@ -2711,15 +2709,14 @@ class GeographicItem(models.Model): using=using, update_fields=update_fields, ) - if not hasattr(self, "_geodata"): # use a cache to manage during geodata attach - self._geodata = [] if self.main_geodata and not self.geodata.filter(pk=self.main_geodata.pk) and\ self.main_geodata.pk not in self._geodata: self.geodata.add(self.main_geodata) - self._geodata.append(self.main_geodata.pk) elif not self.main_geodata and self.geodata.count(): # arbitrary associate the first to geodata self.main_geodata = self.geodata.order_by("pk").all()[0] + self.skip_history_when_saving = True + self._no_move = True self.save() @property diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 4156f9f0f..cfd3544dd 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -3323,6 +3323,12 @@ class GeoVectorTest(TestCase): # accounts self.username, self.password, self.user = create_superuser() + profile, created = models.IshtarSiteProfile.objects.get_or_create( + slug="default", active=True + ) + profile.mapping = True + profile.save() + # operation, context record, find operation_type = models.OperationType.objects.get(txt_idx="arch_diagnostic") data = { @@ -3479,7 +3485,51 @@ class GeoVectorTest(TestCase): self.assertEqual(self.base_find.main_geodata, geo_vector_find) self.assertEqual(self.base_find.geodata.count(), 1) - # test town add + def test_geotown_add(self): + town = models.Town.objects.create(name="Town") + self.assertIsNone(town.main_geodata) + self.assertEqual(town.geodata.count(), 0) + + data_type = models.GeoDataType.objects.get( + txt_idx="town-limit", + ) + town_ct = ContentType.objects.get( + model="town", + app_label="ishtar_common", + ).pk + multi_polygon = MultiPolygon( + Polygon(((1, 1), (5, 1), (5, 5), (1, 1))), + Polygon(((2, 2), (2, 3), (3, 3), (2, 2))), + srid=4326 + ) + geo_vector_town = models.GeoVectorData.objects.create( + source_content_type_id=town_ct, + source_id=town.id, + name="Test town geo", + origin=self.origin, + data_type=data_type, + provider=self.provider, + comment="This is a comment.", + multi_polygon=multi_polygon + ) + town.geodata.add(geo_vector_town) + town = models.Town.objects.get(pk=town.pk) + self.assertEqual(town.geodata.count(), 1) + self.assertEqual(town.main_geodata, geo_vector_town) + + self.operation.towns.add(town) + self._reinit_objects() + # save need to be forced... keep it? + self.operation.save() + + self._reinit_objects() + self.assertEqual(self.operation.geodata.count(), 1) + self.assertEqual(self.operation.main_geodata, geo_vector_town) + self.assertEqual(self.context_record.geodata.count(), 1) + self.assertEqual(self.context_record.main_geodata, geo_vector_town) + self.assertEqual(self.base_find.geodata.count(), 1) + self.assertEqual(self.base_find.main_geodata, geo_vector_town) + # test town remove |