Beispiel #1
0
def enterprise_evaluation():
    username = session.get("username", "")
    password = session.get("password", "")

    enterprise, signer = login(username, password)
    if enterprise is None:
        return redirect("/enterprise")

    evaluation_addr = enterprise.evaluation_addr
    _, al, _, _ = count_numbers()
    if al < 3:
        return render_template("enterprise2.html",
                               is_login=True,
                               fail_msg="缺少足够的安评机构参与(至少3家安评机构)",
                               enterprise=enterprise,
                               username=username)
    if evaluation_addr is None:
        return render_template("enterprise2.html",
                               is_login=True,
                               fail_msg="缺少审查合约地址",
                               enterprise=enterprise,
                               username=username)

    try:
        managementAddr = db.session.query(Contracts).filter(
            Contracts.name == "Management").first().addr

        call_contract(evaluation_addr,
                      "ReportEvaluation",
                      "getAgencyList",
                      args=[to_checksum_address(managementAddr)])

        res = call_contract(evaluation_addr,
                            "ReportEvaluation",
                            "startEvaluation",
                            signer=signer)
        ea_addr_list: List[str] = list(res[0])
    except Exception:
        traceback.print_exc()
        return render_template("enterprise2.html",
                               is_login=True,
                               fail_msg="合约调用失败",
                               enterprise=enterprise,
                               username=username)

    result = []
    for addr in ea_addr_list:
        ag = Agency.query.filter(Agency.contract_addr == addr).first()
        if ag is not None:
            result.append(
                (ag.username, addr, str(ag.envelope_pub, encoding="utf-8")))
    # audit = random.choice(list(AuditList.values()))
    # result.append((audit.username, ""))
    return render_template("evaluation2.html",
                           evaluation_addr=evaluation_addr,
                           result=result)
Beispiel #2
0
def logout():
    try:
        del session["username"]
        del session["password"]
    except KeyError:
        pass
    return render_template("index2.html",
                           succ_msg="退出成功",
                           is_login=False,
                           count=count_numbers())
Beispiel #3
0
def upload():
    username = session.get("username", "")
    password = session.get("password", "")
    agency, signer = login(username, password)

    if agency is None:
        return redirect("/agency")
    
    if request.method == "GET":
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username)

    ent_name = request.form.get("ent-name")
    eng_list = request.form.get("eng-list", "")
    eng_list = [e for e in eng_list.split()]
    data_hash = request.form.get("data-hash")
    if ent_name is None:
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, fail_msg = "缺少生产企业名称")

    _,_,aal,_ = count_numbers()
    if aal < 3:
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, fail_msg = "缺少足够的审查实体,至少需要3家审查实体")

    enterprise = Enterprise.query.filter(Enterprise.username == ent_name).first()
    if enterprise is None:
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, fail_msg = "生产企业名称错误")
    
    data_file = request.files.get("data-file")

    if data_file is None:
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, fail_msg = "缺少上传文件")
    if data_file.filename == "" : 
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, fail_msg = "缺少上传文件")

    data_file_path = os.path.join(app.config["UPLOAD_FOLDER"], secure_filename(data_file.filename))
    data_file.save(data_file_path)

    try:
        n = aal
        t = 3
        key, shares = shamir_encode(t, n)

        enc_data_path = os.path.join(app.config["UPLOAD_FOLDER"], "enc-"+secure_filename(data_file.filename))
        aes_encode(key, data_file_path, enc_data_path)

        data_file_addr = ipfs_client.add(enc_data_path)

        for i, audit in enumerate(Audit.query.all()):

            obj_data = IPFSObject(hash = data_file_addr["Hash"], name = secure_filename(data_file.filename), secret = shares[i][1].hex(), idx = shares[i][0])
            audit.files.append(obj_data)
            db.session.add(obj_data)
            db.session.commit()
    except Exception as e:
        traceback(e)
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, succ_msg = "IPFS上传失败")

    try:
        res = call_contract(enterprise.contract_addr, "Enterprise", "update", args=[data_hash, data_file_addr["Hash"], to_checksum_address(agency.contract_addr), eng_list], signer = signer)
        enterprise.evaluation_addr = res[0]
        db.session.commit()

        res = call_contract(enterprise.evaluation_addr, "ReportEvaluation", "businessUpdate", args = [] , signer = signer)
        licenseAddr = res[0]
        enterprise.license_addr = licenseAddr
        db.session.commit()
    except Exception:
        traceback.print_exc()
        return render_template("agency2-2.html", is_login = True, agency = agency, username = username, succ_msg = "智能合约调用失败")
    return render_template("agency2-2.html", is_login = True, agency = agency, username = username, succ_msg = "添加成功")
