From a6be0e9966c51aef1ef1b154d28627924902e179 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 28 Dec 2015 09:30:16 +0100 Subject: Better management of JSON imports --- chimere/utils.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/chimere/utils.py b/chimere/utils.py index 5289600..0dfe588 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -22,12 +22,13 @@ Utilitaries """ import csv +import collections import datetime import feedparser +import json import os import re import StringIO -import json import tempfile import urllib2 import unicodedata @@ -683,7 +684,8 @@ class JsonManager(ImportManager): vals = source.read().replace('\n', ' ') try: - values = json.JSONDecoder().decode(vals) + values = json.JSONDecoder( + object_pairs_hook=collections.OrderedDict).decode(vals) except ValueError as e: return (new_item, updated_item, _(u"JSON file is not well formed: " + e.message)) @@ -704,20 +706,38 @@ class JsonManager(ImportManager): _(u"A key must be associated to \"%s\" in the " u"filter.") % k) + default_dct = {'origin': self.importer_instance.origin, + 'license': self.importer_instance.license} + if 'prefix_title' in filtr: + default_dct['title'] = filtr.pop('prefix_title') + if 'prefix_description' in filtr: + default_dct['description'] = filtr.pop('prefix_description') + if self.importer_instance.default_localisation: + default_dct['point'] = self.importer_instance.default_localisation + for item in values: - dct = {'origin': self.importer_instance.origin, - 'license': self.importer_instance.license} + dct = default_dct.copy() for k in filtr: if k in item and item[k]: - dct[filtr[k]] = item[k] + if filtr[k] not in dct: + dct[filtr[k]] = "" + else: + if filtr[k] == 'description': + dct[filtr[k]] += "
" + else: + dct[filtr[k]] += " " + dct[filtr[k]] += item[k] if 'point' in item: x, y = item['point'].split(",") dct['point'] = 'SRID=4326;POINT(%s %s)' % (x, y) - elif 'lat' in item and item['lat']: + elif 'lat' in item and item['lat'] \ + and 'lon' in item and item['lon']: dct['point'] = 'SRID=4326;POINT(%s %s)' % (item['lon'], item['lat']) - else: - dct['point'] = self.importer_instance.default_localisation + elif 'x' in item and item['x'] \ + and 'y' in item and item['y']: + dct['point'] = 'SRID=4326;POINT(%s %s)' % (item['x'], + item['y']) if not dct['point']: continue cls = Marker -- cgit v1.2.3