Is it possible to attach a csv in mail in web2py without providing
path?

I have these 2 methods for generating csv:

def report_download(result, colnames):
    import cStringIO
    stream = cStringIO.StringIO()
    render_report_csv(stream, result, colnames=colnames)
    filename = '%s-%s-%s.csv' % (request.function, request.args(0),
request.now)
    response.headers['Content-Type'] = 'text/csv'
    response.headers['Content-Disposition'] = 'attachment; filename=
%s' % filename
    mail.send('[email protected]', 'Message
subject',attachments = Mail.attachment())
    #attachments = Mail.Attachment(stream.getvalue(),
filename=filename,content_id='text/csv')
    return stream.getvalue()

def render_report_csv(ofile, result, colnames=[], null='<NULL>',
*args, **kwargs):
    delimiter = kwargs.get('delimiter', ',')
    quotechar = kwargs.get('quotechar', '"')
    quoting = kwargs.get('quoting', csv.QUOTE_MINIMAL)
    writer = csv.writer(ofile, delimiter=delimiter,
                        quotechar=quotechar, quoting=quoting)
    writer.writerow(colnames)

    for record in result:
        row = []
        for col in colnames:
            if '.' in col:
                (t, f) = col.split('.')
                row.append(record.get(t).get(f))
            else:
                row.append(record.get(col))
        writer.writerow(row)

Is it possible to use the stream and attach it in the mail as a csv?
Please provide your suggestion

Reply via email to