Beispiel #4
0
def license_handle(addr):
    is_login = check_login()

    try:
        res = call_contract(addr, "License", "getInfo", args = [])
        res2 = call_contract(addr, "License", "showInfo", args = [])
    except Exception:
        traceback.print_exc()
        return render_template("index2.html", is_login = is_login, fail_msg = "证书合约地址错误或合约调用失败", count = count_numbers())

    res2 = list(res2)
    license_info = list(res)
    license_info.extend(res2)
    time_local = None
    try:
        time_local = time.localtime(license_info[5]/ 1000)
    except Exception:
        traceback.print_exc()
    if time_local is not None:
        license_info[5] = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
    try:
        agency = Agency.query.filter(Agency.contract_addr == license_info[10]).first()
        license_info[10] = agency.username
    except Exception:
        traceback.print_exc()
    
    try:
        engineer_list = []
        for e_eid in license_info[11]:
            engineer = Engineer.query.filter(Engineer.eid == e_eid).first()
            if engineer is not None:
                engineer_list.append(engineer.username)
            else:
                engineer_list.append(e_eid)
        license_info[11] = engineer_list
    except Exception:
        traceback.print_exc()
    return render_template("license2.html", is_login = is_login,license_info = license_info)
Beispiel #5
0
def enterprise_upload():
    username = session.get("username", "")
    password = session.get("password", "")
    enterprise, signer = login(username, password)

    if enterprise is None:
        return redirect("/enterprise")

    if request.method == "GET":
        return render_template("enterprise2-1.html",
                               is_login=True,
                               enterprise=enterprise,
                               username=username)

    data_hash = request.form.get("data-hash")
    data_file = request.files.get("data-file")

    if data_file is None:
        return render_template("enterprise2-1.html",
                               is_login=True,
                               enterprise=enterprise,
                               username=username,
                               fail_msg="缺少上传文件")
    if data_file.filename == "":
        return render_template("enterprise2-1.html",
                               is_login=True,
                               enterprise=enterprise,
                               username=username,
                               fail_msg="缺少上传文件")

    data_file_path = os.path.join(app.config["UPLOAD_FOLDER"],
                                  secure_filename(data_file.filename))
    data_file.save(data_file_path)

    try:
        _, _, n, _ = count_numbers()
        t = 3
        key, shares = shamir_encode(t, n)

        enc_data_path = os.path.join(
            app.config["UPLOAD_FOLDER"],
            "enc-" + secure_filename(data_file.filename))
        aes_encode(key, data_file_path, enc_data_path)

        data_file_addr = ipfs_client.add(enc_data_path)

        for i, audit in enumerate(Audit.query.all()):
            obj_data = IPFSObject(hash=data_file_addr["Hash"],
                                  name=secure_filename(data_file.filename),
                                  secret=shares[i][1].hex(),
                                  idx=shares[i][0])
            audit.files.append(obj_data)
            db.session.add(obj_data)
            db.session.commit()
    except Exception as e:
        traceback(e)
        return render_template("enterprise2-1.html",
                               is_login=True,
                               enterprise=enterprise,
                               username=username,
                               succ_msg="IPFS上传失败")

    try:
        call_contract(enterprise.contract_addr,
                      "Enterprise",
                      "updateData",
                      args=[data_hash, data_file_addr["Hash"]],
                      signer=signer)
    except Exception as e:
        traceback(e)
        return render_template("enterprise2-1.html",
                               is_login=True,
                               enterprise=enterprise,
                               username=username,
                               succ_msg="智能合约调用失败")
    return render_template("enterprise2-1.html",
                           is_login=True,
                           enterprise=enterprise,
                           username=username,
                           succ_msg="添加成功")
Beispiel #6
0
def index():
    is_login = public.check_login()
    return render_template("index2.html",
                           is_login=is_login,
                           count=count_numbers())