Esempio n. 1
0
    def cast_boolean_voting_ballot(position):
        """
        Records a boolean voting ballot in the models. Modifies write-in
        ids of the dictionary to reflect the written-in candidate's id.

        Args:
            position{dictionary}: the verified position dictionary from client
        """
        election_position = models.BooleanVotingPosition.get(position['id'])
        ballot = models.BooleanVotingBallot(position=election_position)
        ballot.put()
        for cp in position['candidate_points']:
            if cp['points'] > 0:
                # Check for a write-in
                if cp['id'].startswith('write-in'):
                    epc = models.ElectionPositionCandidate.gql(
                        'WHERE election_position=:1 AND name=:2',
                        election_position, cp['name']).get()
                    if not epc:
                        epc = models.ElectionPositionCandidate(
                            election_position=election_position,
                            net_id=None,
                            name=cp['name'],
                            written_in=True)
                        epc.put()
                    cp['id'] = str(epc.key())
                epc = models.ElectionPositionCandidate.get(cp['id'])
                choice = models.BooleanVotingChoice(ballot=ballot,
                                                    candidate=epc,
                                                    points=cp['points'])
                choice.put()
        logging.info('Stored boolean choice ballot in models.')
Esempio n. 2
0
    def add_position(self, election, data):
        position = data['position']
        position_entry = models.get_position(position['name'],
                                             election.organization,
                                             create=True)

        # Store position
        if position['type'] == 'Ranked-Choice':
            ep = models.RankedVotingPosition(
                election=election,
                position=position_entry,
                vote_required=position['vote_required'],
                write_in_slots=position['write_in_slots'],
                description=position['description'])
            ep.put()
        elif position['type'] == 'Cumulative-Voting':
            ep = models.CumulativeVotingPosition(
                election=election,
                position=position_entry,
                vote_required=position['vote_required'],
                write_in_slots=position['write_in_slots'],
                points=position['points'],
                slots=position['slots'],
                description=position['description'])
            ep.put()

        # Store candidates
        for candidate in position['candidates']:
            models.ElectionPositionCandidate(election_position=ep,
                                             name=candidate['name']).put()

        out = {'status': 'OK', 'msg': 'Created', 'position': ep.to_json()}
        self.response.write(json.dumps(out))
Esempio n. 3
0
 def cast_ranked_choice_ballot(position):
     """
     Records a ranked choice ballot in the models. Modifies write-in ids of the dictionary to reflect the 
     written-in candidate's id.
     
     Args:
         position{dictionary}: the verified position dictionary from the client
     """
     election_position = models.RankedVotingPosition.get(position['id'])
     preferences = [None] * len(position['candidate_rankings'])
     for cr in position['candidate_rankings']:
         
         # Check for a write-in
         if cr['id'].startswith('write-in'):
             epc = models.ElectionPositionCandidate.gql('WHERE election_position=:1 AND name=:2',
                                                          election_position, cr['name']).get()
             if not epc:
                 epc = models.ElectionPositionCandidate(election_position=election_position,
                                                          net_id=None,
                                                          name=cr['name'],
                                                          written_in=True)
                 epc.put()
             cr['id'] = str(epc.key())
         rank = cr['rank']
         preferences[rank-1] = models.ElectionPositionCandidate.get(cr['id']).key()
     
     logging.info(preferences)
     assert None not in preferences
     ballot = models.RankedBallot(position=election_position,
                                    preferences=preferences)
     ballot.put()
     logging.info('Stored ballot in database with preferences %s', preferences)
Esempio n. 4
0
    def update_position(self, election, data):
        position_data = data['position']
        ep = models.ElectionPosition.get(position_data['id'])
        position_entry = models.get_position(position_data['name'],
                                             election.organization,
                                             create=True)
        # Can't change position type
        assert (position_data['type'] == ep.position_type)
        ep.position = position_entry
        ep.vote_required = position_data['vote_required']
        ep.write_in_slots = position_data['write_in_slots']
        ep.description = position_data['description']
        if ep.position_type == 'Cumulative-Voting':
            ep.points = position_data['points']
            ep.slots = position_data['slots']
        ep.put()

        # Delete existing candidates
        for candidate in ep.election_position_candidates:
            candidate.delete()

        # Store candidates
        for candidate in position_data['candidates']:
            models.ElectionPositionCandidate(election_position=ep,
                                             name=candidate['name']).put()

        out = {'status': 'OK', 'msg': 'Updated', 'position': ep.to_json()}

        self.response.write(json.dumps(out))