# -*- coding: utf-8 -*-
__author__ = "Lee.le"
from Views.tools import *
from django.shortcuts import render, HttpResponse
import json


def progress(request):
    """
        显示测试用例内容
        :param request:
        :return:
        """
    if request.method == 'POST':
        pass
    config = configparser.ConfigParser()
    config_Path = (os.path.abspath(os.path.join(os.getcwd(), "../multi_processframe/config.ini")))  # 设置配置路径
    config.read(config_Path, encoding='utf-8-sig')
    # config.read(path, encoding='GBK')
    temp = config.get('config', "progress").split(",")
    donelist = []
    undonelist = []
    for k, v in case_translate().items():
        if v in temp:
            donelist.append(k)
        else:
            undonelist.append(k)
    return render(request, "case.html", {'addr': get_addr(), "donelist": donelist, 'data': case_translate(), 'undonelist':undonelist, 'donenum': len(donelist), 'undonenum': len(undonelist)})


def get_case(request):

    config = configparser.ConfigParser()
    config_Path = (os.path.abspath(os.path.join(os.getcwd(), "../multi_processframe/config.ini")))  # 设置配置路径
    config.read(config_Path, encoding='utf-8-sig')
    # config.read(path, encoding='GBK')
    temp = config.get('config', "progress").split(",")
    donelist = []
    undonelist = []
    for k, v in case_translate().items():
        if v in temp:
            donelist.append(k)
        else:
            undonelist.append(k)

    reportlist, htmlname = getreport()  # 获取所有报告
    temp = 0  # 定义一个参数用来指定返回html的数据类型，1需要执行局部加载，0不需要执行局部加载
    for i in htmlname:  # 判断是否有报告未生成的，temp=1执行局部加载，并且把进度传到前端
        file_Path = os.path.join(os.getcwd(), f"static/Report/{i}/{i}.html")  # 判断是否有卫生而成报告的路径
        if not os.path.exists(file_Path):
            temp = 1
            break
    if temp == 1:  # 当有报告未生成的时候，temp=1执行局部加载，并且把进度传到前端
        return HttpResponse(json.dumps({"donelist": donelist, 'undonelist':undonelist, 'donenum': len(donelist), 'undonenum': len(undonelist)}))
    else:  # 条件不满足，返回404告诉前端不需要执行局部加载
        notdo = 404
        return HttpResponse(json.dumps({"notdo": notdo}))




