From 19468fcc2148513d5e716f17fc216005ef00adb4 Mon Sep 17 00:00:00 2001 From: David Lyle Date: Fri, 2 Oct 2015 09:32:31 -0600 Subject: [PATCH] Removing check for unsupported Django version Horizon no longer supports Django 1.5 and less, so this logic is unnecessary. Closes-Bug: #1502206 Change-Id: I7b892760e23f697c6af0ae005f56e9566beb069e --- horizon/utils/csvbase.py | 65 +++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/horizon/utils/csvbase.py b/horizon/utils/csvbase.py index cdbde63555..7d32039a5f 100644 --- a/horizon/utils/csvbase.py +++ b/horizon/utils/csvbase.py @@ -17,8 +17,8 @@ from csv import writer # noqa from django.http import HttpResponse # noqa +from django.http import StreamingHttpResponse # noqa from django import template as django_template -from django import VERSION # noqa import six from six import StringIO @@ -99,47 +99,42 @@ class BaseCsvResponse(CsvDataMixin, HttpResponse): return [] -if VERSION >= (1, 5, 0): +class BaseCsvStreamingResponse(CsvDataMixin, StreamingHttpResponse): - from django.http import StreamingHttpResponse # noqa + """Base CSV Streaming class. Provides streaming response for CSV data.""" - class BaseCsvStreamingResponse(CsvDataMixin, StreamingHttpResponse): + def __init__(self, request, template, context, content_type, **kwargs): + super(BaseCsvStreamingResponse, self).__init__() + self['Content-Disposition'] = 'attachment; filename="%s"' % ( + kwargs.get("filename", "export.csv"),) + self['Content-Type'] = content_type + self.context = context + self.header = None + if template: + # Display some header info if provided as a template + header_template = django_template.loader.get_template(template) + context = django_template.RequestContext(request, self.context) + self.header = header_template.render(context) - """Base CSV Streaming class. Provides streaming response for CSV data. - """ + self._closable_objects.append(self.out) - def __init__(self, request, template, context, content_type, **kwargs): - super(BaseCsvStreamingResponse, self).__init__() - self['Content-Disposition'] = 'attachment; filename="%s"' % ( - kwargs.get("filename", "export.csv"),) - self['Content-Type'] = content_type - self.context = context - self.header = None - if template: - # Display some header info if provided as a template - header_template = django_template.loader.get_template(template) - context = django_template.RequestContext(request, self.context) - self.header = header_template.render(context) + self.streaming_content = self.get_content() - self._closable_objects.append(self.out) + def buffer(self): + buf = self.out.getvalue() + self.out.truncate(0) + return buf - self.streaming_content = self.get_content() + def get_content(self): + if self.header: + self.out.write(self.encode(self.header)) - def buffer(self): - buf = self.out.getvalue() - self.out.truncate(0) - return buf + self.write_csv_header() + yield self.buffer() - def get_content(self): - if self.header: - self.out.write(self.encode(self.header)) - - self.write_csv_header() + for row in self.get_row_data(): + self.write_csv_row(row) yield self.buffer() - for row in self.get_row_data(): - self.write_csv_row(row) - yield self.buffer() - - def get_row_data(self): - return [] + def get_row_data(self): + return []