Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added writer_kwargs

  • Loading branch information...
commit 7d53e098636b02bac49d10a17076f8169563aa91 1 parent 52d8266
@rasca authored
Showing with 13 additions and 3 deletions.
  1. +7 −2 enhanced_cbv/response.py
  2. +6 −1 enhanced_cbv/views/base.py
View
9 enhanced_cbv/response.py
@@ -69,11 +69,16 @@ class CSVTemplateResponse(TemplateResponse):
def __init__(self, request, template, context=None,
mimetype='text/csv', status=None, content_type=None,
- current_app=None, filename=None, rows=None):
+ current_app=None, filename=None, rows=None,
+ writer_kwargs=None):
"""Simple adds a default mimetype for CSVs and a filename"""
self.filename = filename
self.rows = rows
+ if writer_kwargs:
+ self.writer_kwargs = writer_kwargs
+ else:
+ self.writer_kwargs = {}
super(CSVTemplateResponse, self).__init__(request,
template, context, mimetype, status, content_type)
@@ -86,7 +91,7 @@ def render(self):
# File pointer needed to create the CSV in memory
buffer = StringIO()
- writer = UnicodeWriter(buffer)
+ writer = UnicodeWriter(buffer, **self.writer_kwargs)
for row in self.rows:
writer.writerow([unicode(value).encode('utf-8') for value
View
7 enhanced_cbv/views/base.py
@@ -32,6 +32,7 @@ class CSVTemplateResponseMixin(TemplateResponseMixin):
response_class = CSVTemplateResponse
filename = None
+ writer_kwargs = None
def get_filename(self):
if self.filename is None:
@@ -41,6 +42,9 @@ def get_filename(self):
else:
return self.filename
+ def get_writer_kwargs(self):
+ return self.writer_kwargs
+
def render_to_response(self, *args, **kwargs):
"""
Returns a response with a template rendered with the given context.
@@ -50,7 +54,8 @@ def render_to_response(self, *args, **kwargs):
rows.append(self.get_row(obj))
kwargs.update({
'rows': rows,
- 'filename': self.get_filename()
+ 'filename': self.get_filename(),
+ 'writer_kwargs': self.get_writer_kwargs(),
})
return super(CSVTemplateResponseMixin, self).render_to_response(*args, **kwargs)
Please sign in to comment.
Something went wrong with that request. Please try again.