def get(self): """ Serves the empty ballot to the client-side so that the user may fill it out and submit it back via post. """ page_data = {} # Authenticate user voter = auth.get_voter(self) net_id = voter.net_id # Serve the election the user has requested election_id = self.request.get('id') if not election_id: page_data['error_msg'] = 'No election was specified.' webapputils.render_page(self, PAGE_NAME, page_data) return logging.info('%s requested election: %s', net_id, election_id) # Get the election from the database election = db.get(election_id) if not election: page_data['error_msg'] = 'Election not found.' webapputils.render_page(self, PAGE_NAME, page_data) return # Make sure user is eligible to vote status = models.voter_status(voter, election) if status == 'voted': page_data['error_msg'] = 'You have already voted for this election.' elif status == 'not_eligible': page_data['error_msg'] = 'You are not eligible to vote for this election.' elif status == 'invalid_time': page_data['error_msg'] = 'You are not in the eligible voting time for this election.' if status != 'eligible': webapputils.render_page(self, PAGE_NAME, page_data) return # Write election information for key, value in election.to_json().items(): page_data[key] = value page_data['positions'] = [] page_data['voter_net_id'] = voter.net_id # Write position information election_positions = election.election_positions for election_position in election_positions: position = {} for key, value in election_position.to_json().items(): position[key] = value random.shuffle(position['candidates']) page_data['positions'].append(position) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def get(self): """ Serves the empty ballot to the client-side so that the user may fill it out and submit it back via post. """ page_data = {} # Authenticate user voter = auth.get_voter(self) net_id = voter.net_id #Dictionary of candidates images candidate_images = { "Griffin Thomas": "http://sa.rice.edu/img/candidates2016/GriffinThomasPresident.jpg", "Joan Liu": "http://sa.rice.edu/img/candidates2016/JoanLiuPresident.jpg", "Hannah Todd": "http://sa.rice.edu/img/candidates2016/HannahToddEVP.jpg", "Brianna Singh": "http://sa.rice.edu/img/candidates2016/BriannaSinghEVP.jpg", "Justin Onwenu": "http://sa.rice.edu/img/candidates2016/JustinOnwenuEVP.jpg", "Komal Luthra": "http://sa.rice.edu/img/candidates2016/KomalLuthraIVP.jpg", "Sonal Pai": "http://sa.rice.edu/img/candidates2016/SonalPaiSec.jpg", "Maurice Frediere": "http://sa.rice.edu/img/candidates2016/MauriceFrediereTreasurer.jpg", "Iman Khan": "http://sa.rice.edu/img/candidates2016/ImanKhanRPC.jpg", "Jodie Ngheim": "http://sa.rice.edu/img/candidates2016/JodieNghiemRPC.jpg", "Kailan Shi": "http://sa.rice.edu/img/candidates2016/KailanShiRSVP.jpg", "Yasna Haghdoost": "http://sa.rice.edu/img/candidates2016/YasnaHaghdoostThresher.jpg", "Marcela Interiano": "http://sa.rice.edu/img/candidates2016/MarcelaInterianoUCourt.jpg" } page_data["CandidatePictures"] = candidate_images # Serve the election the user has requested election_id = self.request.get('id') if not election_id: page_data['error_msg'] = 'No election was specified.' webapputils.render_page(self, PAGE_NAME, page_data) return logging.info('%s requested election: %s', net_id, election_id) # Get the election from the database election = db.get(election_id) if not election: page_data['error_msg'] = 'Election not found.' webapputils.render_page(self, PAGE_NAME, page_data) return # Make sure user is eligible to vote status = models.voter_status(voter, election) if status == 'voted': page_data[ 'error_msg'] = 'You have already voted for this election.' elif status == 'not_eligible': page_data[ 'error_msg'] = 'You are not eligible to vote for this election.' elif status == 'invalid_time': page_data[ 'error_msg'] = 'You are not in the eligible voting time for this election.' if status != 'eligible': webapputils.render_page(self, PAGE_NAME, page_data) return # Write election information for key, value in election.to_json().items(): page_data[key] = value page_data['positions'] = [] page_data['voter_net_id'] = voter.net_id # TODO Catch Shuffle Option # Write position information election_positions = election.election_positions for election_position in election_positions: position = {} for key, value in election_position.to_json().items(): position[key] = value random.shuffle(position['candidates']) page_data['positions'].append(position) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def post(self): """ Takes the filled out ballot from the client-side, validates it, and stores it in the models. Sends confirmation to client-side. """ logging.info('Received new ballot submission.') logging.info(self.request.POST) formData = json.loads(self.request.get('formData')) logging.info(formData) # Authenticate user voter = auth.get_voter() if not voter: self.respond('ERROR', 'You\'re not logged in!') return # Get the election from the database election_id = formData['election_id'] election = db.get(election_id) if not election: self.respond('ERROR', 'Invalid election!') # Make sure user is eligible to vote status = models.voter_status(voter, election) if status == 'voted': self.respond('ERROR', 'You have already voted for this election.') return elif status == 'not_eligible': self.respond('ERROR', 'You are not eligible to vote for this election.') return elif status == 'invalid_time': self.respond( 'ERROR', 'You are not in the eligible voting time for this election.') return if status != 'eligible': self.respond('ERROR', 'You are not eligible to vote for this election.') return # Verify that the user has voted correctly verified_positions = { } # Whether an election_position has been verified for election_position in election.election_positions: verified_positions[str(election_position.key())] = False for position in formData['positions']: if position['type'] == 'Ranked-Choice': verified_positions[position[ 'id']] = self.verify_ranked_choice_ballot(position) elif position['type'] == 'Cumulative-Voting': verified_positions[position[ 'id']] = self.verify_cumulative_voting_ballot(position) elif position['type'] == 'Boolean-Voting': verified_positions[position[ 'id']] = self.verify_boolean_voting_ballot(position) else: logging.error('Encountered unknown position type: %s', position['type']) verified_positions[position['id']] = False logging.info('Verified Positions: %s', verified_positions) for verified in verified_positions.values(): if verified == False: self.respond( 'ERROR', 'You have attempted to cast an invalid ballot. Please verify that you are following all instructions.' ) return # Record all of the votes for position in formData['positions']: if verified_positions[position['id']]: if position['type'] == 'Ranked-Choice': self.cast_ranked_choice_ballot(position) elif position['type'] == 'Cumulative-Voting': self.cast_cumulative_voting_ballot(position) elif position['type'] == 'Boolean-Voting': self.cast_boolean_voting_ballot(position) models.mark_voted(voter, election) self.respond( 'OK', 'Your ballot has been successfully cast! <a href="/vote">Click here to go to the voting page.</a>' )
def post(self): """ Takes the filled out ballot from the client-side, validates it, and stores it in the models. Sends confirmation to client-side. """ logging.info('Received new ballot submission.') logging.info(self.request.POST) formData = json.loads(self.request.get('formData')) logging.info(formData) # Authenticate user voter = auth.get_voter() if not voter: self.respond('ERROR', 'You\'re not logged in!') return # Get the election from the database election_id = formData['election_id'] election = db.get(election_id) if not election: self.respond('ERROR', 'Invalid election!') # Make sure user is eligible to vote status = models.voter_status(voter, election) if status == 'voted': self.respond('ERROR', 'You have already voted for this election.') return elif status == 'not_eligible': self.respond('ERROR', 'You are not eligible to vote for this election.') return elif status == 'invalid_time': self.respond('ERROR', 'You are not in the eligible voting time for this election.') return if status != 'eligible': self.respond('ERROR', 'You are not eligible to vote for this election.') return # Verify that the user has voted correctly verified_positions = {} # Whether an election_position has been verified for election_position in election.election_positions: verified_positions[str(election_position.key())] = False for position in formData['positions']: if position['type'] == 'Ranked-Choice': verified_positions[position['id']] = self.verify_ranked_choice_ballot(position) elif position['type'] == 'Cumulative-Voting': verified_positions[position['id']] = self.verify_cumulative_voting_ballot(position) else: logging.error('Encountered unknown position type: %s', position['type']) verified_positions[position['id']] = False logging.info('Verified Positions: %s', verified_positions) for verified in verified_positions.values(): if verified == False: self.respond('ERROR', 'You have attempted to cast an invalid ballot. Please verify that you are following all instructions.') return # Record all of the votes for position in formData['positions']: if verified_positions[position['id']]: if position['type'] == 'Ranked-Choice': self.cast_ranked_choice_ballot(position) elif position['type'] == 'Cumulative-Voting': self.cast_cumulative_voting_ballot(position) models.mark_voted(voter, election) self.respond('OK', 'Your ballot has been successfully cast! <a href="/vote">Click here to go to the voting page.</a>')
def get(self): """ Serves the empty ballot to the client-side so that the user may fill it out and submit it back via post. """ page_data = {} # Authenticate user voter = auth.get_voter(self) net_id = voter.net_id #Dictionary of candidates images candidate_images = {"Griffin Thomas": "http://sa.rice.edu/img/candidates2016/GriffinThomasPresident.jpg", "Joan Liu": "http://sa.rice.edu/img/candidates2016/JoanLiuPresident.jpg", "Hannah Todd": "http://sa.rice.edu/img/candidates2016/HannahToddEVP.jpg", "Brianna Singh": "http://sa.rice.edu/img/candidates2016/BriannaSinghEVP.jpg", "Justin Onwenu": "http://sa.rice.edu/img/candidates2016/JustinOnwenuEVP.jpg", "Komal Luthra": "http://sa.rice.edu/img/candidates2016/KomalLuthraIVP.jpg", "Sonal Pai": "http://sa.rice.edu/img/candidates2016/SonalPaiSec.jpg", "Maurice Frediere": "http://sa.rice.edu/img/candidates2016/MauriceFrediereTreasurer.jpg", "Iman Khan": "http://sa.rice.edu/img/candidates2016/ImanKhanRPC.jpg", "Jodie Ngheim": "http://sa.rice.edu/img/candidates2016/JodieNghiemRPC.jpg", "Kailan Shi": "http://sa.rice.edu/img/candidates2016/KailanShiRSVP.jpg", "Yasna Haghdoost": "http://sa.rice.edu/img/candidates2016/YasnaHaghdoostThresher.jpg", "Marcela Interiano": "http://sa.rice.edu/img/candidates2016/MarcelaInterianoUCourt.jpg"} page_data["CandidatePictures"] = candidate_images # Serve the election the user has requested election_id = self.request.get('id') if not election_id: page_data['error_msg'] = 'No election was specified.' webapputils.render_page(self, PAGE_NAME, page_data) return logging.info('%s requested election: %s', net_id, election_id) # Get the election from the database election = db.get(election_id) if not election: page_data['error_msg'] = 'Election not found.' webapputils.render_page(self, PAGE_NAME, page_data) return # Make sure user is eligible to vote status = models.voter_status(voter, election) if status == 'voted': page_data['error_msg'] = 'You have already voted for this election.' elif status == 'not_eligible': page_data['error_msg'] = 'You are not eligible to vote for this election.' elif status == 'invalid_time': page_data['error_msg'] = 'You are not in the eligible voting time for this election.' if status != 'eligible': webapputils.render_page(self, PAGE_NAME, page_data) return # Write election information for key, value in election.to_json().items(): page_data[key] = value page_data['positions'] = [] page_data['voter_net_id'] = voter.net_id # TODO Catch Shuffle Option # Write position information election_positions = election.election_positions for election_position in election_positions: position = {} for key, value in election_position.to_json().items(): position[key] = value random.shuffle(position['candidates']) page_data['positions'].append(position) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)