summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-02-23 10:57:48 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:21:00 +0100
commit55f311f240402612feb72766925131e585040833 (patch)
treebd23b4e1c01800deabf75dfd1a82b532ea150da1
parentef1d582c2e55bd12a5d2169ecb445b280f0afe9c (diff)
downloadIshtar-55f311f240402612feb72766925131e585040833.tar.bz2
Ishtar-55f311f240402612feb72766925131e585040833.zip
Geodata redesign: fix operation migration
-rw-r--r--archaeological_operations/models.py22
-rw-r--r--ishtar_common/management/commands/migrate_to_geo_v4.py14
-rw-r--r--ishtar_common/models.py4
-rw-r--r--ishtar_common/utils.py24
4 files changed, 48 insertions, 16 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 8fa72b423..40ee27fba 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -2069,10 +2069,10 @@ class Operation(
def post_save_geo(self, save=True):
# manage geodata towns
- if getattr(self, "_post_saved_geo", True):
+ if getattr(self, "_post_save_geo_ok", False):
# prevent infinite loop - should not happen, but...
return
- self._post_saved_geo = True
+ self._post_save_geo_ok = True
q_towns = self.towns.filter(main_geodata__multi_polygon__isnull=False)
q_towns_nb = q_towns.count()
q_geodata_town = self.geodata.filter(
@@ -2097,21 +2097,21 @@ class Operation(
self.main_geodata = None
changed = True
- current_geo_town = None
+ current_town_geo = None
if q_towns_nb == 1:
- current_geo_town = q_towns.all()[0]
- if not q_geodata_town.filter(pk=current_geo_town.pk).count():
- for geo in q_geodata_town.all():
+ current_town_geo = q_towns.all()[0]
+ if not q_geodata_town.filter(pk=current_town_geo.pk).count():
+ for geo in q_geodata_town.exclude(source_id=current_town_geo.pk).all():
self.geodata.remove(geo)
if self.main_geodata == geo:
self.main_geodata = None
- self.geodata.add(current_geo_town)
+ self.geodata.add(current_town_geo.main_geodata)
changed = True
current_geo_area = None
if q_towns_nb > 1:
current_geo_area = Area.get_or_create_by_towns(q_towns, get_geo=True)
- if current_geo_area and not q_geodata_area.filter(pk=current_geo_area.pk).count():
+ if current_geo_area and not q_geodata_area.filter(source_id=current_geo_area.pk).count():
for geo in q_geodata_area.all():
self.geodata.remove(geo)
if self.main_geodata == geo:
@@ -2119,8 +2119,8 @@ class Operation(
self.geodata.add(current_geo_area)
changed = True
- if current_geo_town:
- q_extra_geo_town = q_geodata_town.exclude(pk=current_geo_town.pk)
+ if current_town_geo:
+ q_extra_geo_town = q_geodata_town.exclude(source_id=current_town_geo.pk)
if q_extra_geo_town.count():
# should not occur but bad migrations, bad imports...
for geo in q_extra_geo_town.all():
@@ -2129,7 +2129,7 @@ class Operation(
self.main_geodata = None
changed = True
if current_geo_area:
- q_extra_geo_area = q_geodata_area.exclude(pk=current_geo_area.pk)
+ q_extra_geo_area = q_geodata_area.exclude(source_id=current_geo_area.pk)
if q_extra_geo_area.count():
# should not occur but bad migrations, bad imports...
for geo in q_extra_geo_area.all():
diff --git a/ishtar_common/management/commands/migrate_to_geo_v4.py b/ishtar_common/management/commands/migrate_to_geo_v4.py
index 64ab25c16..8daf921d9 100644
--- a/ishtar_common/management/commands/migrate_to_geo_v4.py
+++ b/ishtar_common/management/commands/migrate_to_geo_v4.py
@@ -13,7 +13,7 @@ from django.core.management.base import BaseCommand
from ishtar_common.utils import ugettext_lazy as _
from ishtar_common import models_common, models
-from archaeological_operations.models import Operation
+from archaeological_operations.models import Operation, ArchaeologicalSite
log_path = os.sep.join([settings.ROOT_PATH, "logs"])
@@ -55,6 +55,9 @@ def migrate(quiet=False, log=True):
changed.append(["geovectordata", data.name, data.pk, "Création commune"])
town.main_geodata = data
town.save()
+ if not quiet and nb:
+ sys.stdout.write(f"\r[{get_time()}] Towns migrated\n")
+ sys.stdout.flush()
# manage operation vector sources
operation_content_type = ContentType.objects.get(
@@ -130,10 +133,13 @@ def migrate(quiet=False, log=True):
operation.save()
changed.append(
["geovectordata", data.name, data.pk, "Point opération"])
+ if not quiet and nb:
+ sys.stdout.write(f"\r[{get_time()}] Operation migrated\n")
+ sys.stdout.flush()
if log and changed:
- filename = f"geo_migration-created-{get_time().replace(':', '')}.txt"
+ filename = f"geo_migration-created-{get_time().replace(':', '')}.csv"
path = os.sep.join([log_path, filename])
with open(path, "w+") as fle:
writer = csv.writer(fle)
@@ -141,9 +147,7 @@ def migrate(quiet=False, log=True):
for change in changed:
writer.writerow(change)
if not quiet:
- sys.stdout.write(f"log: {path} written.")
- if not quiet:
- sys.stdout.write("\n")
+ sys.stdout.write(f"[{get_time()}] Log: {path} written\n")
def percent(current, total):
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index e86b8a55d..833851b5c 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -53,6 +53,8 @@ from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db import models
from django.contrib.gis.db.models.aggregates import Union
+from django.contrib.gis.geos.polygon import Polygon
+from django.contrib.gis.geos.collections import MultiPolygon
from django.contrib.postgres.fields import JSONField
from django.contrib.postgres.indexes import GinIndex
from django.contrib.sites.models import Site
@@ -2315,6 +2317,8 @@ class Area(HierarchicalType):
geo.origin = origins[0]
if len(set(providers)) == 1: # no ambiguous provider
geo.provider = providers[0]
+ if isinstance(poly, Polygon):
+ poly = MultiPolygon(poly,)
geo.multi_polygon = poly
geo.save()
if get_geo:
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index 53727b6d3..fc302166b 100644
--- a/ishtar_common/utils.py
+++ b/ishtar_common/utils.py
@@ -846,6 +846,30 @@ def _post_save_geo(sender, **kwargs):
if not instance:
return
+ if getattr(instance, "_post_saved_geo", False):
+ return
+
+ instance._post_saved_geo = True
+
+ modified = False
+ if getattr(instance, "post_save_geo", False):
+ instance.post_save_geo(save=False)
+ modified = True
+
+ if hasattr(instance, "need_update") and instance.need_update:
+ instance.need_update = False
+ modified = True
+
+ if modified:
+ instance.skip_history_when_saving = True
+ instance._post_saved_geo = True
+ instance._cached_label_checked = False
+ instance.save()
+ if hasattr(instance, "cascade_update"):
+ instance.cascade_update()
+ cache_key, __ = get_cache(sender, ["post_save_geo", instance.pk])
+ cache.set(cache_key, None, settings.CACHE_TASK_TIMEOUT)
+
return
# TODO to delete