Пример #1
0
 def test_reading_from_more_models(self):
     model1 = FakeDjangoModel()
     model2 = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter1 = DjangoModelImportAdapter(model1)
     adapter1.column_names = self.content['Sheet1'][0]
     adapter2 = DjangoModelImportAdapter(model2)
     adapter2.column_names = self.content['Sheet2'][0]
     importer.append(adapter1)
     importer.append(adapter2)
     to_store = {
         adapter1.get_name(): self.content['Sheet1'][1:],
         adapter2.get_name(): self.content['Sheet2'][1:]
     }
     save_data(importer, to_store, file_type=DB_DJANGO)
     assert model1.objects.objs == self.result1
     assert model2.objects.objs == self.result2
     model1._meta.model_name = "Sheet1"
     model2._meta.model_name = "Sheet2"
     model1._meta.update(["X", "Y", "Z"])
     model2._meta.update(["A", "B", "C"])
     exporter = DjangoModelExporter()
     adapter1 = DjangoModelExportAdapter(model1)
     adapter2 = DjangoModelExportAdapter(model2)
     exporter.append(adapter1)
     exporter.append(adapter2)
     reader = DjangoBookReader()
     reader.open_content(exporter)
     data = reader.read_all()
     for key in data.keys():
         data[key] = list(data[key])
     assert data == self.content
Пример #2
0
 def test_reading_from_more_models(self):
     model1=FakeDjangoModel()
     model2=FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter1 = DjangoModelImportAdapter(model1)
     adapter1.set_column_names(self.content['Sheet1'][0])
     adapter2 = DjangoModelImportAdapter(model2)
     adapter2.set_column_names(self.content['Sheet2'][0])
     importer.append(adapter1)
     importer.append(adapter2)
     to_store = {
        adapter1.get_name(): self.content['Sheet1'][1:],
        adapter2.get_name(): self.content['Sheet2'][1:]
     }
     save_data(importer, to_store, file_type=DB_DJANGO)
     assert model1.objects.objs == self.result1
     assert model2.objects.objs == self.result2
     model1._meta.model_name = "Sheet1"
     model2._meta.model_name = "Sheet2"
     model1._meta.update(["X", "Y", "Z"])
     model2._meta.update(["A", "B", "C"])
     exporter = DjangoModelExporter()
     adapter1 = DjangoModelExportAdapter(model1)
     adapter2 = DjangoModelExportAdapter(model2)
     exporter.append(adapter1)
     exporter.append(adapter2)
     reader = DjangoBookReader()
     reader.open_content(exporter)
     data = reader.read_all()
     for key in data.keys():
         data[key] = list(data[key])
     assert data == self.content
 def test_sheet_save_to_django_model(self):
     model = FakeDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()
     eq_(model.objects.objs, self.result)
Пример #4
0
 def test_sheet_save_to_django_model(self):
     model = FakeDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()
     eq_(model.objects.objs, self.result)
Пример #5
0
 def test_sheet_save_to_django_model_with_empty_array(self):
     model = FakeDjangoModel()
     data = [["X", "Y", "Z"], ['', '', ''], [1, 2, 3], [4, 5, 6]]
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(data[1:])
     writer.close()
     assert model.objects.objs == self.result
Пример #6
0
 def test_mapping_array(self):
     data2 = [["A", "B", "C"], [1, 2, 3], [4, 5, 6]]
     model = FakeDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = data2[0]
     adapter.column_name_mapping_dict = ["X", "Y", "Z"]
     writer = DjangoModelWriter(adapter)
     writer.write_array(data2[1:])
     writer.close()
     eq_(model.objects.objs, self.result)
Пример #7
0
 def setUp(self):
     self.data = [["X", "Y", "Z"], [1, 2, 3], [4, 5, 6]]
     self.result = [{'Y': 2, 'X': 1, 'Z': 3}, {'Y': 5, 'X': 4, 'Z': 6}]
     self.model = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(self.model)
     adapter.column_names = self.data[0]
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): self.data[1:]},
               file_type=DB_DJANGO)
     assert self.model.objects.objs == self.result
     self.model._meta.update(["X", "Y", "Z"])
