def test_check_hash(self): hash = "0x1b16b1df538ba12dc3f97edbb85caa7050d46c148134290feba80f8236c83db9" b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi, contract_address=contract_address) self.assertIsNotNone(b.check_hash(hash), msg="Hash already exists/ error")
def test_insert_hash(self): hash = "0x1b16b1df538ba12dc3f97edbb85caa7050d46c148134290feba80f8236c83db9" b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi, contract_address=contract_address) self.assertIsNone(b.insert_hash(hash), msg="Error. Inserted hash even if it existed.") new_hash = "0x1b16b1df538ba12dc3f97edbb85caa7050d46c148134290feba80f8236c83db0" self.assertIsNotNone(b.insert_hash(new_hash), msg="Insertion failed")
def view(self ,request ,pk): instance = Prescriptions.objects.get(pk=pk) text = instance.text hash_ = Web3.soliditySha3(['string'],[text]) hash_hex = hash_.hex() b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi) b.connect() exists = b.check_hash(hash_hex) return exists , instance
def test_return_hash(self): hash = "0x1b16b1df538ba12dc3f97edbb85caa7050d46c148134290feba80f8236c83db9" b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi, contract_address=contract_address) self.assertIsNone(b.return_hash(hash), msg="Returned hash even it didn't exist. ") new_hash = "0x1b16b1df538ba12dc3f97edbb85caa7050d46c148134290feba80f8236c83db1" ret_hash = b.insert_hash(new_hash) self.assertEqual(new_hash, ret_hash, msg="Hashes not equal")
def download(request, pk): instance = Prescriptions.objects.get(pk=pk) text = instance.text hash_ = Web3.soliditySha3(['string'], [text]) hash_hex = hash_.hex() if flag1 == False: b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi) # flag1=True b.connect() exists = b.check_hash(hash_hex) if exists: return render(request, 'prescription/prescription_download.html', {"prescription": instance}) else: return HttpResponse("<h1>Hash does not exist in blockchain</h1>")
def ocr(request, pk): if request.method == "GET": trial = prescription.Prescription() ocr_instance = trial.ocr(request, pk) # pk2 = kwargs.get('pk2', none) text = ocr_instance[0] prescriptions = ocr_instance[1] elif request.method == "POST": # form = forms.VerifyOCRText(request.POST , request.FILES) # if form.is_valid(): instance = Prescriptions.objects.get(pk=pk) if instance is not None: instance.text = request.POST.get('ocr-text', "") instance.save() instance = Prescriptions.objects.get(pk=pk) text = instance.text hash_ = Web3.sha3(text=text) hash_hex = hash_.hex() # print(hash_hex) # print(hash_) # print(len(hash_hex)) if flag1 == False: b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi) # flag1=True if b.connect(): inserted = b.insert_hash(hash_hex) print(inserted) if inserted: return redirect('prescription:list') else: return HttpResponse('<h1>Push to blockchain failed</h1>') else: return HttpResponse("<h1>Connection failed</h1>") # return redirect('prescription:block' , args=(pk)) # return render(block(request,pk)) else: return HttpResponse("<h1>Instance is null</h1>") return render(request, 'prescription/prescription_ocr.html', { 'prescriptions': prescriptions, 'text': text })
def test_connect(self): b = blockchain.Blockchain(url="http://127.0.0.1:7545", abi=abi, contract_address=contract_address) self.assertTrue(b.connect(), msg="Connection failed")