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 approveCandidateDB(status, electionID, candidateID): if 'admin' in session: if status == "1" : Election.updateElection({'_id':ObjectId(electionID), 'candidates.id': candidateID}, {'$set': {'candidates.$.status': True}}) elif status == "0" : Election.updateElection({'_id':ObjectId(electionID)}, {'$pull': {'candidates':{'id': candidateID}}}) return redirect(url_for('approveCandidate')) else: flash('You need to Login first!', 'warning') return redirect(url_for('admin'))
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'))