def get(self, request): # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="somefilename.csv"' writer = csv.writer(response) writer.writerow(self.headers) # writer.writerow([ # 'ID', # 'Currency', # 'Source', # 'Buy', # 'Sale', # 'Created', # ]) for rate in Rate.objects.all().iterator(): writer.writerow( [display(rate, header) for header in self.__class__.headers]) # for rate in Rate.objects.all().iterator(): # writer.writerow([ # rate.id, # rate.get_currency_display(), # rate.get_source_display(), # rate.buy, # rate.sale, # rate.created, # ]) return response
def get(self, request): output = BytesIO() queryset = Rate.objects.all().iterator() workbook = xlsxwriter.Workbook(output) worksheet = workbook.add_worksheet("rates") columns = self.__class__.HEADERS style = workbook.add_format({'bold': True}) for col, elem in enumerate(columns): # sheet headers worksheet.write(0, col, elem, style) for row, rate in enumerate(queryset, start=1): # sheet rows for col, item in enumerate(self.__class__.HEADERS): worksheet.write(row, col, display(rate, item)) workbook.close() output.seek(0) response = HttpResponse( output, content_type= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) response['Content-Disposition'] = 'attachment; filename="rates.xlsx"' return response
def get(self, request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; filename="somefilename.csv"' writer = csv.writer(response) writer.writerow(self.__class__.headers) for rate in Rate.objects.all().iterator(): writer.writerow([display(rate, header) for header in self.__class__.headers]) return response
def get(self, request): response = self.prepare_response writer = csv.writer(response) writer.writerow(self.__class__.HEADERS) for rate in self.queryset: values = [] for attr in self.__class__.HEADERS: values.append(display(rate, attr)) writer.writerow(values) return response
def get(self, request): output = io.BytesIO() workbook = xlsxwriter.Workbook(output, options={'remove_timezone': True}) worksheet = workbook.add_worksheet('rates') worksheet.set_column(0, 5, width=15) row = 0 col = 0 for attr in self.__class__.HEADERS: worksheet.write_string(row, col, attr) col += 1 row = 1 col = 0 for rate in self.queryset: for attr in self.__class__.HEADERS: worksheet.write(row, col, display(rate, attr)) if attr == 'type_rate': row += 1 col = -1 if attr == 'created': dt = display(rate, attr).strftime("%m/%d/%Y, %H:%M") worksheet.write_string(row, col, dt) col += 1 workbook.close() output.seek(0) filename = 'rates.xlsx' response = HttpResponse( output, content_type= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ) response['Content-Disposition'] = f'attachment; filename={filename}' return response
def get(self, request): response = self.prepare_response workbook = xlsxwriter.Workbook(response, {'in_memory': True}) worksheet = workbook.add_worksheet() for col, data in enumerate(self.__class__.HEADERS): worksheet.write(0, col, data) for col, rate in enumerate(self.queryset, start=1): values = [] for attr in self.__class__.HEADERS: values.append(display(rate, attr)) for row, value in enumerate(values): worksheet.write(col, row, value) workbook.close() return response
def get(self, request): # Create the HttpResponse object with the appropriate CSV header. response = self.get_response() writer = csv.writer(response) writer.writerow(self.__class__.HEADERS) for rate in self.queryset: values = [] for attr in self.__class__.HEADERS: values.append(display(rate, attr)) writer.writerow(values) return response
def get(self, request): queryset = Rate.objects.all().iterator() response = self.get_response() writer = csv.writer(response) writer.writerow(self.__class__.HEADERS) for rate in queryset: values = [] for attr in self.__class__.HEADERS: values.append(display(rate, attr)) writer.writerow(values) return response
def get(self, request): # Create the HttpResponse object with the appropriate CSV header. response = HttpResponse(content_type='text/csv') response[ 'Content-Disposition'] = 'attachment; filename="somefilename.csv"' writer = csv.writer(response) from time import sleep sleep(15) writer.writerow(self.headers) for rate in Rate.objects.all().iterator(): writer.writerow( [display(rate, header) for header in self.__class__.headers]) return response
def get(self, request): """ Downloads all movies as Excel file with a single worksheet """ # movie_queryset = Movie.objects.all() response = HttpResponse( content_type= 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', ) today = datetime.now().strftime("%Y-%m-%d") response[ 'Content-Disposition'] = f'attachment; filename={today}-rates.xlsx' workbook = Workbook() # Get active worksheet/tab worksheet = workbook.active worksheet.title = 'Movies' # Define the titles for columns row_num = 1 # Assign the titles for each cell of the header for col_num, column_title in enumerate(self.columns, 1): cell = worksheet.cell(row=row_num, column=col_num) cell.value = column_title # Iterate through all movies for rate in self.queryset: row_num += 1 # row row = [] for attr in self.__class__.columns: row.append(display(rate, attr)) # Assign the data for each cell of the row for col_num, cell_value in enumerate(row, 1): cell = worksheet.cell(row=row_num, column=col_num) cell.value = cell_value workbook.save(response) return response
def get(self, request): response = self.get_response book = xlsxwriter.Workbook(response, {'in_memory': True}) sheet = book.add_worksheet() for i, column in enumerate(self.__class__.HEADERS): sheet.write(0, i, column) for i, amount in enumerate(self.queryset, start=1): values = [] for attr in self.__class__.HEADERS: values.append(display(amount, attr)) for j, value in enumerate(values): sheet.write(i, j, value) book.close() return response