Esempio n. 1
0
def __export_csv(data_table, filename="tabella"):
    # Create the HttpResponse object with the appropriate CSV header.
    response = http.HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename="%s_%s.csv"' % (filename, datetime.datetime.today().strftime("%d-%m-%Y"))

    export_list = user_settings.settings_columView('export_table')

    response.write("\xEF\xBB\xBF")
    writer = tools.UnicodeWriter(response, delimiter=';')
    writer.writerow(["%s" % j.replace('_', ' ').capitalize() for j in export_list])

    for item_dict in data_table:
        l = []
        for i in export_list:
            l.append(data_render.formatFields(item_dict, i, default_text="-"))

        writer.writerow(l)

    return response
Esempio n. 2
0
def __export_xls(data_table, filename="tabella"):
    # Create the HttpResponse object with the appropriate CSV header.
    response = http.HttpResponse(mimetype='application/ms-excel; charset=utf-8')
    response['Content-Disposition'] = 'attachment; filename="%s_%s.xls"' % (filename, datetime.datetime.today().strftime("%d-%m-%Y"))

    book = xlwt.Workbook(encoding='utf-8')
    sheet = book.add_sheet('Elenco')

    #Add header
    export_list = user_settings.settings_columView('export_table')
    for colum,j in enumerate(export_list):
        sheet.write(0, colum, "%s" % j.replace('_', ' ').capitalize())

    #Write table
    for row,i in enumerate(data_table):
        for colum,j in enumerate(export_list):
            #we should skip the header row.
            sheet.write(row + 1, colum, data_render.formatFields(i,j, default_text="-"))

    book.save(response)
    return response
Esempio n. 3
0
def tag_replace(m, item_dict):
    k = m.group()
    field_name = k[1:-1].lower()
    field = data_render.formatFields(item_dict, field_name, default_text="-")
    return ''.join([c if ord(c) < 128 else u'\\u' + unicode(ord(c)) + u'?' for c in unicode(field)])