Esempio n. 1
0
    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)
Esempio n. 2
0
    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)