Пример #8
0
 def test_load_sheet_from_django_model(self):
     model=FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(model)
     adapter.set_column_names(self.data[0])
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): self.data[1:]}, file_type=DB_DJANGO)
     assert model.objects.objs == self.result
     model._meta.update(["X", "Y", "Z"])
     reader = DjangoModelReader(model)
     data = reader.to_array()
     assert list(data) == self.data
Пример #9
0
 def test_load_sheet_from_django_model(self):
     model = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): self.data[1:]},
               file_type=DB_DJANGO)
     assert model.objects.objs == self.result
     model._meta.update(["X", "Y", "Z"])
     reader = DjangoModelReader(model)
     data = reader.to_array()
     assert list(data) == self.data
 def test_mapping_array(self):
     data2 = [
         ["A", "B", "C"],
         [1, 2, 3],
         [4, 5, 6]
     ]
     model = FakeDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = data2[0]
     adapter.column_name_mapping_dict = ["X", "Y", "Z"]
     writer = DjangoModelWriter(adapter)
     writer.write_array(data2[1:])
     writer.close()
     eq_(model.objects.objs, self.result)
 def test_sheet_save_to_django_model_with_empty_array(self):
     model = FakeDjangoModel()
     data = [
         ["X", "Y", "Z"],
         ['', '', ''],
         [1, 2, 3],
         [4, 5, 6]
     ]
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(data[1:])
     writer.close()
     assert model.objects.objs == self.result
 def test_sheet_save_to_django_model(self):
     model = FakeExceptionDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()
     # now raise excpetion
     model = FakeExceptionDjangoModel(raiseException=True)
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()
Пример #13
0
    def write_data(self, book):
        new_models = [model for model in self.models if model is not None]
        batch_size = self.keywords.get(params.BATCH_SIZE, None)
        initializers = self.keywords.get(params.INITIALIZERS, None)
        if initializers is None:
            initializers = [None] * len(new_models)
        mapdicts = self.keywords.get(params.MAPDICTS, None)
        if mapdicts is None:
            mapdicts = [None] * len(new_models)
        for sheet in book:
            if len(sheet.colnames) == 0:
                sheet.name_columns_by_row(0)
        colnames_array = [sheet.colnames for sheet in book]
        scattered = zip(new_models, colnames_array, mapdicts, initializers)

        importer = DjangoModelImporter()
        for each_model in scattered:
            adapter = DjangoModelImportAdapter(each_model[0])
            adapter.set_column_names(each_model[1])
            adapter.set_column_name_mapping_dict(each_model[2])
            adapter.set_row_initializer(each_model[3])
            importer.append(adapter)
        to_store = OrderedDict()
        for sheet_name in book.sheet_names():
            # due book.to_dict() brings in column_names
            # which corrupts the data
            to_store[sheet_name] = book[sheet_name].array
        save_data(importer,
                  to_store,
                  file_type=DB_DJANGO,
                  batch_size=batch_size)
    def test_sheet_save_to_django_model_3(self):
        model = FakeDjangoModel()

        def wrapper(row):
            row[0] = row[0] + 1
            return row
        adapter = DjangoModelImportAdapter(model)
        adapter.column_names = self.data[0]
        adapter.row_initializer = wrapper
        writer = DjangoModelWriter(adapter)
        writer.write_array(self.data[1:])
        writer.close()
        assert model.objects.objs == [
            {'Y': 2, 'X': 2, 'Z': 3},
            {'Y': 5, 'X': 5, 'Z': 6}
        ]
Пример #15
0
    def test_sheet_save_to_django_model_skip_me(self):
        model = FakeDjangoModel()

        def wrapper(row):
            if row[0] == 4:
                return None
            else:
                return row

        adapter = DjangoModelImportAdapter(model)
        adapter.column_names = self.data[0]
        adapter.row_initializer = wrapper
        writer = DjangoModelWriter(adapter)
        writer.write_array(self.data[1:])
        writer.close()
        assert model.objects.objs == [{'Y': 2, 'X': 1, 'Z': 3}]
