Merge "Removing check for unsupported Django version"
This commit is contained in:
commit
8d008a73c4
|
@ -17,8 +17,8 @@ from csv import writer # noqa
|
||||||
|
|
||||||
|
|
||||||
from django.http import HttpResponse # noqa
|
from django.http import HttpResponse # noqa
|
||||||
|
from django.http import StreamingHttpResponse # noqa
|
||||||
from django import template as django_template
|
from django import template as django_template
|
||||||
from django import VERSION # noqa
|
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from six import StringIO
|
from six import StringIO
|
||||||
|
@ -99,47 +99,42 @@ class BaseCsvResponse(CsvDataMixin, HttpResponse):
|
||||||
return []
|
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):
|
self.streaming_content = self.get_content()
|
||||||
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._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):
|
self.write_csv_header()
|
||||||
buf = self.out.getvalue()
|
yield self.buffer()
|
||||||
self.out.truncate(0)
|
|
||||||
return buf
|
|
||||||
|
|
||||||
def get_content(self):
|
for row in self.get_row_data():
|
||||||
if self.header:
|
self.write_csv_row(row)
|
||||||
self.out.write(self.encode(self.header))
|
|
||||||
|
|
||||||
self.write_csv_header()
|
|
||||||
yield self.buffer()
|
yield self.buffer()
|
||||||
|
|
||||||
for row in self.get_row_data():
|
def get_row_data(self):
|
||||||
self.write_csv_row(row)
|
return []
|
||||||
yield self.buffer()
|
|
||||||
|
|
||||||
def get_row_data(self):
|
|
||||||
return []
|
|
||||||
|
|
Loading…
Reference in New Issue