def certify_transcript_single(request): if request.method == 'POST': student_id = request.POST['student_id'] payload = { 'commitment': hashlib.sha256(os.urandom(8)).hexdigest(), 'student_id': student_id, 'recipient': request.POST['recipient'], 'gpa': request.POST['gpa'], 'date': request.POST['date'] } # Be careful here; remember that changing the way the document # is formatted will create different hashes. We also eliminate # space b/w separators so that when we recreate the document # client-side, we get the same string representation for hashing. document = json.dumps(payload, sort_keys=True, separators=(',', ':')) resp = requests.post(create_rpc_url('/certify/Transcript/'+student_id), data=document) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'The transcript has been submitted to the network for certification.') else: messages.error(request,\ 'An error occurred while processing your \ certification request: ' + resp.text) return redirect(reverse('certchain:certify')) raise Http404
def certify_transcript_batch(request): if request.method == 'POST': batch_csv = request.POST['batch_contents'] documents = [] for row in batch_csv.split('\n'): cols = row.split(',') if len(cols) == 4: doc = { 'commitment': hashlib.sha256(os.urandom(8)).hexdigest(), 'student_id': cols[0], 'recipient': cols[1], 'gpa': cols[2], 'conferral_date': cols[3] } documents.append(json.dumps(doc, sort_keys=True, separators=(',', ':'))) # Be careful here; remember that changing the way the document # is formatted will create different hashes. We also eliminate # space b/w separators so that when we recreate the document # client-side, we get the same string representation for hashing. payload = json.dumps(documents) resp = requests.post(create_rpc_url('/certify/Transcript'), data=payload) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'The transcripts have been submitted to the network for certification.') else: messages.error(request,\ 'An error occurred while processing your \ certification request: ' + resp.text) return redirect(reverse('certchain:certify')) raise Http404
def overview(request): try: resp = requests.get(create_rpc_url('/network')) return render(request, 'certchain/overview.html',\ {'network' : resp.json()}) except Exception as ex: messages.error(request, 'Your institution\'s CertChain node \ is not available at this time.') return render(request, 'certchain/overview.html', {})
def manage_certifications(request): try: resp = requests.get(create_rpc_url('/all_certifications')) return render(request, 'certchain/certifications.html',\ {'certifications' : resp.json()}) except Exception as ex: messages.error(request, 'Your institution\'s CertChain node \ is not available at this time: ' + str(ex)) return render(request, 'certchain/certifications.html', {})
def get_document(request, docid, type): try: resp = requests.get(create_rpc_url('/document/' + docid)) json = resp.json() return render(request, 'public/'+ type + '.html', {'docid' : docid, 'doc' : json['contents'], 'raw_data': resp.text}) except Exception as ex: messages.error(request, 'Unable to retrieve ' + type + ' at this time: ' + str(ex)) return redirect(reverse('certchain:manage_certifications'))
def end_peering(request): if request.method == 'POST': addr = request.POST['addr'] resp = requests.post(create_rpc_url('/end_peering/' + addr)) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'Your peering termination request was successfully submitted.') else: messages.error(request,\ 'An error occurred while processing your \ peering termination request: ' + resp.text) return redirect(reverse('certchain:overview')) raise Http404
def approve_peer_request(request): if request.method == 'POST': addr = request.POST['requesting_addr'] resp = requests.post(create_rpc_url('/approve_peerreq/' + addr)) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'Your approval was submitted successfully.') else: messages.error(request,\ 'An error occurred while processing your \ peer request approval: ' + resp.text) return redirect(reverse('certchain:overview')) raise Http404
def remove_node(request): if request.method == 'POST': inst_addr = request.POST['inst_addr'] resp = requests.post(create_rpc_url('/remove_node/' + inst_addr)) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'The node was removed from the list of known nodes.') else: messages.error(request,\ 'An error occurred while removing the node you specified: ' + str(resp.text)) return redirect(reverse('certchain:overview')) raise Http404
def revoke_document(request): if request.method == 'POST': docid = request.POST['docid_to_revoke'] resp = requests.post(create_rpc_url('/revoke/' + docid)) if resp.status_code == 200: messages.success(request,\ 'Your document revocation request has been submitted to the network.') else: messages.error(request,\ 'An error occurred while processing your revocation \ request: ' + str(resp.status_code)) return redirect(reverse('certchain:manage_certifications')) raise Http404
def student(request, student_id): try: resp = requests.get(create_rpc_url('/certifications_by_student_id/' + student_id)) certs_by_type = defaultdict(list) for c in resp.json(): certs_by_type[c['doc_type']].append(c) return render(request, 'public/student.html',\ {'certs_by_type' : certs_by_type.iteritems(), 'student_id': student_id, }) except Exception as ex: messages.error(request, 'Your institution\'s CertChain node \ is not available at this time: ' + str(ex)) return render(request, 'public/student.html', {'student_id': student_id})
def add_node(request): if request.method == 'POST': payload = { 'hostname': request.POST['hostname'], 'port': request.POST['port'], 'address': request.POST['address'] } resp = requests.post(create_rpc_url('/add_node'), data=json.dumps(payload)) if resp.status_code == 200 and resp.text == 'OK': messages.success(request,\ 'The node was added to the list of known nodes.') else: messages.error(request,\ 'An error occurred while adding the node you specified: ' + str(resp.text)) return redirect(reverse('certchain:overview')) raise Http404
def documents(request): studentId = request.session.get('studentId') if (studentId): try: resp = requests.get(create_rpc_url('/certifications_by_student_id/' + studentId)) certs_by_type = defaultdict(list) for c in resp.json(): print c['doc_id'] certs_by_type[c['doc_type']].append(c) print 'yoo' return render(request, 'student/documents.html',\ {'certs_by_type' : certs_by_type.iteritems(), 'studentId': studentId, }) except Exception as ex: messages.error(request, 'Your institution\'s CertChain node \ is not available at this time: ' + str(ex)) return render(request, 'student/documents.html', {'studentId': studentId}) else: return HttpResponse("Yooo", content_type='text/plain')
def raw_block(request, block_height): resp = requests.get(create_rpc_url('/block/' + block_height)) return HttpResponse(resp.text, content_type='application/json')
def raw_document(request, docid): resp = requests.get(create_rpc_url('/document/' + docid)) return HttpResponse(resp.text, content_type='application/json')