app.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:metaseek 作者: ahoarfrost 项目源码 文件源码
def get(self, id):
        discovery = Discovery.query.filter_by(id=id).first()
        filter_params = json.loads(discovery.filter_params)
        rules = filter_params['rules']

        queryObject = filterDatasetQueryObjectWithRules(Dataset.query,filter_params['rules'])
        results = queryObject.all()

        # StringIO lets you write a csv to a buffer instead of directly to a file with csv writer
        si = StringIO.StringIO()

        # have to use unicodecsv instead of csv or it fails on our unicode data (like the "degree" sign)
        cw = unicodecsv.writer(si, encoding='utf-8')

        # write all of the names of the columns as the first row so the CSV has column headers
        cw.writerow([column.name for column in Dataset.__mapper__.columns])

        # for every result object, get the value for every column, store in an array and write to the csv buffer
        [cw.writerow([getattr(row, column.name) for column in Dataset.__mapper__.columns]) for row in results]

        # send with the right headers to have the browser treat it like a downloadable file
        output = make_response(si.getvalue())
        output.headers["Content-Disposition"] = "attachment; filename=" + slugify(discovery.discovery_title) + "_discovery_data.csv"
        output.headers["Content-type"] = "text/csv"
        return output
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号