def __init__(self, file_path): content = open(file_path, 'r+b') HttpResponse.__init__(self, content=content, content_type='application/zip') self['Content-Disposition'] = 'attachment; filename={}'.format( to_ascii(file_path.split(os.sep)[-1]))
def __init__(self, rows, name='Listagem'): import csv file_name = mktemp() with open(file_name, 'w', encoding='iso8859-1') as output: writer = csv.writer(output) for row in rows: writer.writerow([col for col in row]) # .encode('iso8859-1') HttpResponse.__init__(self, content=open(file_name, 'r').read(), content_type='application/csv') self['Content-Disposition'] = 'attachment; filename={}.xls'.format(to_ascii(name))
def pre_save(self, model_instance, add): search_text = [] for attr_name in get_metadata(model_instance, 'search_fields'): tokens = attr_name.split('__') if len(tokens) == 1: if attr_name != 'ascii': val = getattr2(model_instance, attr_name) if val: search_text.append(str(to_ascii(val).upper().strip())) return ' '.join(search_text)
def __init__(self, data, name='Listagem'): import xlwt wb = xlwt.Workbook(encoding='iso8859-1') for title, rows in data: sheet = wb.add_sheet(title) for row_idx, row in enumerate(rows): for col_idx, label in enumerate(row): sheet.write(row_idx, col_idx, label=label) file_name = mktemp() wb.save(file_name) HttpResponse.__init__(self, content=open(file_name, 'rb').read(), content_type='application/vnd.ms-excel') self['Content-Disposition'] = 'attachment; filename={}.xls'.format(to_ascii(name))