summaryrefslogtreecommitdiff
path: root/chimere/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/utils.py')
-rw-r--r--chimere/utils.py63
1 files changed, 39 insertions, 24 deletions
diff --git a/chimere/utils.py b/chimere/utils.py
index 4b850e3..9eed7f7 100644
--- a/chimere/utils.py
+++ b/chimere/utils.py
@@ -65,29 +65,33 @@ class ImportManager:
pass
def create_or_update_item(self, cls, values, import_key, version=None):
- updated, created = False, False
- dct_import = {
- 'import_key__icontains':'%s:%s;' % (
- self.importer_instance.importer_type,
- import_key),
- 'import_source':self.importer_instance.source}
- try:
- item = cls.objects.get(**dct_import)
- if version and item.import_version == int(version):
- # no update since the last import
- return item, None, None
- for k in values:
- setattr(item, k, values[k])
- item.save()
- updated = True
- except ObjectDoesNotExist:
+ updated, created, item = False, False, None
+ if import_key:
+ dct_import = {
+ 'import_key__icontains':'%s:%s;' % (
+ self.importer_instance.importer_type,
+ import_key),
+ 'import_source':self.importer_instance.source}
+ try:
+ item = cls.objects.get(**dct_import)
+ if version and item.import_version == int(version):
+ # no update since the last import
+ return item, None, None
+ for k in values:
+ setattr(item, k, values[k])
+ item.save()
+ updated = True
+ except ObjectDoesNotExist:
+ pass
+ if not item:
values.update({
'import_source':self.importer_instance.source})
values['status'] = 'I'
item = cls.objects.create(**values)
created = True
- item.set_key(self.importer_instance.importer_type,
- import_key)
+ if import_key:
+ item.set_key(self.importer_instance.importer_type,
+ import_key)
item.categories.clear()
for cat in self.importer_instance.categories.all():
item.categories.add(cat)
@@ -269,7 +273,7 @@ class ShapefileManager(ImportManager):
- number of item updated ;
- error detail on error
"""
- from models import Marker
+ from models import Marker, Route
new_item, updated_item, msg = 0, 0, ''
tmpdir = tempfile.mkdtemp()
sources, msg = self.get_source_file(source,
@@ -299,9 +303,6 @@ class ShapefileManager(ImportManager):
shapefilename = tmpdir + os.sep + sources[0]
ds = DataSource(shapefilename)
lyr = ds[0]
- if lyr.geom_type not in ('Point',):
- return (0, 0, _(u"Type of geographic item of this shapefile "
- u"is not managed by Chimère."))
# for this first version it is assumed that the first field is a
# id name and the second field is the name
id_name = lyr.fields[0] if len(lyr.fields) > 0 else None
@@ -315,6 +316,11 @@ class ShapefileManager(ImportManager):
lbl_name = lyr.fields[1]
elif id_name:
lbl_name = id_name
+ if lyr.geom_type not in ('Point', 'LineString'):
+ return (0, 0, _(u"Type of geographic item of this shapefile "
+ u"is not managed by Chimère."))
+ geom_key = 'point' if lyr.geom_type == 'Point' else 'route'
+ geom_cls = Marker if lyr.geom_type == 'Point' else Route
indexes = []
for idx, feat in enumerate(lyr):
name = unicode(idx)
@@ -329,9 +335,18 @@ class ShapefileManager(ImportManager):
except:
continue
geom = feat.geom.wkt
- dct = {'point':'SRID=%s;%s' % (srid, feat.geom.wkt),
+ dct = {geom_key:'SRID=%s;%s' % (srid, feat.geom.wkt),
'name':name
}
+ import_key = feat.get(id_name) if id_name else ''
+ item, updated, created = self.create_or_update_item(
+ geom_cls, dct, import_key)
+ if updated:
+ updated_item += 1
+ if created:
+ new_item += 1
+
+ """
m = None
if id_name:
c_id = feat.get(id_name)
@@ -356,7 +371,7 @@ class ShapefileManager(ImportManager):
m.set_key(id_name, c_id)
m.categories.clear()
for cat in self.importer_instance.categories.all():
- m.categories.add(cat)
+ m.categories.add(cat)"""
# clean up
tmpdirs = set()
for src in sources: