Beispiel #1
0
def upload_file():

    universities = ['VJTI', 'IITB', 'KJSCE']
    if request.method == 'POST':
        if 'file' not in request.files:
            flash("No file selected", "danger")
            error = "No file selected"
            return render_template('upload.html',
                                   universities=universities,
                                   error=error)
        file = request.files['file']
        if file.filename == '':
            flash("No file selected", "error")
            return render_template('upload.html', universities=universities)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['CSV_FOLDER'], filename))

            #Merkle Tree init
            mt = MerkleTools(hash_type='sha3_256')
            count = 0

            with open(os.path.join(app.config['CSV_FOLDER'],
                                   filename)) as File:
                reader = csv.reader(File)
                for row in reader:

                    #studentID CPI Name batch college
                    data = str(row[0]) + str(row[2]) + str(row[1]) + str(
                        row[3]) + session['username']
                    batch = str(row[3])
                    mt.add_leaf(data, do_hash=True)
                    count += 1

            mt.make_tree()
            # if mt.get_leaf_count() != count:
            # print("not equal")
            # print(count, mt.get_leaf_count())
            if mt.get_tree_ready_state:
                merkleRoot = mt.get_merkle_root()
            institute = session['username']
            bc = Blockchain(institute)
            print("Adding to the blockchain: ", session['username'], batch,
                  merkleRoot)
            try:
                bc.addBatchMerkleRoot(batch, merkleRoot)
            except:
                error = "Institute not registered!"
                return render_template('upload.html',
                                       universities=universities,
                                       error=error)

            itr = 0

            with open(os.path.join(app.config['CSV_FOLDER'],
                                   filename)) as File:
                reader = csv.reader(File)
                for row in reader:
                    data = {}
                    data["cpi"] = str(row[2])
                    data["name"] = str(row[1])
                    data["year"] = str(row[3])
                    data["studentId"] = str(row[0])
                    data["institution"] = session['username']
                    data["merklePath"] = mt.get_proof(itr)
                    itr += 1

                    filename = str(row[1]) + '.json'

                    with open(
                            os.path.join(app.config['RECEIPT_FOLDER'],
                                         filename), 'w') as json_file:
                        json_file.write(json.dumps(data))

                flash("Successfully uploaded to blockchain!", "success")

    return render_template('upload.html', universities=universities)