def validate_ballot(parameters): """ Validate a ballot's parameters based on constraints. :param parameters: The ballot's parameters. :return: None """ # check if election exists specified_election = elections_alchemy.query_election(election_id=parameters['election']) if specified_election == list(): raise exceptions.NotFound404Exception('election with specified ID not found') # check if position exists and is active specified_position = elections_alchemy.query_position(position_id=parameters['position']) if specified_position == list() or specified_position[0].active is False: raise exceptions.Forbidden403Exception('position with specified ID not found') # check if position is the right election type if specified_position[0].election_type != specified_election[0].election_type: raise exceptions.Forbidden403Exception('you are creating a vote for a position in a different election type') # check for valid candidate username if mask_alchemy.query_by_username(parameters['vote']) is None: raise exceptions.Forbidden403Exception('you cannot create a vote for this candidate') # check for duplicate votes if elections_alchemy.query_vote(election_id=specified_election[0].id, position_id=specified_position[0].id, vote=parameters['vote'], username=parameters['username']) != list(): raise exceptions.Forbidden403Exception('this candidate has already been voted for by this user')
def post(self): # get current user user = self.current_user # load request body body = self.request.body.decode('utf-8') body_json = json.loads(body) # validate parameters required_parameters = ('election', 'position', 'vote') elections_validator.validate_parameters(body_json, required_parameters) body_json['username'] = str(user.username) elections_validator.validate_vote(body_json) # check for too many votes specified_election = elections_alchemy.query_election(election_id=body_json['election']) specified_position = elections_alchemy.query_position(position_id=body_json['position']) if len(elections_alchemy.query_vote(election_id=specified_election[0].id, position_id=specified_position[0].id, username=str(user.username))) > specified_election[0].max_votes: raise exceptions.Forbidden403Exception( 'you may only cast {} vote/s'.format(str(specified_election[0].max_votes)) ) if specified_election[0].election_type == 'senate': votes = elections_alchemy.query_vote(election_id=specified_election[0].id, username=str(user.username)) for vote in votes: if vote.position != body_json['position']: elections_alchemy.delete(vote) # create new vote vote = elections_model.Vote() for parameter in required_parameters: setattr(vote, parameter, body_json[parameter]) setattr(vote, 'username', str(user.username)) elections_alchemy.add_or_update(vote) # response self.set_status(201) self.write(vote.serialize())
def get(self, election_id, ballot_id): # get vote vote = elections_alchemy.query_vote(vote_id=str(ballot_id), election_id=election_id, manual_entry=True) # validate vote ID if vote == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') # response self.set_status(200) self.write(vote[0].serialize_ballot())
def delete(self, election_id, ballot_id): # get vote vote = elections_alchemy.query_vote(vote_id=str(ballot_id), election_id=election_id, manual_entry=True) # validate vote ID if vote == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') vote = vote[0] # delete the candidate elections_alchemy.delete(vote) # response self.set_status(204)
def post(self, election_id): # get current user user = self.current_user # load request body body = self.request.body.decode('utf-8') body_json = json.loads(body) # validate parameters required_parameters = ('election', 'position', 'student_id', 'vote') elections_validator.validate_parameters(body_json, required_parameters) # get student username from student ID voter = mask_alchemy.query_by_wwuid(mask_model.User, body_json['student_id']) if voter == list(): raise exceptions.NotFound404Exception('user with specified student ID not found') voter = voter[0] # build proper vote from ballot data and validate it body_json['election'] = str(election_id) body_json['username'] = str(voter.username) body_json['manual_entry'] = str(user.username) elections_validator.validate_ballot(body_json) # check for too many votes specified_election = elections_alchemy.query_election(election_id=body_json['election']) specified_position = elections_alchemy.query_position(position_id=body_json['position']) if len(elections_alchemy.query_vote(election_id=specified_election[0].id, position_id=specified_position[0].id, username=str(voter.username))) >= specified_election[0].max_votes: raise exceptions.Forbidden403Exception( 'this user has already cast {} vote/s'.format(str(specified_election[0].max_votes)) ) # create new vote vote = elections_model.Vote() for parameter in required_parameters: if parameter != 'student_id': setattr(vote, parameter, body_json[parameter]) setattr(vote, 'username', str(voter.username)) setattr(vote, 'manual_entry', str(user.username)) elections_alchemy.add_or_update(vote) # response self.set_status(201) self.write(vote.serialize_ballot())
def get(self, election_id): # build query parameter dict search_criteria = build_query_params(self.request.arguments) # get the specified election specified_election = elections_alchemy.query_election(election_id=str(election_id)) if specified_election == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') specified_election = specified_election[0] # get votes votes = elections_alchemy.query_vote(election_id=specified_election.id, position_id=search_criteria.get('position', None), vote=search_criteria.get('vote', None), manual_entry=True) # response self.set_status(200) self.write({'ballots': [v.serialize_ballot() for v in votes]})
def delete(self, vote_id): # get vote vote = elections_alchemy.query_vote(vote_id=str(vote_id)) if vote == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') vote = vote[0] # get election election = elections_alchemy.query_election(election_id=str(vote.election))[0] # dont allow deleting past candidates if election != elections_alchemy.query_current(): raise exceptions.Forbidden403Exception('vote not in the current election') # delete the vote elections_alchemy.delete(vote) # response self.set_status(204)
def get(self, vote_id): # get current user user = self.current_user # get current election current_election = elections_alchemy.query_current() if current_election is None: raise exceptions.Forbidden403Exception('there is currently no open election') # get vote vote = elections_alchemy.query_vote(vote_id=str(vote_id), election_id=current_election.id, username=str(user.username)) # validate vote ID if vote == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') # response self.set_status(200) self.write(vote[0].serialize())
def get(self): # get current user user = self.current_user # build query parameter dict search_criteria = build_query_params(self.request.arguments) # get current election current_election = elections_alchemy.query_current() if current_election is None: raise exceptions.NotFound404Exception('there is currently no open election') # get votes votes = elections_alchemy.query_vote(election_id=current_election.id, username=str(user.username), position_id=search_criteria.get('position', None), vote=search_criteria.get('vote', None)) # response self.set_status(200) self.write({'votes': [v.serialize() for v in votes]})
def validate_vote(parameters, existing_vote=None): """ Validate a vote's parameters based on constraints. :param parameters: The vote's parameters. :param existing_vote: An existing vote being updated used to check for duplicate votes. :return: None """ # check if election exists specified_election = elections_alchemy.query_election(election_id=parameters['election']) if specified_election == list(): raise exceptions.NotFound404Exception('election with specified ID not found') # check if not current election current_election = elections_alchemy.query_current() if specified_election[0] != current_election: raise exceptions.Forbidden403Exception('this election is not available for voting') # check if position exists and is active specified_position = elections_alchemy.query_position(position_id=parameters['position']) if specified_position == list() or specified_position[0].active is False: raise exceptions.Forbidden403Exception('position with specified ID not found') # check if position is the right election type if specified_position[0].election_type != current_election.election_type: raise exceptions.Forbidden403Exception('you are voting for a position in a different election type') # check for valid candidate username if mask_alchemy.query_by_username(parameters['vote']) is None: raise exceptions.Forbidden403Exception('you cannot vote for this person') # check for duplicate votes if parameters['vote'] != getattr(existing_vote, 'vote', None) and \ elections_alchemy.query_vote(election_id=specified_election[0].id, position_id=specified_position[0].id, vote=parameters['vote'], username=parameters['username']) != list(): raise exceptions.Forbidden403Exception('you have already voted for this person')
def put(self, vote_id): # get current user user = self.current_user # load request body body = self.request.body.decode('utf-8') body_json = json.loads(body) # get current election current_election = elections_alchemy.query_current() if current_election is None: exceptions.Forbidden403Exception('there is currently no open election') # get vote vote = elections_alchemy.query_vote(vote_id=vote_id, election_id=current_election.id, username=str(user.username)) if vote == list(): raise exceptions.NotFound404Exception('vote with specified ID not found') vote = vote[0] # validate parameters required_parameters = ('id', 'election', 'position', 'vote', 'username') elections_validator.validate_parameters(body_json, required_parameters) body_json['username'] = str(user.username) elections_validator.validate_vote(body_json, vote) # update vote for parameter in required_parameters: if parameter not in ('id', 'username'): setattr(vote, parameter, body_json[parameter]) setattr(vote, 'username', str(user.username)) elections_alchemy.add_or_update(vote) # response self.set_status(200) self.write(vote.serialize())