def _build_zip(queries):
    temp = tempfile.TemporaryFile()
    zip_file = ZipFile(temp, 'w')
    map(lambda r: zip_file.writestr('%s.csv' % r.title, csv_report(r) or "Error!"), queries)
    zip_file.close()
    ret = FileWrapper(temp)
    temp.seek(0)
    return ret
    def test_simple_query_runs(self):
        expected_csv = 'two\r\n2\r\n'

        r = SimpleQueryFactory()
        result = csv_report(r)

        self.assertIsNotNone(result, "Query '%s' returned None." % r.title)
        self.assertEqual(result.lower(), expected_csv)
Beispiel #3
0
def snapshot_query(query_id):
    logger.info("Starting snapshot for query %s..." % query_id)
    q = Query.objects.get(pk=query_id)
    r = csv_report(q)
    k = 'query-%s.snap-%s.csv' % (q.id, date.today().strftime('%Y%m%d-%H:%M:%S'))
    logger.info("Uploading snapshot for query %s as %s..." % (query_id, k))
    resp = _upload(k, r)
    logger.info("Done uploading snapshot for query %s. URL: %s" % (query_id, resp.url))
Beispiel #4
0
def snapshot_query(query_id):
    logger.info("Starting snapshot for query %s..." % query_id)
    q = Query.objects.get(pk=query_id)
    r = csv_report(q)
    k = 'query-%s.snap-%s.csv' % (q.id, date.today().strftime('%Y%m%d-%H:%M:%S'))
    logger.info("Uploading snapshot for query %s as %s..." % (query_id, k))
    resp = _upload(k, r)
    logger.info("Done uploading snapshot for query %s. URL: %s" % (query_id, resp.url))
Beispiel #5
0
    def test_simple_query_runs(self):

        expected_csv = 'two\r\n2\r\n'

        r = SimpleQueryFactory()
        result = csv_report(r)

        self.assertIsNotNone(result, "Query '%s' returned None." % r.title)
        self.assertEqual(result.lower(), expected_csv)
    def test_simple_query_does_not_transform(self):

        expected_csv = 'foo\r\n1\r\n'

        r = SimpleQueryFactory(sql='select 1 as foo')
        result = csv_report(r).getvalue()

        self.assertIsNotNone(result, "Query '%s' returned None." % r.title)
        self.assertEqual(result.lower(), expected_csv)
def _build_zip(queries):
    temp = tempfile.TemporaryFile()
    zip_file = ZipFile(temp, 'w')
    for r in queries:
        zip_file.writestr('%s.csv' % r.title, csv_report(r).getvalue() or "Error!")
    zip_file.close()
    ret = FileWrapper(temp)
    temp.seek(0)
    return ret
def _package(queries):
    ret = {}
    is_one = len(queries) == 1
    name_root = lambda n: "attachment; filename=%s" % 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 csv_report(queries[0]).getvalue()) or _build_zip(queries)
    ret["length"] = (is_one and len(ret["data"]) or ret["data"].blksize)
    return ret
Beispiel #9
0
def _build_zip(queries):
    temp = tempfile.TemporaryFile()
    zip_file = ZipFile(temp, 'w')
    for r in queries:
        zip_file.writestr('%s.csv' % r.title, csv_report(r) or "Error!")
    zip_file.close()
    ret = FileWrapper(temp)
    temp.seek(0)
    return ret
Beispiel #10
0
    def test_simple_query_does_not_transform(self):

        expected_csv = 'foo\r\n1\r\n'

        r = SimpleQueryFactory(sql='select 1 as foo')
        result = csv_report(r).getvalue()

        self.assertIsNotNone(result, "Query '%s' returned None." % r.title)
        self.assertEqual(result.lower(), expected_csv)
Beispiel #11
0
def execute_query(query_id, email_address):
    q = Query.objects.get(pk=query_id)
    r = csv_report(q)
    random_part = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
    resp = _upload('%s.csv' % random_part, r)

    subj = '[SQL Explorer] Report "%s" is ready' % q.title
    msg = 'Download results:\n\r%s' % resp.url

    send_mail(subj, msg, app_settings.FROM_EMAIL, [email_address])
Beispiel #12
0
def execute_query(query_id, email_address):
    q = Query.objects.get(pk=query_id)
    r = csv_report(q)
    random_part = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20))
    resp = _upload('%s.csv' % random_part, r)

    subj = '[SQL Explorer] Report "%s" is ready' % q.title
    msg = 'Download results:\n\r%s' % resp.url

    send_mail(subj, msg, app_settings.FROM_EMAIL, [email_address])
Beispiel #13
0
def _package(queries):
    ret = {}
    is_one = len(queries) == 1
    name_root = lambda n: "attachment; filename=%s" % 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 csv_report(queries[0])) or _build_zip(queries)
    ret["length"] = (is_one and len(ret["data"]) or ret["data"].blksize)
    return ret