def test_minimal(self): my_data = [ { 'msg': "My first Cell", 'id': 1, 'when': now, }, { 'msg': "My second Cell", 'id': 2, 'when': now, }, ] mm_doc = mm.Document(my_data) mm_doc.set_name("My Sheet 1") mm_child = mm.Document(my_data) mm_child.set_name("My Sheet 2") mm_doc.add_child(mm_child) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_multi.xls", "wb") as f: f.write(str) self.check("tests/generated_files/test_multi.xls", my_data)
def excel_sheet(request): if request.method == 'GET': try: filename = request.GET['filename'] except: return render(request, "bad.html", {'type':'no filename in excel GET'}) if not check_filename(filename): return render(request, "bad.html", {'type':'bad filename'}) try: with open('%s/media/%s' % (path_to_xpert_su, filename), "r") as fp: data = json.load(fp) except: return render(request, "bad.html", {'type':'no such JSON file'}) data['intermed'].insert(0, 'Intervention/Scenario') sheets = ['year1','year2','year3','year4','year5'] config = { 'header_style':'', 'row_styles': () } [ data [str(x)][y].insert (0, strat_names[x]) for x in range (9) for y in sheets ] #add intervention names in #total sheet total_data = [] for x in range(9): total_data.append([]) total_data[x].insert(0, strat_names[x]) for y in range(1,len(data['intermed'])): total_data[x].append(0.0) for z in sheets: total_data[x][y] += data[str(x)][z][y] total_sheet = mm.Document (total_data, order=data['intermed'],config_dict=config) total_sheet.set_name('Totals') mmdocs = [] #This next section could be all list comprehensives but I'm leaving them as for statements #to reduce visual complexity. Also programmer complexity. for x in range(len(sheets)): mmdocs.append(mm.Document ([data[str(y)][sheets[x]] for y in range(9) ],order=data['intermed'],config_dict=config)) mmdocs[x].set_name(sheets[x]) #Add all excel sheets to the first sheet [ total_sheet.add_child(mmdocs[x]) for x in range(len(sheets)) ] #write to a file: write_filename = '/media/excel_%s.xls' % ( filename.split('.json')[0] ) total_sheet.write('%s%s' % (path_to_xpert_su, write_filename) ) #Let Apache serve it return HttpResponseRedirect ( write_filename ) else: return render(request, "bad.html", {'type':'excel'})
def test_write_and_writestr_binary_output(self): """ write() and writestr() should generate the same files """ my_data = [ { 'msg': "My first Cell", 'id': 1, 'when': now, }, { 'msg': "My second Cell", 'id': 2, 'when': now, }, ] # Write using write() mm_doc = mm.Document(my_data) mm_doc.write("tests/generated_files/test_file1.xls") # Write using writestr() in binary mode str = mm_doc.writestr() with open("tests/generated_files/test_file2.xls", "wb") as f: f.write(str) self.assertTrue(filecmp.cmp("tests/generated_files/test_file1.xls", "tests/generated_files/test_file2.xls"))
def test_mid_complex(self): my_data = [ { 'msg': "My first Cell", 'id': 1, 'when': mm.Date(now, "%Y-%m-%dT%H:%M:%S"), 'homepage': mm.URL("https://github.com/brianray") }, { 'msg': "My second Cell", 'id': 2, 'when': now, 'homepage': mm.URL("http://twitter.com/brianray", "Tweet Tweet") }, ] mm_doc = mm.Document(my_data) str = mm_doc.writestr() self.assertTrue( len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_doc2.xls", "wb") as f: f.write(str)
def test_missing_2(self): my_data = [ { 'msg': "My first Cell", 'id': 1, }, { 'msg': "My second Cell", 'id': 2, }, { 'msg': "My third Cell has missing data", 'id': 3, 'when': now, }, ] with self.assertRaises(Exception): mm_doc = mm.Document(my_data) str = mm_doc.writestr() self.assertTrue( len(str) > 10, msg="String should be longer than %s" % len(str)) with open("test_doc.xls", "wb") as f: f.write(str) self.check("tests/generated_files/test_doc.xls", my_data)
def get(self, request, *args, **kwargs): members, headers = self.get_export_data() config = { 'header_style': 'background-color: #c5d7ef; font-size: 12pt;', 'row_styles': (), 'freeze_row': 0 } mm_doc = mm.Document(members, config_dict=config, order=headers) return self.render_to_xls(mm_doc)
def test_row_style(self): config = {'row_styles': ("font-family: Times-New-Roman;", )} mm_doc = mm.Document(self.my_data, config_dict=config) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_custom_row_styles.xls", "wb") as f: f.write(str)
def test_no_style(self): config = {'header_style': '', 'row_styles': ()} mm_doc = mm.Document(self.my_data, config_dict=config) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_custom_no_styles.xls", "wb") as f: f.write(str)
def test_no_header_row(self): config = {'headers': False, 'freeze_row': 0} mm_doc = mm.Document(self.my_data, config_dict=config) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_custom_no_header.xls", "wb") as f: f.write(str)
def test_custom_django_serializer(self): django_query_set = TestAllBaseTypes.objects.all() mm_doc = mm.Document(django_query_set, data_model_class=DjangoDataModel, grid_class=DjangoGrid) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_django_serializer.xls", "wb") as f: f.write(str)
def test_image(self): my_data = [ { 'profile': mm.Image(os.path.join(path, "author.bmp"), 230, 326) }, ] mm_doc = mm.Document(my_data) str = mm_doc.writestr() self.assertTrue( len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_doc_image.xls", "wb") as f: f.write(str)
def test_col_type(self): my_data = [ { 'date': "2003-01-01" }, ] col_types = {'date': mm.Date} mm_doc = mm.Document(my_data, column_types=col_types) str = mm_doc.writestr() self.assertTrue( len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_col_types.xls", "wb") as f: f.write(str)
def test_minimal(self): my_data = [ { 'none type': None, 'Bool True': True, 'Bool False': False, 'Decimal': Decimal("3.14"), }, ] mm_doc = mm.Document(my_data) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_multi_data.xls", "wb") as f: f.write(str) self.check("tests/generated_files/test_multi_data.xls", my_data)
def test_minimal_lists(self): my_headers = ('id', 'msg', 'when') my_data = ( (1, "my first row", now), (2, "my second row", now), ) mm_doc = mm.Document(my_data, order=my_headers) str = mm_doc.writestr() self.assertTrue( len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_list_doc.xls", "wb") as f: f.write(str) as_dict = [dict(zip(my_headers, row)) for row in my_data] self.check("tests/generated_files/test_list_doc.xls", as_dict)
def test_minimal(self): my_data = [ { 'msg': "My first Cell", 'id': 1, 'when': now, }, { 'msg': "My second Cell", 'id': 2, 'when': now, }, ] mm_doc = mm.Document(my_data) # TODO: store this in a config username = raw_input("Google Username: "******"Google password: "******"Test MarMir File", username, password)
def test_simple_formula(self): my_data = [ { 'msg': "My first Row", 'id': 1, 'when': now, }, { 'msg': "My second Row", 'id': 2, 'when': now, }, { 'msg': "The total", 'id': mm.Formula("SUM(C2:C3)"), 'when': now, }, ] mm_doc = mm.Document(my_data) mm_doc.write("example_formula.xls")
def test_psycopg(self): if no_psycopg: print 'Skipping psycopg2 test (psycopg2 package not installed)' return dict_cur = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor) dict_cur.execute("INSERT INTO marmir_test (num, data) VALUES(%s, %s)", (100, "abc'def")) dict_cur.execute("INSERT INTO marmir_test (num, data) VALUES(%s, %s)", (200, "mmmmaaarrrr")) dict_cur.execute("SELECT * FROM marmir_test") my_data = dict_cur.fetchall() dict_cur.close() mm_doc = mm.Document(my_data) str = mm_doc.writestr() self.assertTrue(len(str) > 10, msg="String should be longer than %s" % len(str)) with open("tests/generated_files/test_psycopg.xls", "wb") as f: f.write(str) self.check("tests/generated_files/test_psycopg.xls", my_data)
def test_custom_pretty_table_serializer(self): my_data = [ { 'msg': "My first Cell", 'id': 1, 'when': now, }, { 'msg': "My second Cell", 'id': 2, 'when': now, }, ] mm_doc = mm.Document(my_data) # data_model_class=PrettyTableModel) mm_doc.set_composer_class(ComposerPrettyTable) out = ''' +----------------+---------------------+----+ | msg | when | id | +----------------+---------------------+----+ | My first Cell | %(now)s | 1 | | My second Cell | %(now)s | 2 | +----------------+---------------------+----+''' % dict(now=now) self.assertEquals(str(mm_doc.writestr()), out.strip())