def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED }) return # Get organization information admin = models.Admin.gql('WHERE voter=:1', voter).get() logging.info(admin) org_admin = models.OrganizationAdmin.gql('WHERE admin=:1', admin).get() logging.info(org_admin) if not org_admin: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED }) return org = org_admin.organization auth.set_organization(org) # Construct page information page_data = {} page_data['organization'] = org page_data['admins'] = self.admin_list(org) page_data['elections'] = [elec.to_json(True) for elec in org.elections] logging.info(page_data['elections']) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page( self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) organizations = [] # Aggregate all information about organizations for org in models.Organization.all(): organizations.append({ 'name': org.name, 'electionCount': org.elections.count(), 'adminCount': org.organization_admins.count(), 'voteCount': sum([elec.voted_count for elec in org.elections]) }) # get 20 elections that have not ended, sorted by starting time elections = [ e.to_json(True) for e in models.Election.all().order('-end').order('start').run( limit=20) ] page_data = {"organizations": organizations, "elections": elections} return webapputils.render_page(self, '/intern/command-center', page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) organizations = [] # Aggregate all information about organizations for org in models.Organization.all(): organizations.append({ 'name': org.name, 'electionCount': org.elections.count(), 'adminCount': org.organization_admins.count(), 'voteCount': sum([elec.voted_count for elec in org.elections]) }) # get 20 elections that have not ended, sorted by starting time elections = [e.to_json(True) for e in models.Election.all().order('-end').order('start').run(limit=20)] page_data = { "organizations": organizations, "elections": elections } return webapputils.render_page(self, '/intern/command-center', page_data)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', { 'status': 'ERROR', 'msg': 'Not Authorized' }) return # Get election election = auth.get_election() if not election: panel = get_panel(PAGE_URL, { 'status': 'ERROR', 'msg': 'No election found.' }, None) webapputils.render_page_content(self, PAGE_URL, panel) return data = { 'status': 'OK', 'id': str(election.key()), 'election': election.to_json() } panel = get_panel(PAGE_URL, data, data.get('id')) webapputils.render_page_content(self, PAGE_URL, panel)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return # Get organization information admin = models.Admin.gql('WHERE voter=:1', voter).get() logging.info(admin) org_admin = models.OrganizationAdmin.gql('WHERE admin=:1', admin).get() logging.info(org_admin) if not org_admin: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return org = org_admin.organization auth.set_organization(org) # Construct page information page_data = {} page_data['organization'] = org page_data['admins'] = self.admin_list(org) page_data['elections'] = [elec.to_json() for elec in org.elections] logging.info(page_data['elections']) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def get(self): page_data = {'open_elections': [], 'election_results': []} # Authenticate user voter = auth.get_voter(self) # Elections the user is eligible to vote for elections = voter.elections election_keys = [election.key() for election in elections] # Add universal elections universal_elections = models.Election.gql("WHERE universal=TRUE AND hidden=FALSE") for election in universal_elections: if datetime.datetime.now() < election.end and election.key() not in election_keys: elections.append(election) logging.info(elections) for election in elections: logging.info('Found election') data = {} data['id'] = str(election.key()) data['name'] = election.name data['organization'] = election.organization.name now = datetime.datetime.now() if now > election.end: # Election passed result_delay = election.result_delay data['end_date'] = election.end.strftime('%a, %B %d, %Y, %I:%M %p') + ' UTC' data['result_delay'] = result_delay # Compute how much time the user will have to wait to view the election time_since_election_end = (now - election.end).seconds + (now - election.end).days * 86400 if time_since_election_end > result_delay: data['time_remaining'] = -1 else: data['time_remaining'] = (result_delay - time_since_election_end) *1000 page_data['election_results'].append(data) else: data['user_action'] = 'not_started' # Initial assumption # Check election times if election.start > now: start_str = election.start.strftime('%a, %B %d, %Y, %I:%M %p') + ' UTC' data['status'] = {'text': 'Voting begins on', 'date': start_str} data['user_action'] = 'not_started' else: end_str = election.end.strftime('%a, %B %d, %Y, %I:%M %p') + ' UTC' data['status'] = {'text': 'Voting ends on', 'date': end_str} data['user_action'] = 'vote' # Check to see if the user has already voted if models.voter_status(voter, election) == 'voted': data['user_action'] = 'voted' page_data['open_elections'].append(data) logging.info(data) webapputils.render_page(self, '/vote', page_data)
def post(self): methods = { 'create_organization': self.create_organization, 'add_admin': self.add_admin } data = json.loads(self.request.get('data')) voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return # hacker out = methods[data['method']](data)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED }) return # Get organization information admin = models.Admin.gql('WHERE voter=:1', voter).get() logging.info("<Admin: %s>", admin.email) org_admin = models.OrganizationAdmin.gql('WHERE admin=:1', admin).fetch(None) logging.info("<Admin of Organizations: %s>", [oa.organization.name for oa in org_admin]) if not org_admin: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED }) return orgs = [org_admin.organization for org_admin in org_admin] auth.set_organizations(orgs) # Pick one organization to display information about. org_param = self.request.get('org') if org_param: # Wants to change current active organization org_req = models.Organization.get(org_param) auth.set_active_organization(org_req) elif auth.get_active_organization( ): # Did not intend a change in the active organization pass else: # No active organizations have been set yet auth.set_active_organization(orgs[0]) # Construct page information page_data = {} page_data['organizations'] = orgs page_data['active_org'] = auth.get_active_organization() page_data['admins'] = self.admin_list(auth.get_active_organization()) page_data['elections'] = [ elec.to_json(True) for elec in auth.get_active_organization().elections ] logging.info(page_data['elections']) 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 # 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 update_profile(self, data): """ Updates the organization profile. """ logging.info('Updating profile') org_id = data['id'] org = models.Organization.get(org_id) assert models.get_admin_status(auth.get_voter(), org) for field in ['name', 'description', 'website']: setattr(org, field, data[field].strip()) org.put() self.respond('OK', 'Updated')
def post(self): # Authenticate user voter = auth.get_voter() if not voter: self.respond('ERROR', MSG_NOT_AUTHORIZED) return status = models.get_admin_status(voter) if not status: self.respond('ERROR', MSG_NOT_AUTHORIZED) return # Get method and data logging.info('Received call') data = json.loads(self.request.get('data')) methods = {'update_profile': self.update_profile} methods[data['method']](data['data'])
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return # Get organization information admin = models.Admin.gql('WHERE voter=:1', voter).get() logging.info("<Admin: %s>", admin.email) org_admin = models.OrganizationAdmin.gql('WHERE admin=:1', admin).fetch(None) logging.info("<Admin of Organizations: %s>", [oa.organization.name for oa in org_admin]) if not org_admin: logging.info('Not authorized') webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return orgs = [org_admin.organization for org_admin in org_admin] auth.set_organizations(orgs) # Pick one organization to display information about. org_param = self.request.get('org') if org_param: # Wants to change current active organization org_req = models.Organization.get(org_param) auth.set_active_organization(org_req) elif auth.get_active_organization(): # Did not intend a change in the active organization pass else: # No active organizations have been set yet auth.set_active_organization(orgs[0]) # Construct page information page_data = {} page_data['organizations'] = orgs page_data['active_org'] = auth.get_active_organization() page_data['admins'] = self.admin_list(auth.get_active_organization()) page_data['elections'] = [elec.to_json(True) for elec in auth.get_active_organization().elections] logging.info(page_data['elections']) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', {'status': 'Error', 'msg': 'Not Authorized'}) return data = {} # Get election election = auth.get_election() if election: data = {'id': str(election.key()), 'election': election.to_json()} panel = get_panel(PAGE_URL, data, data.get('id')) webapputils.render_page_content(self, PAGE_URL, panel)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page( self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) jobs = models.ProcessingJob.gql("ORDER BY started DESC LIMIT 20") ready = { "name": "jonesresults", "description": "Send out jones results" } page_data = {"jobs": jobs, "ready": ready} return webapputils.render_page(self, '/intern/jobs', page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) jobs = models.ProcessingJob.gql("ORDER BY started DESC LIMIT 20") ready = { "name": "MartelDeleteCampusWideElection", "description": "Deletes Campus Wide Positions Election for Martel Its Incomplete" } page_data = { "jobs": jobs, "ready": ready } return webapputils.render_page(self, '/intern/jobs', page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) jobs = models.ProcessingJob.gql("ORDER BY started DESC LIMIT 20") ready = { "name": "CSClubVoterList2", "description": "Sends the list of voters for CS Club elections" } page_data = { "jobs": jobs, "ready": ready } return webapputils.render_page(self, '/intern/jobs', page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) jobs = models.ProcessingJob.gql("ORDER BY started DESC LIMIT 20") ready = { "name": "WillRiceSpringRound3Unsent2", "description": "Sends the unsent results of the Will Rice election." } page_data = { "jobs": jobs, "ready": ready } return webapputils.render_page(self, '/intern/jobs', page_data)
def get(self): voter = auth.get_voter(self) if voter.net_id not in COMMANDERS: return self.render_template('/templates/message', { 'status': 'Not Authorized', 'msg': "You're not authorized to enter the command center" }) organizations = [] # Aggregate all information about organizations for org in models.Organization.all(): organizations.append({ 'name': org.name, 'electionCount': org.elections.count(), 'adminCount': org.organization_admins.count(), 'voteCount': sum([elec.voted_count for elec in org.elections]) }) page_data = { "organizations": organizations } return webapputils.render_page(self, '/intern/command-center', page_data)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return # Get election election = auth.get_election() if not election: panel = get_panel( PAGE_URL, {'status': 'ERROR','msg': 'No election found.'}, None) webapputils.render_page_content(self, PAGE_URL, panel) return if election.universal: panel = get_panel( PAGE_URL, {'status': 'Universal Election', 'msg': 'This is a universal election, anyone with a valid ' 'NetID can vote for. Therefore you cannot manage ' 'the voters list.'}, None) webapputils.render_page_content(self, PAGE_URL, panel) return data = {'status': 'OK', 'id': str(election.key()), 'voters': sorted(list(models.get_voter_set(election)))} logging.info(data) panel = get_panel(PAGE_URL, data, data.get('id')) webapputils.render_page_content(self, PAGE_URL, panel)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return election = None election_id = self.request.get('id') if election_id: election = models.Election.get(election_id) if not election: webapputils.render_page(self, '/templates/message', {'status': 'Error', 'msg': 'Election not found.'}) return auth.set_election(election) else: auth.clear_election() # Construct page information panel = get_panel(PAGE_NAME + '/information', {}, election_id) webapputils.render_page_content(self, PAGE_NAME, panel)
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 get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', { 'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED }) return # Get election election = auth.get_election() if not election: panel = get_panel(PAGE_URL, { 'status': 'ERROR', 'msg': 'No election found.' }, None) webapputils.render_page_content(self, PAGE_URL, panel) return if election.universal: panel = get_panel( PAGE_URL, { 'status': 'Universal Election', 'msg': 'This is a universal election, anyone with a valid ' 'NetID can vote for. Therefore you cannot manage ' 'the voters list.' }, None) webapputils.render_page_content(self, PAGE_URL, panel) return # check to see if task already in task queue, indicates voters still being added # TODO: Don't add blank task, just see if there is already something working in the queue. # try: # response = requests.get('https://www.googleapis.com/taskqueue/v1beta2/projects/owlection/taskqueues/voters/tasks') # # # taskqueue.add(name=str(election.key()), # # queue_name='voters') # except taskqueue.TaskAlreadyExistsError: # panel = get_panel( # PAGE_URL, # {'status': 'Adding Voters', # 'msg': 'Come back later. Still adding voters.'}, # None) # webapputils.render_page_content(self, PAGE_URL, panel) # return #checks to see if the voter set exists (in mem cache), #if not indicates voters still being added # voter_set = memcache.get(str(election.key())+'-voter-set') # if not voter_set: # deferred.defer(models.update_voter_set, election, _name=str(election.key()), _queue='voters') # panel = get_panel( # PAGE_URL, # {'status': 'Adding Voters', # 'msg': 'Come back later. Still adding voters.'}, # None) # webapputils.render_page_content(self, PAGE_URL, panel) # return data = { 'status': 'OK', 'id': str(election.key()), 'voters': list(models.get_voter_set(election)) } #'voters': sorted(list(models.get_voter_set(election)))} logging.info(data) panel = get_panel(PAGE_URL, data, data.get('id')) webapputils.render_page_content(self, PAGE_URL, panel)
def get(self): # Authenticate user voter = auth.get_voter(self) status = models.get_admin_status(voter) if not status: webapputils.render_page(self, '/templates/message', {'status': 'Not Authorized', 'msg': MSG_NOT_AUTHORIZED}) return # Get election election = auth.get_election() if not election: panel = get_panel( PAGE_URL, {'status': 'ERROR','msg': 'No election found.'}, None) webapputils.render_page_content(self, PAGE_URL, panel) return if election.universal: panel = get_panel( PAGE_URL, {'status': 'Universal Election', 'msg': 'This is a universal election, anyone with a valid ' 'NetID can vote for. Therefore you cannot manage ' 'the voters list.'}, None) webapputils.render_page_content(self, PAGE_URL, panel) return # check to see if task already in task queue, indicates voters still being added # TODO: Don't add blank task, just see if there is already something working in the queue. # try: # response = requests.get('https://www.googleapis.com/taskqueue/v1beta2/projects/owlection/taskqueues/voters/tasks') # # # taskqueue.add(name=str(election.key()), # # queue_name='voters') # except taskqueue.TaskAlreadyExistsError: # panel = get_panel( # PAGE_URL, # {'status': 'Adding Voters', # 'msg': 'Come back later. Still adding voters.'}, # None) # webapputils.render_page_content(self, PAGE_URL, panel) # return #checks to see if the voter set exists (in mem cache), #if not indicates voters still being added # voter_set = memcache.get(str(election.key())+'-voter-set') # if not voter_set: # deferred.defer(models.update_voter_set, election, _name=str(election.key()), _queue='voters') # panel = get_panel( # PAGE_URL, # {'status': 'Adding Voters', # 'msg': 'Come back later. Still adding voters.'}, # None) # webapputils.render_page_content(self, PAGE_URL, panel) # return data = {'status': 'OK', 'id': str(election.key()), 'voters': list(models.get_voter_set(election))} #'voters': sorted(list(models.get_voter_set(election)))} logging.info(data) panel = get_panel(PAGE_URL, data, data.get('id')) webapputils.render_page_content(self, PAGE_URL, panel)
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)
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 get(self): page_data = {'open_elections': [], 'election_results': []} # Authenticate user voter = auth.get_voter(self) # Elections the user is eligible to vote for elections = voter.elections election_keys = [election.key() for election in elections] # Add universal elections universal_elections = models.Election.gql( "WHERE universal=TRUE AND hidden=FALSE") for election in universal_elections: if datetime.datetime.now() < election.end and election.key( ) not in election_keys: elections.append(election) logging.info(elections) for election in elections: logging.info('Found election') data = {} data['id'] = str(election.key()) data['name'] = election.name data['organization'] = election.organization.name now = datetime.datetime.now() if now > election.end: # Election passed result_delay = election.result_delay data['end_date'] = election.end.strftime( '%a, %B %d, %Y, %I:%M %p') + ' UTC' data['result_delay'] = result_delay # Compute how much time the user will have to wait to view the election time_since_election_end = (now - election.end).seconds + ( now - election.end).days * 86400 if time_since_election_end > result_delay: data['time_remaining'] = -1 else: data['time_remaining'] = (result_delay - time_since_election_end) * 1000 page_data['election_results'].append(data) else: data['user_action'] = 'not_started' # Initial assumption # Check election times if election.start > now: start_str = election.start.strftime( '%a, %B %d, %Y, %I:%M %p') + ' UTC' data['status'] = { 'text': 'Voting begins on', 'date': start_str } data['user_action'] = 'not_started' else: end_str = election.end.strftime( '%a, %B %d, %Y, %I:%M %p') + ' UTC' data['status'] = { 'text': 'Voting ends on', 'date': end_str } data['user_action'] = 'vote' # Check to see if the user has already voted if models.voter_status(voter, election) == 'voted': data['user_action'] = 'voted' page_data['open_elections'].append(data) logging.info(data) webapputils.render_page(self, '/vote', page_data)
def get(self): """ Serves the election data to the front-end for display. """ 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 = models.Election.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 != 'invalid_time' and not models.get_admin_status( voter, election.organization): page_data['error_msg'] = 'You are not eligible to view results.' webapputils.render_page(self, PAGE_NAME, page_data) return if not election.result_computed: page_data['error_msg'] = 'Election results are not available yet.' webapputils.render_page(self, PAGE_NAME, page_data) return public_result_time = election.end if election.result_delay: public_result_time += timedelta(seconds=election.result_delay) if datetime.now() < public_result_time: # Check to see if the user is an election admin status = models.get_admin_status(voter, election.organization) if not status: page_data['error_msg'] = ( 'Election results are not available to the public yet. ' 'Please wait for %s longer.' % str(public_result_time - datetime.now())[:6]) 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['voter_net_id'] = voter.net_id page_data['positions'] = [] # 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 page_data['positions'].append(position) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)
def get(self): """ Serves the election data to the front-end for display. """ 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 = models.Election.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 != 'invalid_time' and not models.get_admin_status(voter, election.organization): page_data['error_msg'] = 'You are not eligible to view results.' webapputils.render_page(self, PAGE_NAME, page_data) return if not election.result_computed: page_data['error_msg'] = 'Election results are not available yet.' webapputils.render_page(self, PAGE_NAME, page_data) return public_result_time = election.end if election.result_delay: public_result_time += timedelta(seconds=election.result_delay) if datetime.now() < public_result_time: # Check to see if the user is an election admin status = models.get_admin_status(voter, election.organization) if not status: page_data['error_msg'] = ('Election results are not available to the public yet. ' 'Please wait for %s longer.' % str(public_result_time - datetime.now())[:6]) 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['voter_net_id'] = voter.net_id page_data['positions'] = [] # 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 page_data['positions'].append(position) logging.info(page_data) webapputils.render_page(self, PAGE_NAME, page_data)