def import_maps(gs_data_dir, conn, zipfile, no_password=False, chown_to=None, do_django_layer_save=True, from_string=None, to_string=None): tempdir = tempfile.mkdtemp() temppath = lambda *p: os.path.join(tempdir, *p) os.system('unzip %s -d %s' % (zipfile, tempdir)) for layer_name in os.listdir(temppath('layers')): import_layer(gs_data_dir, conn, temppath('layers', layer_name), layer_name, no_password, chown_to, do_django_layer_save) conn.commit() print 'layers import complete' def import_models(path, add_owner=False): with open(path, 'r') as f: models = serializers.deserialize('json', f) for model in models: if add_owner: owner = User.objects.filter(pk=model.object.owner_id) if not owner: model.object.owner = User.objects.get(pk=1) model.save() return models print 'importing maps' import_models(temppath('maps.json'), add_owner=True) print 'importing map layers' maplayer_models = import_models(temppath('maplayers.json')) print 'importing map thumb specs' with open(temppath('map_thumb_specs.json')) as f: map_thumbs = json.load(f) thumb_updater = (make_thumbnail_updater(from_string, to_string) if (from_string is not None and to_string is not None) else lambda m:m.save()) for mapid, thumb_spec in map_thumbs: try: m = Map.objects.get(pk=mapid) except Map.DoesNotExist: print 'No map "%s" for importing thumb spec' % mapid else: m.set_thumbnail(thumb_spec) t = m.get_thumbnail() thumb_updater(t) if from_string is not None and to_string is not None: print 'adjusting maplayer params' updater = make_maplayer_updater(from_string, to_string) for maplayer_model in maplayer_models: try: maplayer = MapLayer.objects.get(pk=maplayer_model.object.id) except MapLayer.DoesNotExist: print 'No maplayer "%s" for updating layer params' % maplayer.object.id else: updater(maplayer)
def import_maps(conn, zipfile, no_password=False, chown_to=None, do_django_layer_save=True, from_string=None, to_string=None): # unless specified, assume importing to local setup if to_string is None: to_string = settings.SITEURL tempdir = tempfile.mkdtemp() temppath = lambda *p: os.path.join(tempdir, *p) os.system('unzip %s -d %s' % (zipfile, tempdir)) for layer_name in os.listdir(temppath('layers')): import_layer(conn, temppath('layers', layer_name), layer_name, no_password, chown_to, do_django_layer_save) conn.commit() print 'layers import complete' def import_models(path, add_owner=False): with open(path, 'r') as f: # deserialize returns a generator, read everything models = list(serializers.deserialize('json', f)) for model in models: if add_owner: owner = User.objects.filter(pk=model.object.owner_id) if not owner: model.object.owner = User.objects.get(pk=1) model.save() print 'imported %s=%s' % (model.object.__class__.__name__, model.object.pk) return models print 'importing maps' import_models(temppath('maps.json'), add_owner=True) print 'importing map layers' maplayer_models = import_models(temppath('maplayers.json')) print 'importing users' import_models(temppath('comment_users.json')) print 'importing map comments' import_models(temppath('map_comments.json')) print 'importing map publishing status' # make sure we don't end up with duplicates, just copy the status value with open(temppath('map_publishing_status.json'), 'r') as f: models = list(serializers.deserialize('json', f)) for model in models: real_object = model.object PublishingStatus.objects.set_status(real_object.map or real_object.layer, real_object.status) print 'importing map thumb specs' with open(temppath('map_thumb_specs.json')) as f: map_thumbs = json.load(f) thumb_updater = (make_thumbnail_updater(from_string, to_string) if (from_string is not None and to_string is not None) else lambda m:m.save()) for mapid, thumb_spec in map_thumbs: try: m = Map.objects.get(pk=mapid) except Map.DoesNotExist: print 'No map "%s" for importing thumb spec' % mapid else: m.set_thumbnail(thumb_spec) t = m.get_thumbnail() thumb_updater(t) if from_string is not None and to_string is not None: print 'adjusting maplayer params' if to_string[-1] != '/': to_string += '/' updater = make_maplayer_updater(from_string, to_string) for maplayer_model in maplayer_models: try: maplayer = MapLayer.objects.get(pk=maplayer_model.object.id) except MapLayer.DoesNotExist: print 'No maplayer "%s" for updating layer params' % maplayer.object.id else: updater(maplayer)
def import_maps(conn, zipfile, no_password=False, chown_to=None, do_django_layer_save=True, from_string=None, to_string=None): # unless specified, assume importing to local setup if to_string is None: to_string = settings.SITEURL tempdir = tempfile.mkdtemp() temppath = lambda *p: os.path.join(tempdir, *p) os.system('unzip %s -d %s' % (zipfile, tempdir)) for layer_name in os.listdir(temppath('layers')): import_layer(conn, temppath('layers', layer_name), layer_name, no_password, chown_to, do_django_layer_save) conn.commit() print 'layers import complete' def import_models(path, add_owner=False): with open(path, 'r') as f: # deserialize returns a generator, read everything models = list(serializers.deserialize('json', f)) for model in models: if add_owner: owner = User.objects.filter(pk=model.object.owner_id) if not owner: model.object.owner = User.objects.get(pk=1) model.save() print 'imported %s=%s' % (model.object.__class__.__name__, model.object.pk) return models print 'importing maps' import_models(temppath('maps.json'), add_owner=True) print 'importing map layers' maplayer_models = import_models(temppath('maplayers.json')) print 'importing users' import_models(temppath('comment_users.json')) print 'importing map comments' import_models(temppath('map_comments.json')) print 'importing map publishing status' # make sure we don't end up with duplicates, just copy the status value with open(temppath('map_publishing_status.json'), 'r') as f: models = list(serializers.deserialize('json', f)) for model in models: real_object = model.object PublishingStatus.objects.set_status( real_object.map or real_object.layer, real_object.status) print 'importing map thumb specs' with open(temppath('map_thumb_specs.json')) as f: map_thumbs = json.load(f) thumb_updater = (make_thumbnail_updater(from_string, to_string) if (from_string is not None and to_string is not None) else lambda m: m.save()) for mapid, thumb_spec in map_thumbs: try: m = Map.objects.get(pk=mapid) except Map.DoesNotExist: print 'No map "%s" for importing thumb spec' % mapid else: m.set_thumbnail(thumb_spec) t = m.get_thumbnail() thumb_updater(t) if from_string is not None and to_string is not None: print 'adjusting maplayer params' if to_string[-1] != '/': to_string += '/' updater = make_maplayer_updater(from_string, to_string) for maplayer_model in maplayer_models: try: maplayer = MapLayer.objects.get(pk=maplayer_model.object.id) except MapLayer.DoesNotExist: print 'No maplayer "%s" for updating layer params' % maplayer.object.id else: updater(maplayer)