def result(electionID): if 'admin' in session: election = Election.getElection({'_id': ObjectId(electionID)}) if election['date'].date() < datetime.datetime.now().date(): pass elif election['date'].date() == datetime.datetime.now().date() and election['endtime'] < str(datetime.datetime.now().time())[:5]: pass else: flash('Election not yet ended! Wait for Election to end to calculate result', 'warning') return redirect(url_for('dashboard')) result = {} response = Blockchain.get_chain() for block in response['chain']: IpfsBlock = Blockchain.get_by_hash(block['IpfsHash']) for txn_hash in IpfsBlock['data']: txn = Blockchain.get_by_hash(txn_hash) if txn['electionID'] == electionID: if txn['candidateID'] not in result: result[txn['candidateID']] = 1 else: result[txn['candidateID']] += 1 Election.updateElection({'_id':ObjectId(electionID)}, {'$set':{'result':result}}) flash('Result calculated and published.', 'info') return redirect(url_for('dashboard')) else: flash('You need to Login first!', 'warning') return redirect(url_for('login'))
def vote(electionID, candidateID): if 'voter' in session: voter = Voter.getVoter({'username': session['voter']}) election = Election.getElection({'_id': ObjectId(electionID)}) if election['date'].date() > datetime.datetime.now().date(): flash(f"Election not yet started. Come back on {election['date'].date()}", 'danger') elif election['date'].date() < datetime.datetime.now().date(): flash(f"Election ended on {election['date'].date()}", 'danger') elif election['date'].date() == datetime.datetime.now().date(): if str(datetime.datetime.now().time())[:5] > election['starttime'] and str(datetime.datetime.now().time())[:5] < election['endtime']: flash('Vote recorded successfully', 'success') vote = { 'timestamp': str(datetime.datetime.now()), 'electionID': electionID, 'candidateID': candidateID, 'voter': str(voter['_id']) } response = Blockchain.add_transaction(vote) flash(response, 'info') else: flash(f"You can vote between {election['starttime']} to {election['endtime']}", 'info') return redirect(url_for('home')) else: flash('You need to Login first!', 'warning') return redirect(url_for('login'))
def election(electionID): if 'voter' in session: election = Election.getElection({'_id':ObjectId(electionID)}) return render_template('election.html', Election = election) else: flash('You need to Login first!', 'warning') return redirect(url_for('login'))
def registerCandidate(): if 'voter' in session: voter = Voter.getVoter({'username':session['voter']}) electionID = request.form.get('electionID') election = Election.getElection({'_id':ObjectId(electionID)}) if datetime.datetime.now() + datetime.timedelta(days=2) >= election['date']: flash("Candiate Registration is closed", 'danger') else : candidate = { 'id' : str(uuid4()).replace('-', ''), 'voterID' : voter['_id'], 'name' : voter['name'], 'slogan' : request.form['slogan'], 'representing' : request.form['representing'], 'qualification' : request.form['qualification'], 'status' : False } Election.updateElection({'_id':ObjectId(electionID)}, {'$push': {'candidates': candidate}}) flash('Your registration details as candidate are successfully recorderd', 'info') return redirect(url_for('home')) else: flash('You need to Login first!', 'warning') return redirect(url_for('login'))