def run(self, query, query_args):
"""Genera una respuesta CSV, con columnas
(indice tiempo, serie1, serie2, ...) y un dato por fila
"""
# Saco metadatos, no se usan para el formato CSV
query.set_metadata_config(constants.METADATA_NONE)
header = query_args.get(constants.PARAM_HEADER,
constants.API_DEFAULT_VALUES[constants.PARAM_HEADER])
series_ids = query.get_series_ids(how=header)
data = query.run()['data']
response = HttpResponse(content_type='text/csv')
content = 'attachment; filename="{}"'
response['Content-Disposition'] = content.format(constants.CSV_RESPONSE_FILENAME)
writer = unicodecsv.writer(response)
header = [settings.INDEX_COLUMN] + series_ids
writer.writerow(header)
for row in data:
writer.writerow(row)
return response
评论列表
文章目录