fileview.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import datetime
  2. import os
  3. from django.http import HttpResponse
  4. from rest_framework.views import APIView
  5. from TestLaboratory.settings import PLAN_ROOT, TASK_ROOT
  6. from apps.file.models import FileManager
  7. from apps.log.models import get_log, gen_log
  8. from apps.plan.models import TestPlan
  9. from apps.task.models import TestTask
  10. from apps.user.middleware.rolecontrol import RoleControl
  11. from utils.util_add_id import gen_next_id
  12. import logging
  13. logger = logging.getLogger('django')
  14. class FileView(APIView):
  15. # create file
  16. @staticmethod
  17. @RoleControl
  18. def post(request, *args, **kwargs):
  19. file_type = request.POST.get('type')
  20. plan_id = request.POST.get('plan_id')
  21. task_id = request.POST.get('task_id')
  22. files = request.FILES.getlist('files')
  23. model_object = None
  24. path_head = None
  25. if file_type == 'plan_statement':
  26. model_object = TestPlan.objects.filter(id=plan_id, delete=False)
  27. if not model_object:
  28. logger.error("测试计划已删除或不存在")
  29. return HttpResponse(status=404, content='测试计划已删除或不存在')
  30. model_object = model_object[0]
  31. path_head = PLAN_ROOT
  32. elif file_type == 'task_statement':
  33. model_object = TestTask.objects.filter(id=task_id, delete=False)
  34. if not model_object:
  35. logger.error("测试任务已删除或不存在")
  36. return HttpResponse(status=404, content='测试任务已删除或不存在')
  37. model_object = model_object[0]
  38. path_head = TASK_ROOT
  39. # step7:创建文件的数据库字段
  40. fids = ''
  41. file_paths = []
  42. for file in files:
  43. try:
  44. create_time = update_time = datetime.datetime.now()
  45. file_dir = path_head + "/" + model_object.id + "/" + str(create_time)
  46. if not os.path.exists(file_dir):
  47. os.makedirs(file_dir)
  48. file_path = file_dir + "/" + file.name
  49. with open(file_path, 'wb') as f:
  50. f.write(file.read())
  51. f.close()
  52. except:
  53. logger.error("文件上传失败")
  54. return HttpResponse(status=500, content="文件上传失败")
  55. if not FileManager.objects.last():
  56. id_file = "TestLaboratory_V1_File_1"
  57. else:
  58. id_file = gen_next_id(FileManager.objects.order_by('create_time').last().id)
  59. try:
  60. file_new = FileManager.objects.create(id=id_file, category=file_type,
  61. path=model_object.id+ "/" + str(create_time) + "/" + file.name,
  62. create_time=create_time,
  63. update_time=update_time)
  64. except:
  65. logger.error("文件路径写入数据库失败")
  66. return HttpResponse(status=500, content="文件路径写入数据库失败")
  67. file_paths.append(PLAN_ROOT + file_new.path)
  68. fids += id_file + "&&"
  69. fids = fids[:-2]
  70. model_object.statement_file += ("&&" + fids)
  71. model_object.save()
  72. executor, action, method = get_log(request)
  73. gen_log(action, "说明文档", model_object.title, method, executor)
  74. return HttpResponse(status=200, content='新建文件成功')
  75. # create file
  76. @staticmethod
  77. @RoleControl
  78. def delete(request, *args, **kwargs):
  79. file_id = request.GET.get('file_id')
  80. file_type = request.GET.get('type')
  81. plan_id = request.GET.get('plan_id')
  82. task_id = request.GET.get('task_id')
  83. model_object = None
  84. if file_type == 'plan_statement':
  85. model_object = TestPlan.objects.filter(id=plan_id, delete=False)
  86. if not model_object:
  87. logger.error("测试计划已删除或不存在")
  88. return HttpResponse(status=404, content='测试计划已删除或不存在')
  89. model_object = model_object[0]
  90. elif file_type == 'task_statement':
  91. model_object = TestTask.objects.filter(id=task_id, delete=False)
  92. if not model_object:
  93. logger.error("测试任务已删除或不存在")
  94. return HttpResponse(status=404, content='测试任务已删除或不存在')
  95. model_object = model_object[0]
  96. file = FileManager.objects.filter(id=file_id, delete=False)
  97. if not file:
  98. logger.error("文件已删除不存在")
  99. return HttpResponse(status=404, content='文件已删除不存在')
  100. file = file[0]
  101. file_list = model_object.statement_file.split('&&')
  102. if file.id not in file_list:
  103. logger.error("该文件不存在于测试计划或测试任务中,数据库错误")
  104. return HttpResponse(status=404, content='该文件不存在于测试计划或测试任务中,数据库错误')
  105. file_list.remove(file.id)
  106. file_str = ''
  107. for file_name in file_list:
  108. file_str += file_name + "&&"
  109. file_str = file_str[:-2]
  110. model_object.statement_file = file_str
  111. model_object.save()
  112. executor, action, method = get_log(request)
  113. gen_log(action, "说明文档", file.path.split('/')[-1], method, executor)
  114. return HttpResponse(status=200, content='删除成功')