Exemple #1
0
    def upload_files(self, filenames, configs=None):

        from geonode.layers.models import Layer
        from osgeo_importer.models import UploadLayer
        outfiles = []
        buildconfigs = False
        if configs is None:
            buildconfigs = True
            configs = []
        for filename in filenames:
            idx = 0
            path = self.get_file_path(filename)
            with open(path) as stream:
                data = stream.read()
            upload = SimpleUploadedFile(filename, data)
            outfiles.append(upload)
            # Check if filename has been configured and apply default
            # configuration if not
            if buildconfigs:
                configs.append({
                    'upload_file_name': filename,
                    'config': {
                        'index': idx
                    }
                })
            idx = idx + 1

        response = self.client.post(reverse('uploads-new-json'), {
            'file': outfiles,
            'json': json.dumps(configs)
        },
                                    follow=True)
        content = json.loads(response.content)
        logger.debug('UPLOAD RESPONSE -------- %s', content)
        self.assertEqual(response.status_code, 200)

        upload_id = content['id']
        upload_layers = UploadLayer.objects.filter(upload_id=upload_id)
        self.upload_layers = upload_layers

        retval = []

        response = self.client.get('/importer-api/data-layers',
                                   content_type='application/json')

        for upload_layer in upload_layers:
            for cfg in configs:
                logger.debug('CFG: %s', cfg)
                if cfg['upload_file_name'] == upload_layer.upload_file.name:
                    config = cfg['config']
                    config['upload_layer_id'] = upload_layer.id
                    logger.debug('CONFIG: %s', config)
                    import_object(upload_layer.upload_file.id, config)
                    retval.append(upload_layer)
        logger.debug('Upload Files result: %s', retval)
        return retval
Exemple #2
0
    def upload_files(self, filenames, configs=None):

        from geonode.layers.models import Layer
        from osgeo_importer.models import UploadLayer
        outfiles = []
        for filename in filenames:
            path = self.get_file_path(filename)
            with open(path) as stream:
                data = stream.read()
            upload = SimpleUploadedFile(filename, data)
            outfiles.append(upload)
        response = self.client.post(reverse('uploads-new-json'), {
            'file': outfiles,
            'json': json.dumps(configs)
        },
                                    follow=True)
        content = json.loads(response.content)
        logger.debug('UPLOAD RESPONSE -------- %s', content)
        self.assertEqual(response.status_code, 200)
        uls = UploadLayer.objects.all()
        logger.debug(
            'There are ----------------- %s ---------------------- Upload Layers',
            uls.count())
        for testul in uls:
            logger.debug('UploadLayer %s %s %s', testul.id, testul.task_id,
                         testul.import_status)
        # Configure Uploaded Files
        upload_id = content['id']
        upload_layers = UploadLayer.objects.filter(upload_id=upload_id)

        retval = []

        response = self.client.get('/importer-api/data-layers',
                                   content_type='application/json')
        logger.debug('UPLOAD LAYERS %s', response.content)
        for testul in uls:
            logger.debug('UploadLayer %s %s %s', testul.id, testul.task_id,
                         testul.import_status)

        for upload_layer in upload_layers:
            for cfg in configs:
                logger.debug('CFG: %s', cfg)
                config = cfg['config']
                config['upload_layer_id'] = upload_layer.id
                logger.debug('CONFIG: %s', config)
                import_object(upload_layer.upload_file.id, config)
                retval.append(upload_layer)
        return retval