diff options
Diffstat (limited to 'chimere/utils.py')
| -rw-r--r-- | chimere/utils.py | 45 | 
1 files changed, 30 insertions, 15 deletions
| diff --git a/chimere/utils.py b/chimere/utils.py index 0d97757..33ebdf7 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -1,6 +1,6 @@  #!/usr/bin/env python  # -*- coding: utf-8 -*- -# Copyright (C) 2012  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2012-2013  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>  # This program is free software: you can redistribute it and/or modify  # it under the terms of the GNU General Public License as @@ -46,11 +46,13 @@ from chimere import get_version  from external_utils import OsmApi  def unicode_normalize(string): +    if type(string) == str: +        string = unicode(string.decode('utf-8'))      return ''.join(          (c for c in unicodedata.normalize('NFD', string)          if unicodedata.category(c) != 'Mn')) -class ImportManager: +class ImportManager(object):      u"""      Generic class for specific importers      """ @@ -74,8 +76,11 @@ class ImportManager:                                key='', pk=None):          updated, created, item = False, False, None          import_key = unicode(import_key).replace(':', '^') +        if not values.get('name'): +            values['name'] = self.default_name          if not key:              key = self.importer_instance.importer_type +        item = None          if import_key or pk:              dct_import = {                  'import_key__icontains':'%s:%s;' % (key, import_key), @@ -85,7 +90,10 @@ class ImportManager:                  if pk:                      ref_item = cls.objects.get(pk=pk)                  else: -                    ref_item = cls.objects.get(**dct_import) +                    ref_item = cls.objects.filter(**dct_import) +                    if not ref_item.count(): +                        raise ObjectDoesNotExist +                    ref_item = ref_item.all()[0]                  if version and ref_item.import_version == int(version):                      # no update since the last import                      return ref_item, None, None @@ -95,7 +103,8 @@ class ImportManager:                  else:                      item = ref_item                      for k in values: -                        setattr(item, k, values[k]) +                        if values[k]: +                            setattr(item, k, values[k])                      try:                          item.save()                          # force the modified_since_import status @@ -108,6 +117,10 @@ class ImportManager:              except ObjectDoesNotExist:                  pass          if not item: +            if not self.importer_instance.get_description and \ +               self.importer_instance.default_description: +                values['description'] = \ +                                     self.importer_instance.default_description              values.update({                  'import_source':self.importer_instance.source})              values['status'] = 'I' @@ -197,7 +210,7 @@ class KMLManager(ImportManager):      XPATH = '//kml:Folder/kml:name[text()="%s"]/../kml:Placemark'      DEFAULT_XPATH = '//kml:Placemark'      def __init__(self, importer_instance, ns=''): -        self.importer_instance = importer_instance +        super(KMLManager, self).__init__(importer_instance)          self.ns = ns      def get(self): @@ -237,6 +250,7 @@ class KMLManager(ImportManager):              pl_id = placemark.attrib.get('id')              pl_key = 'kml-%d' % self.importer_instance.pk              ns = '{%s}' % self.ns +            description = ''              for item in placemark:                  if item.tag == ns + 'name':                      name = item.text @@ -244,7 +258,8 @@ class KMLManager(ImportManager):                          # if no ID is provided assume that name is a key                          pl_id = name                  elif item.tag == ns + 'description': -                    description = item.text +                    if self.importer_instance.get_description: +                        description = item.text                  elif item.tag == ns + 'Point':                      for coord in item:                          if coord.tag == ns + 'coordinates': @@ -506,7 +521,10 @@ class CSVManager(ImportManager):              if len(row) < len(cols):                  continue              pk, name, cats, state = row[0], row[1], row[2], row[3] -            description, geom = row[4], row[5].upper() +            geom = row[5] +            description = '' +            if self.importer_instance.get_description: +                description = row[4]              COL_INDEX = 6              dct = {'description':description,                     'name':name, @@ -595,10 +613,11 @@ class GeoRSSManager(ImportManager):                      y = item['geo_lat']                      x = item['geo_long']                  dct['point'] = 'SRID=4326;POINT(%s %s)' % (x, y) -                for k in ['description', 'summary', 'value']: -                    if k in item: -                        dct['description'] = item[k] -                        break +                if self.importer_instance.get_description: +                    for k in ['description', 'summary', 'value']: +                        if k in item: +                            dct['description'] = item[k] +                            break              else:                  cls = Route                  points = item['georss_line'].split(' ') @@ -675,8 +694,6 @@ class OSMManager(ImportManager):                      name = item.attrib.get('v')                  if item.tag == 'nd':                      points.append(item.get('ref')) -            if not name: -                name = self.default_name              if not points:                  continue              wkt = 'SRID=4326;LINESTRING(%s)' % ",".join([nodes[point_id] @@ -710,8 +727,6 @@ class OSMManager(ImportManager):                  k = item.attrib.get('k')                  if k == 'name':                      name = item.attrib.get('v') -            if not name: -                name = self.default_name              point = 'SRID=4326;POINT(%s %s)' % (node.get('lon'),                                                  node.get('lat'))              dct = {'point':point, | 
