# Path: djenga/csv/unicode_csv_writer.py
# class UnicodeCsvWriter:
#     def __init__(self, f, dialect=csv.excel, **kwds):
#         self.queue = StringIO()
#         self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
#         self.stream = f
# 
#     def writerow(self, row):
#         """
#         writerow(unicode) -> None
#         This function takes a Unicode string and encodes it to the output.
#         """
#         data = []
#         basestring_type = six.string_types[0]
#         for value in row:
#             if not isinstance(value, basestring_type):
#                 value = '%s' % (value,)
#             data.append(value.encode('utf-8'))
#         self.writer.writerow(data)
#         data = self.queue.getvalue()
#         data = data.decode('utf-8')
#         self.stream.write(data)
#         self.queue.truncate(0)
# 
#     def writerows(self, rows):
#         for row in rows:
#             self.writerow(row)


import os
from io import BytesIO
from io import StringIO
from wsgiref.util import FileWrapper
from zipfile import ZipFile, ZIP_DEFLATED
from django.http import HttpResponse
from django.http import StreamingHttpResponse
from django.views.generic import View
from ..csv.unicode_csv_writer import UnicodeCsvWriter


class CsvViewMixin:
    """
    This class offers several utility functions to
    make it easier to return a CSV response to a user.
    To use:
        1.  Add this class as a mixin to any class-based view
        2.  Call self.initialize_response with a proposed download filename
        3.  write data to the CSV response with self.writerow
        4.  return self.response from the get or post function to send the
            CSV to the user
    """
    def __init__(self, **kwargs):
        super(CsvViewMixin, self).__init__(**kwargs)
        self.response = None
        """:type HttpResponse"""
        self.writer = None
        """:type csv.writer"""

    def initialize_response(self, filename):
        """
        Prepares the HttpResponse that will be used
        to contain the CSV data
        :param filename: the default filename for the user
        when he/she downloads the file.
        """
        key = 'Content-Disposition'
        self.response = HttpResponse(content_type='text/csv')
        self.response[key] = f'attachment; filename="{filename}"'

# Path: djenga/csv/unicode_csv_writer.py
# class UnicodeCsvWriter:
#     def __init__(self, f, dialect=csv.excel, **kwds):
#         self.queue = StringIO()
#         self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
#         self.stream = f
# 
#     def writerow(self, row):
#         """
#         writerow(unicode) -> None
#         This function takes a Unicode string and encodes it to the output.
#         """
#         data = []
#         basestring_type = six.string_types[0]
#         for value in row:
#             if not isinstance(value, basestring_type):
#                 value = '%s' % (value,)
#             data.append(value.encode('utf-8'))
#         self.writer.writerow(data)
#         data = self.queue.getvalue()
#         data = data.decode('utf-8')
#         self.stream.write(data)
#         self.queue.truncate(0)
# 
#     def writerows(self, rows):
#         for row in rows:
#             self.writerow(row)


# Path: djenga/views/csv_views.py
import os
from io import BytesIO
from io import StringIO
from wsgiref.util import FileWrapper
from zipfile import ZipFile, ZIP_DEFLATED
from django.http import HttpResponse
from django.http import StreamingHttpResponse
from django.views.generic import View
from ..csv.unicode_csv_writer import UnicodeCsvWriter



class CsvViewMixin:
    """
    This class offers several utility functions to
    make it easier to return a CSV response to a user.
    To use:
        1.  Add this class as a mixin to any class-based view
        2.  Call self.initialize_response with a proposed download filename
        3.  write data to the CSV response with self.writerow
        4.  return self.response from the get or post function to send the
            CSV to the user
    """
    def __init__(self, **kwargs):
        super(CsvViewMixin, self).__init__(**kwargs)
        self.response = None
        """:type HttpResponse"""
        self.writer = None
        """:type csv.writer"""

    def initialize_response(self, filename):
        """
        Prepares the HttpResponse that will be used
        to contain the CSV data
        :param filename: the default filename for the user
        when he/she downloads the file.
        """
        key = 'Content-Disposition'
        self.response = HttpResponse(content_type='text/csv')
        self.response[key] = f'attachment; filename="{filename}"'

        self.writer = UnicodeCsvWriter(self.response)