Example #1
0
    def put(self, election_id):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # get current election
        election = elections_alchemy.query_election(election_id=str(election_id))
        if election == list():
            raise exceptions.NotFound404Exception('election with specified ID not found')
        election = election[0]

        # validate parameters
        required_parameters = ('id', 'election_type', 'name', 'max_votes', 'start', 'end', 'show_results')
        elections_validator.validate_parameters(body_json, required_parameters)
        elections_validator.validate_election(body_json, election)

        # update election
        for parameter in required_parameters:
            if parameter in ('start', 'end'):
                d = datetime.strptime(body_json[parameter], '%Y-%m-%d %H:%M:%S.%f')
                setattr(election, parameter, d)
            elif parameter == 'show_results' and body_json[parameter] is not None:
                d = datetime.strptime(body_json[parameter], '%Y-%m-%d %H:%M:%S.%f')
                setattr(election, parameter, d)
            else:
                setattr(election, parameter, body_json[parameter])
        elections_alchemy.add_or_update(election)

        # response
        self.set_status(200)
        self.write(election.serialize())
Example #2
0
    def put(self, election_id, candidate_id):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # validate parameters
        required_parameters = ('id', 'election', 'position', 'username', 'display_name')
        elections_validator.validate_parameters(body_json, required_parameters)
        body_json['election'] = election_id
        elections_validator.validate_candidate(body_json)

        # get election
        election = elections_alchemy.query_election(election_id=str(election_id))
        if election == list():
            raise exceptions.NotFound404Exception('election with specified ID not found')

        # get candidate
        candidate = elections_alchemy.query_candidates(election_id=str(election_id), candidate_id=str(candidate_id))
        if candidate == list():
            raise exceptions.NotFound404Exception('candidate with specified ID not found')
        candidate = candidate[0]

        # update candidate
        for parameter in required_parameters:
            setattr(candidate, parameter, body_json[parameter])
        elections_alchemy.add_or_update(candidate)

        # response
        self.set_status(200)
        self.write(candidate.serialize())
Example #3
0
    def post(self):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # validate parameters
        required_parameters = ('election_type', 'name', 'max_votes', 'start', 'end', 'show_results')
        elections_validator.validate_parameters(body_json, required_parameters)
        elections_validator.validate_election(body_json)

        # create new election
        election = elections_model.Election()
        for parameter in required_parameters:
            if parameter in ('start', 'end'):
                d = datetime.strptime(body_json[parameter], '%Y-%m-%d %H:%M:%S.%f')
                setattr(election, parameter, d)
            elif parameter == 'show_results' and body_json[parameter] is not None:
                d = datetime.strptime(body_json[parameter], '%Y-%m-%d %H:%M:%S.%f')
                setattr(election, parameter, d)
            else:
                setattr(election, parameter, body_json[parameter])
        elections_alchemy.add_or_update(election)

        # response
        self.set_status(201)
        self.write(election.serialize())
Example #4
0
    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())
Example #5
0
    def post(self):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # validate parameters
        required_parameters = ('position', 'election_type', 'active', 'order')
        elections_validator.validate_parameters(body_json, required_parameters)
        elections_validator.validate_position(body_json)

        # create new position
        position = elections_model.Position()
        for parameter in required_parameters:
            setattr(position, parameter, body_json[parameter])
        elections_alchemy.add_or_update(position)

        # response
        self.set_status(201)
        self.write(position.serialize())
Example #6
0
    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())
Example #7
0
    def post(self, election_id):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # validate parameters
        required_parameters = ('position', 'username', 'display_name')
        elections_validator.validate_parameters(body_json, required_parameters)
        body_json['election'] = election_id
        elections_validator.validate_candidate(body_json)

        # create new candidate
        candidate = elections_model.Candidate()
        for parameter in required_parameters:
            setattr(candidate, parameter, body_json[parameter])
        setattr(candidate, 'election', str(election_id))
        elections_alchemy.add_or_update(candidate)

        # response
        self.set_status(201)
        self.write(candidate.serialize())
Example #8
0
    def put(self, position_id):
        # load request body
        body = self.request.body.decode('utf-8')
        body_json = json.loads(body)

        # validate parameters
        required_parameters = ('id', 'position', 'election_type', 'active', 'order')
        elections_validator.validate_parameters(body_json, required_parameters)
        elections_validator.validate_position(body_json)

        # get position
        position = elections_alchemy.query_position(position_id=str(position_id))
        if position == list():
            raise exceptions.NotFound404Exception('position with specified ID not found')
        position = position[0]

        # update position
        for parameter in required_parameters:
            setattr(position, parameter, body_json[parameter])
        elections_alchemy.add_or_update(position)

        # response
        self.set_status(200)
        self.write(position.serialize())
Example #9
0
    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())