Пример #16
0
 def write_data(self, sheet):
     headers = sheet.colnames
     if len(headers) == 0:
         headers = sheet.rownames
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(self.model)
     adapter.set_column_names(headers)
     adapter.set_column_name_mapping_dict(
         self.keywords.get(params.MAPDICT, None))
     adapter.set_row_initializer(self.keywords.get(params.INITIALIZER,
                                                   None))
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): sheet.array},
               file_type=DB_DJANGO)
Пример #17
0
    def write_data(self, book):
        new_models = [model for model in self.models if model is not None]
        batch_size = self.keywords.get(params.BATCH_SIZE, None)
        initializers = self.keywords.get(params.INITIALIZERS, None)
        if initializers is None:
            initializers = [None] * len(new_models)
        mapdicts = self.keywords.get(params.MAPDICTS, None)
        if mapdicts is None:
            mapdicts = [None] * len(new_models)
        for sheet in book:
            if len(sheet.colnames) == 0:
                sheet.name_columns_by_row(0)
        colnames_array = [sheet.colnames for sheet in book]
        scattered = zip(new_models, colnames_array, mapdicts, initializers)

        importer = DjangoModelImporter()
        for each_model in scattered:
            adapter = DjangoModelImportAdapter(each_model[0])
            adapter.set_column_names(each_model[1])
            adapter.set_column_name_mapping_dict(each_model[2])
            adapter.set_row_initializer(each_model[3])
            importer.append(adapter)
        to_store = OrderedDict()
        for sheet_name in book.sheet_names():
            # due book.to_dict() brings in column_names
            # which corrupts the data
            to_store[sheet_name] = book[sheet_name].array
        save_data(importer, to_store, file_type=DB_DJANGO,
                  batch_size=batch_size)
Пример #18
0
 def write_data(self, sheet):
     headers = sheet.colnames
     if len(headers) == 0:
         headers = sheet.rownames
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(self.model)
     adapter.set_column_names(headers)
     adapter.set_column_name_mapping_dict(self.keywords.get(params.MAPDICT, None))
     adapter.set_row_initializer(self.keywords.get(params.INITIALIZER, None))
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): sheet.array}, file_type=DB_DJANGO, **self.keywords)
Пример #19
0
    def test_load_and_format_sheet_from_django_model(self):
        model = FakeDjangoModel()
        importer = DjangoModelImporter()
        adapter = DjangoModelImportAdapter(model)
        adapter.column_names = self.data[0]
        importer.append(adapter)
        save_data(importer, {adapter.get_name(): self.data[1:]},
                  file_type=DB_DJANGO)
        assert model.objects.objs == self.result
        model._meta.update(["X", "Y", "Z"])

        def row_renderer(row):
            return [str(element) for element in row]

        # the key point of this test case
        reader = DjangoModelReader(model, row_renderer=row_renderer)
        data = reader.to_array()
        expected = [["X", "Y", "Z"], ['1', '2', '3'], ['4', '5', '6']]
        eq_(list(data), expected)
 def setUp(self):
     self.data = [
         ["X", "Y", "Z"],
         [1, 2, 3],
         [4, 5, 6]
     ]
     self.result = [
         {'Y': 2, 'X': 1, 'Z': 3},
         {'Y': 5, 'X': 4, 'Z': 6}
     ]
     self.model = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(self.model)
     adapter.column_names = self.data[0]
     importer.append(adapter)
     save_data(importer, {adapter.get_name(): self.data[1:]},
               file_type=DB_DJANGO)
     assert self.model.objects.objs == self.result
     self.model._meta.update(["X", "Y", "Z"])
