diff options
| -rw-r--r-- | ishtar_common/data_importer.py | 54 | ||||
| -rw-r--r-- | ishtar_common/models_imports.py | 12 | ||||
| -rw-r--r-- | ishtar_common/utils.py | 10 | 
3 files changed, 45 insertions, 31 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 2e782d5e4..7ecf800ba 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -39,11 +39,12 @@ from django.template.defaultfilters import slugify  from django.utils.translation import gettext_lazy as _  from ishtar_common.utils import ( +    BColors,      get_all_field_names,      get_current_profile,      get_file_from_link, +    IMPORT_GEOMETRY,      update_data, -    BColors  ) @@ -1325,14 +1326,24 @@ class Importer(object):          self.ambiguous_objects, self.not_find_objects = [], []          geodata, main_geodata = {}, {} +        self.geo_prefix = "base_finds" if self.OBJECT_CLS.__name__ == "Find" else ""          if self.TYPE in ("gis", "qgs"):              profile = get_current_profile()              default_srs = profile.srs if profile.srs else None -            if "geodata" in data: -                geodata = self._manage_geodata(default_srs, geodata, data) -            if "main_geodata" in data: -                main_geodata = self._manage_geodata(default_srs, main_geodata, data, key="main_geodata") +            alt_data = data +            if self.geo_prefix and self.geo_prefix in data: +                alt_data = data[self.geo_prefix] +            if "geodata" in alt_data: +                geodata = self._manage_geodata(default_srs, geodata, alt_data) +                # do not import if not geometry is set +                geom_fields = list(IMPORT_GEOMETRY.values()) + ["x", "z"] +                if not any(1 for k in geom_fields +                           if (k in geodata) and geodata[k]): +                    geodata = {} +            if "main_geodata" in alt_data: +                main_geodata = self._manage_geodata(default_srs, main_geodata, alt_data, key="main_geodata")                  # TODO: main_geodata not used? +          obj, created = self.get_object(self.OBJECT_CLS, data, idx_line=idx_line)          if self.simulate:              return data @@ -1367,9 +1378,12 @@ class Importer(object):              self._add_to_post_save(obj.__class__, obj.pk, idx_line)          GeoVectorData = apps.get_model("ishtar_common", "GeoVectorData") -        if self.TYPE in ("gis", "qgs") and not isinstance(obj, GeoVectorData): +        if self.TYPE in ("gis", "qgs") and geodata and not isinstance(obj, GeoVectorData):              # create GIS data and attach to the created object if the object is not GIS -            if not obj and not self.c_errors: +            geo_obj = obj +            if self.OBJECT_CLS.__name__ == "Find": +                geo_obj = obj.get_first_base_find() +            if not geo_obj and not self.c_errors:                  self.c_errors = True                  self.errors.append(                      ( @@ -1380,11 +1394,11 @@ class Importer(object):                  )                  return              content_type = ContentType.objects.get( -                app_label=obj.__class__._meta.app_label, -                model=obj.__class__.__name__.lower() +                app_label=geo_obj.__class__._meta.app_label, +                model=geo_obj.__class__.__name__.lower()              )              geodata.update({ -                "source_id": obj.pk, +                "source_id": geo_obj.pk,                  "source_content_type": content_type,              })              item = None @@ -1392,7 +1406,7 @@ class Importer(object):              if "import_key" in geodata:                  q = GeoVectorData.objects.filter(                      import_key=geodata["import_key"], -                    source_id=obj.pk, +                    source_id=geo_obj.pk,                      source_content_type=content_type,                  )                  if q.count(): @@ -1417,15 +1431,15 @@ class Importer(object):              if self.import_instance and created:                  item.imports.add(self.import_instance)              if self.MAIN_GEO: -                obj._timestamp = self.timestamp -                obj.main_geodata = item -                obj._post_saved_geo = True -                obj._no_move = True -                obj.skip_history_when_saving = True -                obj._queue = "low_priority" -                if hasattr(obj, "no_post_process"): -                    obj.no_post_process(history=True) -                obj.save() +                geo_obj._timestamp = self.timestamp +                geo_obj.main_geodata = item +                geo_obj._post_saved_geo = True +                geo_obj._no_move = True +                geo_obj.skip_history_when_saving = True +                geo_obj._queue = "low_priority" +                if hasattr(geo_obj, "no_post_process"): +                    geo_obj.no_post_process(history=True) +                geo_obj.save()          n = datetime.datetime.now()          logger.debug("* %s - Item saved" % (str(n - n2))) diff --git a/ishtar_common/models_imports.py b/ishtar_common/models_imports.py index 712411569..dd675e840 100644 --- a/ishtar_common/models_imports.py +++ b/ishtar_common/models_imports.py @@ -80,6 +80,7 @@ from ishtar_common.utils import (      put_session_message,      put_session_var,      update_data, +    IMPORT_GEOMETRY,      OwnPerms,      SheetItem,      jinja_evaluation @@ -1515,17 +1516,6 @@ class ImportChunk(models.Model):          return new_import -IMPORT_GEOMETRY = { -    "Point": "point_2d", -    "3D Point": "point_3d", -    "MultiPoint": "multi_points", -    "LineString": "multi_line", -    "MultiLineString": "multi_line", -    "Polygon": "multi_polygon", -    "MultiPolygon": "multi_polygon", -} - -  class BaseImport(models.Model, OwnPerms, SheetItem):      user = models.ForeignKey(          "IshtarUser", blank=True, null=True, on_delete=models.SET_NULL diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 242fa4159..937b9bb99 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -391,6 +391,16 @@ API_MAIN_MODELS = dict(      [(model_name, app_name) for app_name, model_name in API_MAIN_CONTENT_TYPES]  ) +IMPORT_GEOMETRY = { +    "Point": "point_2d", +    "3D Point": "point_3d", +    "MultiPoint": "multi_points", +    "LineString": "multi_line", +    "MultiLineString": "multi_line", +    "Polygon": "multi_polygon", +    "MultiPolygon": "multi_polygon", +} +  class HistoryError(Exception):      def __init__(self, value):  | 
