def test_get_list_of_object(self): #test if the function get_list_of_object is correct message, objects_list = get_list_of_object('Upload.objects.all()', [(1, 2)]) self.assertEqual(message, 'ok') #check if the returned message is 'ok; #check if the returned objects list is the correct list self.assertEqual(objects_list[0], self.upload) self.assertEqual(len(objects_list), 1) #test for exception, when both argument of this function is empty: message, objects_list = get_list_of_object('', []) self.assertEqual(message, 'The data function should be specify!') self.assertEqual(objects_list, []) #test if the function is not correct, then the correct message should be returned message, objects_list = get_list_of_object('toilatung', [(1, 2)]) self.assertEqual(message, 'Definition of data function error at cell C2') self.assertEqual(objects_list, []) #test if the function if correct, but returned value of object_list is not appropriate message, objects_list = get_list_of_object('Upload.objects', [(1, 2)]) self.assertEqual( message, 'The function you defined returns wrong result (must return a list of objects):cell C2' ) self.assertEqual(objects_list, [])
def test_get_list_of_object(self): #test if the function get_list_of_object is correct message, objects_list = get_list_of_object('Upload.objects.all()', [(1,2)]) self.assertEqual(message, 'ok') #check if the returned message is 'ok; #check if the returned objects list is the correct list self.assertEqual(objects_list[0],self.upload) self.assertEqual(len(objects_list),1) #test for exception, when both argument of this function is empty: message, objects_list = get_list_of_object('',[]) self.assertEqual(message, 'The data function should be specify!') self.assertEqual(objects_list, []) #test if the function is not correct, then the correct message should be returned message, objects_list = get_list_of_object('toilatung', [(1,2)]) self.assertEqual(message, 'Definition of data function error at cell C2') self.assertEqual(objects_list, []) #test if the function if correct, but returned value of object_list is not appropriate message, objects_list = get_list_of_object('Upload.objects', [(1,2)]) self.assertEqual(message, 'The function you defined returns wrong result (must return a list of objects):cell C2') self.assertEqual(objects_list, [])
def generate(filename, request): fname = filename #name of the input file response = HttpResponse(mimetype='application/ms-excel') response['Content-Disposition'] = u'attachment; filename=%s' % fname #read input file, style list: input_book = xlrd.open_workbook( '%s/%s' % (FILE_UPLOAD_PATH, filename), formatting_info=True) #Read excel file for get data style_list = copy2( input_book ) #copy the content and the format(style) of the input file into wtbook #create output file: wtbook = xlwt.Workbook(encoding='utf-8') #create new workbook for i in range(input_book.nsheets): sheet = input_book.sheet_by_index(i) # Get the first sheet try: #extract the specified information function_name, index_of_function, group, index_of_group, body, indexes_of_body, index_of_excel_function, excel_function, body_input, index_of_body_input, head, index_of_head, head_input, index_of_head_input, foot, index_of_foot, foot_input, index_of_foot_input, once, index_of_once, once_input, index_of_once_input, reserve_postions = fileExtractor( sheet) except: return 'Wrong input file, please check all data', response #if cannot extract the data, return wrong message else: message, list_objects = get_list_of_object(function_name, index_of_function, request) if message != 'ok': return message, response #generate the report to the excel file, message here is the signal of the success message = generate_output( list_objects, index_of_function, group, index_of_group, body, indexes_of_body, fname, index_of_excel_function, excel_function, body_input, index_of_body_input, head, index_of_head, head_input, index_of_head_input, foot, index_of_foot, foot_input, index_of_foot_input, request, once, index_of_once, once_input, index_of_once_input, sheet, style_list, wtbook, reserve_postions) if message != 'ok': return message, response wtbook.save(response) if request.session.get('is_spreadsheet'): wtbook.save('%s/%s' % (FILE_GENERATE_PATH, fname)) return 'ok', response
def generate(filename, request): fname = filename #name of the input file response = HttpResponse(mimetype='application/ms-excel') response['Content-Disposition'] = u'attachment; filename=%s' % fname #read input file, style list: input_book = xlrd.open_workbook('%s/%s' % (FILE_UPLOAD_PATH, filename), formatting_info=True) #Read excel file for get data style_list = copy2(input_book) #copy the content and the format(style) of the input file into wtbook #create output file: wtbook = xlwt.Workbook(encoding='utf-8') #create new workbook for i in range(input_book.nsheets): sheet = input_book.sheet_by_index(i) # Get the first sheet try: #extract the specified information function_name, index_of_function, group, index_of_group, body, indexes_of_body, index_of_excel_function, excel_function, body_input, index_of_body_input, head, index_of_head, head_input, index_of_head_input, foot, index_of_foot, foot_input, index_of_foot_input, once, index_of_once, once_input, index_of_once_input, reserve_postions, index_of_end_group = fileExtractor(sheet) except: return 'Wrong input file, please check all data', response #if cannot extract the data, return wrong message else: message, list_objects = get_list_of_object(function_name,index_of_function, request) if message != 'ok': return message, response #generate the report to the excel file, message here is the signal of the success message = generate_output(list_objects, index_of_function, group, index_of_group, body, indexes_of_body, fname, index_of_excel_function, excel_function, body_input, index_of_body_input, head, index_of_head, head_input, index_of_head_input, foot, index_of_foot, foot_input, index_of_foot_input, request, once, index_of_once, once_input, index_of_once_input, sheet, style_list, wtbook, reserve_postions, index_of_end_group) if message != 'ok': return message, response wtbook.save(response) if request.session.get('is_spreadsheet'): wtbook.save('%s/%s' % (FILE_GENERATE_PATH, fname)) return 'ok', response