"""
    pyexcel.plugin.renderers.django
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Export data into django models

    :copyright: (c) 2015-2017 by Onni Software Ltd.
    :license: New BSD License
"""
from pyexcel_io import save_data
import pyexcel_io.database.common as django

from pyexcel._compact import OrderedDict
from pyexcel.renderer import DbRenderer
import pyexcel.internal.common as common


NO_COLUMN_NAMES = "Only sheet with column names is accepted"


class DjangoRenderer(DbRenderer):
    """Import data into database"""
    def render_sheet_to_stream(self, model, sheet, init=None, mapdict=None,
                               **keywords):
        headers = common.get_sheet_headers(sheet)
        importer = django.DjangoModelImporter()
        adapter = django.DjangoModelImportAdapter(model)
        adapter.column_names = headers
        adapter.column_name_mapping_dict = mapdict
        adapter.row_initializer = init
        importer.append(adapter)
        save_data(importer, {adapter.get_name(): sheet.get_internal_array()},
                  file_type=self._file_type, **keywords)

    def render_book_to_stream(self, models, thebook,
                              inits=None, mapdicts=None,
                              batch_size=None, **keywords):
        book = thebook
        colnames_array = common.get_book_headers_in_array(thebook)
        new_models = [model for model in models if model is not None]
        initializers = inits
        if initializers is None:
            initializers = [None] * len(new_models)
        if mapdicts is None:
            mapdicts = [None] * len(new_models)
        scattered = zip(new_models, colnames_array, mapdicts, initializers)

        importer = django.DjangoModelImporter()
        for each_model in scattered:
            adapter = django.DjangoModelImportAdapter(each_model[0])
            adapter.column_names = each_model[1]
            adapter.column_name_mapping_dict = each_model[2]
            adapter.row_initializer = each_model[3]
            importer.append(adapter)
        to_store = OrderedDict()
        for sheet in thebook:
            # due book.to_dict() brings in column_names
            # which corrupts the data
            to_store[sheet.name] = sheet.get_internal_array()
        save_data(importer, to_store, file_type=self._file_type,
                  batch_size=batch_size, **keywords)

@@ -1150,19 +1150,16 @@
 models, 
-the
 book,%0A  
@@ -1277,31 +1277,8 @@
 s):%0A
-        book = thebook%0A
     
@@ -1331,19 +1331,16 @@
 n_array(
-the
 book)%0A  
@@ -2082,19 +2082,16 @@
 heet in 
-the
 book:%0A  

remove unused variables