def _package(queries): ret = {} is_one = len(queries) == 1 name_root = lambda n: f"attachment; filename={n}" ret["content_type"] = (is_one and 'text/csv') or 'application/zip' ret["filename"] = (is_one and name_root( '%s.csv' % queries[0].title.replace(',', ''))) or name_root( "Report_%s.zip" % date.today()) ret["data"] = (is_one and CSVExporter( queries[0]).get_output()) or _build_zip(queries) ret["length"] = (is_one and len(ret["data"]) or ret["data"].blksize) return ret
def test_writing_unicode(self): res = QueryResult( SimpleQueryFactory(sql='select 1 as "a", 2 as ""').sql, connections[CONN], 1, 1000, 10000, ) res.execute_query() res.process() res._data = [[1, None], [u"Jenét", '1']] res = CSVExporter(query=None)._get_output(res).getvalue() self.assertEqual(res, 'a,\r\n1,\r\nJenét,1\r\n')
def test_custom_delimiter(self): q = SimpleQueryFactory(sql='select 1, 2') exporter = CSVExporter(query=q) res = exporter.get_output(delim='|') self.assertEqual(res, '1|2\r\n1|2\r\n')
def test_custom_delimiter(self): q = SimpleQueryFactory(sql="select 1, 2") exporter = CSVExporter(query=q) res = exporter.get_output(delim="|") self.assertEqual(res, "1|2\r\n1|2\r\n")
def test_writing_bom(self): q = SimpleQueryFactory(sql='select 1, 2') exporter = CSVExporter(query=q) res = exporter.get_output() self.assertEqual(res, '\ufeff1,2\r\n1,2\r\n')