Ejemplo n.º 1
0
def export_csv_for_model(model, dataset):
    model_cls = getattr(udata_models, model.capitalize(), None)
    if not model_cls:
        log.error('Unknow model %s' % model)
        return
    queryset = get_queryset(model_cls)
    adapter = csv.get_adapter(model_cls)
    if not adapter:
        log.error('No adapter found for %s' % model)
        return
    adapter = adapter(queryset)

    log.info('Exporting CSV for %s...' % model)

    csvfile = NamedTemporaryFile(delete=False)
    try:
        # write adapter results into a tmp file
        writer = csv.get_writer(csvfile)
        writer.writerow(adapter.header())
        for row in adapter.rows():
            writer.writerow(row)
        csvfile.seek(0)
        # make a resource from this tmp file
        created, resource = store_resource(csvfile, model, dataset)
        # add it to the dataset
        if created:
            dataset.add_resource(resource)
        dataset.last_modified = datetime.now()
        dataset.save()
    finally:
        os.unlink(csvfile.name)
Ejemplo n.º 2
0
def export_csv_for_model(model, dataset):
    model_cls = getattr(udata_models, model.capitalize(), None)
    if not model_cls:
        log.error('Unknow model %s' % model)
        return
    queryset = get_queryset(model_cls)
    adapter = csv.get_adapter(model_cls)
    if not adapter:
        log.error('No adapter found for %s' % model)
        return
    adapter = adapter(queryset)

    log.info('Exporting CSV for %s...' % model)

    csvfile = NamedTemporaryFile(mode='w', encoding='utf8', delete=False)
    try:
        # write adapter results into a tmp file
        writer = csv.get_writer(csvfile)
        writer.writerow(adapter.header())
        for row in adapter.rows():
            writer.writerow(row)
        # make a resource from this tmp file
        created, resource = store_resource(csvfile, model, dataset)
        # add it to the dataset
        if created:
            dataset.add_resource(resource)
        dataset.last_modified = datetime.now()
        dataset.save()
    finally:
        csvfile.close()
        os.unlink(csvfile.name)
Ejemplo n.º 3
0
def datasets_csv():
    params = multi_to_dict(request.args)
    # redirect to EXPORT_CSV dataset if feature is enabled and no filter is set
    exported_models = current_app.config.get('EXPORT_CSV_MODELS', [])
    if not params and 'dataset' in exported_models:
        return redirect(get_export_url('dataset'))
    params['facets'] = False
    datasets = search.iter(Dataset, **params)
    adapter = csv.get_adapter(Dataset)
    return csv.stream(adapter(datasets), 'datasets')
Ejemplo n.º 4
0
def datasets_csv():
    params = multi_to_dict(request.args)
    # redirect to EXPORT_CSV dataset if feature is enabled and no filter is set
    exported_models = current_app.config.get('EXPORT_CSV_MODELS', [])
    if not params and 'dataset' in exported_models:
        return redirect(get_export_url('dataset'))
    params['facets'] = False
    datasets = search.iter(Dataset, **params)
    adapter = csv.get_adapter(Dataset)
    return csv.stream(adapter(datasets), 'datasets')
Ejemplo n.º 5
0
def datasets_csv():
    params = multi_to_dict(request.args)
    params['facets'] = False
    datasets = search.iter(Dataset, **params)
    adapter = csv.get_adapter(Dataset)
    return csv.stream(adapter(datasets), 'datasets')
Ejemplo n.º 6
0
def from_adapter():
    cls = csv.get_adapter(Fake)
    adapter = cls(Fake.objects)
    return csv.stream(adapter)
Ejemplo n.º 7
0
def with_basename():
    cls = csv.get_adapter(Fake)
    adapter = cls(Fake.objects)
    return csv.stream(adapter, 'test')
Ejemplo n.º 8
0
def from_adapter():
    cls = csv.get_adapter(Fake)
    adapter = cls(Fake.objects)
    return csv.stream(adapter)
Ejemplo n.º 9
0
def with_basename():
    cls = csv.get_adapter(Fake)
    adapter = cls(Fake.objects)
    return csv.stream(adapter, 'test')
Ejemplo n.º 10
0
def datasets_csv():
    params = multi_to_dict(request.args)
    params['facets'] = False
    datasets = search.iter(Dataset, **params)
    adapter = csv.get_adapter(Dataset)
    return csv.stream(adapter(datasets), 'datasets')