Пример #21
0
 def test_special_case_where_only_one_model_used(self):
     model1 = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(model1)
     adapter.column_names = self.content['Sheet1'][0]
     importer.append(adapter)
     to_store = {
         adapter.get_name(): self.content['Sheet1'][1:],
         "Sheet2": self.content['Sheet2'][1:]
     }
     save_data(importer, to_store, file_type=DB_DJANGO)
     assert model1.objects.objs == self.result1
     model1._meta.model_name = "Sheet1"
     model1._meta.update(["X", "Y", "Z"])
     exporter = DjangoModelExporter()
     adapter = DjangoModelExportAdapter(model1)
     exporter.append(adapter)
     reader = DjangoBookReader()
     reader.open_content(exporter)
     data = reader.read_all()
     assert list(data['Sheet1']) == self.content['Sheet1']
 def test_special_case_where_only_one_model_used(self):
     model1 = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter = DjangoModelImportAdapter(model1)
     adapter.column_names = self.content['Sheet1'][0]
     importer.append(adapter)
     to_store = {
         adapter.get_name(): self.content['Sheet1'][1:],
         "Sheet2": self.content['Sheet2'][1:]
     }
     save_data(importer, to_store, file_type=DB_DJANGO)
     assert model1.objects.objs == self.result1
     model1._meta.model_name = "Sheet1"
     model1._meta.update(["X", "Y", "Z"])
     exporter = DjangoModelExporter()
     adapter = DjangoModelExportAdapter(model1)
     exporter.append(adapter)
     reader = DjangoBookReader()
     reader.open_content(exporter)
     data = reader.read_all()
     assert list(data['Sheet1']) == self.content['Sheet1']
    def test_load_and_format_sheet_from_django_model(self):
        model = FakeDjangoModel()
        importer = DjangoModelImporter()
        adapter = DjangoModelImportAdapter(model)
        adapter.column_names = self.data[0]
        importer.append(adapter)
        save_data(importer, {adapter.get_name(): self.data[1:]},
                  file_type=DB_DJANGO)
        assert model.objects.objs == self.result
        model._meta.update(["X", "Y", "Z"])

        def row_renderer(row):
            return [str(element) for element in row]
        # the key point of this test case
        reader = DjangoModelReader(model,
                                   row_renderer=row_renderer)
        data = reader.to_array()
        expected = [
            ["X", "Y", "Z"],
            ['1', '2', '3'],
            ['4', '5', '6']
        ]
        eq_(list(data), expected)
Пример #24
0
 def test_save_to_more_models(self):
     model1 = FakeDjangoModel()
     model2 = FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter1 = DjangoModelImportAdapter(model1)
     adapter1.column_names = self.content['Sheet1'][0]
     adapter2 = DjangoModelImportAdapter(model2)
     adapter2.column_names = self.content['Sheet2'][0]
     importer.append(adapter1)
     importer.append(adapter2)
     to_store = {
         adapter1.get_name(): self.content['Sheet1'][1:],
         adapter2.get_name(): self.content['Sheet2'][1:]
     }
     writer = DjangoBookWriter()
     writer.open_content(importer)
     writer.write(to_store)
     writer.close()
     assert model1.objects.objs == self.result1
     assert model2.objects.objs == self.result2
Пример #25
0
 def test_save_to_more_models(self):
     model1=FakeDjangoModel()
     model2=FakeDjangoModel()
     importer = DjangoModelImporter()
     adapter1 = DjangoModelImportAdapter(model1)
     adapter1.set_column_names(self.content['Sheet1'][0])
     adapter2 = DjangoModelImportAdapter(model2)
     adapter2.set_column_names(self.content['Sheet2'][0])
     importer.append(adapter1)
     importer.append(adapter2)
     to_store = {
         adapter1.get_name(): self.content['Sheet1'][1:],
         adapter2.get_name(): self.content['Sheet2'][1:]
     }
     writer = DjangoBookWriter()
     writer.open_content(importer)
     writer.write(to_store)
     writer.close()
     assert model1.objects.objs == self.result1
     assert model2.objects.objs == self.result2
Пример #26
0
 def test_sheet_save_to_django_model(self):
     model = FakeExceptionDjangoModel()
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()
     # now raise excpetion
     model = FakeExceptionDjangoModel(raiseException=True)
     adapter = DjangoModelImportAdapter(model)
     adapter.column_names = self.data[0]
     writer = DjangoModelWriter(adapter)
     writer.write_array(self.data[1:])
     writer.close()