util_xlsx.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from io import BytesIO
  2. import xlrd
  3. import xlwt
  4. from xlwt import Workbook
  5. from apps.task.models import TestTask
  6. def load_xlsx(file_path, task):
  7. if task.state == 2:
  8. return {'code': 500, 'info': '测试任务已完成,无法提交'}
  9. sheet = xlrd.open_workbook(file_path).sheet_by_index(0)
  10. error = []
  11. if len(sheet.col_values(0)[3:]) != len(set(sheet.col_values(0)[3:])):
  12. error.append('文件存在重复id,请检查')
  13. if sheet.row_values(0)[1].replace(" ", "") != task.plan.software.name:
  14. error.append('软件名称错误')
  15. if str(sheet.row_values(1)[1]).replace(" ", "") != task.plan.version:
  16. error.append('软件版本错误')
  17. case_info = []
  18. rows_num = sheet.nrows
  19. for i in range(3, rows_num):
  20. # print(sheet.row_values(i))
  21. if not sheet.row_values(i)[0].replace(" ", ""):
  22. error.append('第{}行编号不能为空'.format(i+1))
  23. if not sheet.row_values(i)[1].replace(" ", ""):
  24. error.append('第{}行名称不能为空'.format(i+1))
  25. if not sheet.row_values(i)[6].replace(" ", ""):
  26. error.append('第{}行测试步骤不能为空'.format(i+1))
  27. if not sheet.row_values(i)[7].replace(" ", ""):
  28. error.append('第{}行预期结果不能为空'.format(i+1))
  29. if task.state == 0:
  30. if sheet.row_values(i)[8].replace(" ", ""):
  31. error.append('测试任务未审核通过,第{}行实际结果需为空'.format(i + 1))
  32. if sheet.row_values(i)[9].replace(" ", "") != '未执行':
  33. error.append('当前测试任务状态为未执行,第{}行状态只能填写"未执行"'.format(i + 1))
  34. elif task.state == 1:
  35. if not sheet.row_values(i)[8].replace(" ", ""):
  36. error.append('测试任务已审核通过,第{}行实际结果不能为空'.format(i + 1))
  37. if sheet.row_values(i)[9].replace(" ", "") not in ['未执行', '执行成功', '执行失败']:
  38. error.append('第{}行状态只能填写"执行成功"或"执行失败"。若执行结果有时成功,有时失败,填写"执行失败",并在备注中写明详细信息'.format(i + 1))
  39. if not sheet.row_values(i)[10].replace(" ", ""):
  40. error.append('第{}行编写人不能为空'.format(i+1))
  41. if not sheet.row_values(i)[11].replace(" ", ""):
  42. error.append('第{}行评审员不能为空'.format(i+1))
  43. if task.state == 1:
  44. if not sheet.row_values(i)[12].replace(" ", ""):
  45. error.append('第{}行执行人不能为空'.format(i+1))
  46. case_info.append([info.replace(" ", "") for info in sheet.row_values(i)])
  47. if error:
  48. return {'code': 50001, 'info': error}
  49. return {'code': 200, 'info': case_info}
  50. def write_to_xlsx(file_path, cases_info):
  51. ws = Workbook(encoding='UTF-8')
  52. w = ws.add_sheet(u'Sheet1')
  53. for i in range(len(cases_info)):
  54. for j in range(len(cases_info[i])):
  55. w.write(i, j, cases_info[i][j])
  56. ws.save(file_path)
  57. sio = BytesIO()
  58. ws.save(sio)
  59. return sio
  60. # if __name__ == '__main__':
  61. # file = 'static/case-file/TestLaboratory_V1_Task_3/2021-07-10 17:41:13.798046/测试用例模版.xlsx'
  62. # task_ = TestTask.objects.filter(id='task = TestTask.objects.filter(id=task_id, delete=False)', delete=False)
  63. # load_xlsx(file, task_)