diff options
Diffstat (limited to 'ishtar_common/data_importer.py')
| -rw-r--r-- | ishtar_common/data_importer.py | 44 | 
1 files changed, 44 insertions, 0 deletions
diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 9dbd45e93..1c99782e3 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -26,8 +26,10 @@ import re  import sys  import zipfile +from django.apps import apps  from django.conf import settings  from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType  from django.contrib.gis.geos.error import GEOSException  from django.db.models.fields import FieldDoesNotExist  from django.core.exceptions import FieldError @@ -831,6 +833,8 @@ class Importer(object):      SLUG = ""      NAME = ""      DESC = "" +    TYPE = "" +    MAIN_GEO = False      LINE_FORMAT = []      OBJECT_CLS = None      UNICITY_KEYS = [] @@ -1338,6 +1342,7 @@ class Importer(object):          n2 = n          self.c_errors = False          c_row = [] +        idx_col = 0          for idx_col, val in enumerate(line):              try:                  self._row_processing(c_row, idx_col, idx_line, val, data) @@ -1368,6 +1373,10 @@ class Importer(object):          self.new_objects, self.updated_objects = [], []          self.ambiguous_objects, self.not_find_objects = [], [] +        geodata = {} +        if self.TYPE == "gis": +            if "geodata" in data: +                geodata = data.pop("geodata")          obj, created = self.get_object(self.OBJECT_CLS, data, idx_line=idx_line)          if self.simulate:              return data @@ -1391,6 +1400,41 @@ class Importer(object):              obj._no_post_save = True              obj.save()              self._add_to_post_save(obj.__class__, obj.pk, idx_line) + +        if self.TYPE == "gis": +            content_type = ContentType.objects.get( +                app_label=obj.__class__._meta.app_label, +                model=obj.__class__.__name__.lower() +            ) +            geodata.update({ +                "source_id": obj.pk, +                "source_content_type": content_type, +            }) +            GeoVectorData = apps.get_model("ishtar_common", "GeoVectorData") +            item = None +            if "import_key" in geodata: +                q = GeoVectorData.objects.filter( +                    import_key=geodata["import_key"], +                    source_id=obj.pk, +                    source_content_type=content_type, +                ) +                if q.count(): +                    item = q.all()[0] +            if item: +                for k in geodata: +                    setattr(item, k, geodata[k]) +                item.save() +            else: +                item = GeoVectorData.objects.create(**geodata) +            if self.import_instance: +                item.imports.add(self.import_instance) +            if self.MAIN_GEO: +                obj.main_geodata = item +                obj._post_saved_geo = True +                obj._no_move = True +                obj.skip_history_when_saving = True +                obj.save() +          n = datetime.datetime.now()          logger.debug("* %s - Item saved" % (str(n - n2)))          n2 = n  | 
