Beispiel #1
0
def delete_party(party_id):
    if User().i_am_admin(get_jwt_identity()):
        party = PoliticalParties().get_one_party(party_id)
       
        if not party:
            abort(Serializer.error_fn(404, 'Political party cannot be found'))

        return Serializer.json_success('Political Party deleted successfully', \
             PoliticalParties().delete_party(party_id), 200), 200
        
    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #2
0
def delete_office(office_id):
    if User().i_am_admin(get_jwt_identity()):
        office = PoliticalOffices().get_one_office(office_id)

        if office:
            return Serializer.json_success('Political Office deleted successfully',\
                 PoliticalOffices().delete_office(office_id), 200), 200
        
        abort(Serializer.error_fn(404, 'Political office cannot be found'))
        
    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #3
0
def post_user():
    """Route for registering a user"""
    try:
        data = request.get_json()
        email = data['email']
        firstname = data['firstname']
        lastname = data['lastname']
        username = data['username']
        password = data['password']
        phone = data['phone']
        
    except KeyError:
        abort(Serializer.error_fn(400, 'Check if all fields exist'))

    """Checks if fields are either of type string or integer"""
    Validators().is_str_or_int(firstname, lastname, username, password, phone)

    """Checks if fields are empty"""
    Validators().is_space_or_empty(firstname, lastname, username, email, phone, password,
    firstname=firstname, lastname=lastname,username=username, email=email, phone=phone, password=password)
    
    """Checks if firstname and lastname contain any digits"""
    Validators().is_digit(firstname, lastname)
   
    """Checks if phone number contain any digits"""
    Validators().is_not_digit(phone)

    """Checks if field content has spaces in between"""
    Validators().has_space(firstname, lastname, username, password, phone)
    
    """Checks if password is valid"""
    Validators().check_valid_password(password)
    
    """Checks if email is valid"""
    Validators().valid_email(email)
    
    """Checks if phone number is valid"""
    Validators().valid_phone_number(phone)

    if User().get_user_by_username(username):
        abort(Serializer.error_fn(400, 'User already exists'))

    password = User().generate_hash(password)
    new_user = User().register(firstname, lastname, username, email, phone, password)
    return Serializer.json_success('User signed up successfully', new_user, 201), 201
Beispiel #4
0
def login():
    """ Route for signing in a user"""
    try:
        data = request.get_json()
        username = data['username']
        password = data['password']
    except KeyError:
        abort(Serializer.error_fn(400, 'Check if all fields exist'))

    if User().get_user_by_username(username):
        if User().password_is_valid(username, password) == False:
            abort(Serializer.error_fn(400, 'Check if credentials are correct'))
        
        access_token = User().user_login(username)
        if access_token:
            return Serializer.signup_success('You are now logged in', access_token, 201), 201
    
    abort(Serializer.error_fn(404, 'User does not exist'))
Beispiel #5
0
def post_office():
    if User().i_am_admin(get_jwt_identity()):
        data = request.get_json()
        try:
            office_name = data["office_name"]
            office_type = data["office_type"]
            location = data["location"]
            
        except KeyError:
            abort(Serializer.error_fn(400, 'Check if all fields exist'))
        
        types_of_offices = ['Legislative', 'Executive', 'County']

        """Check if fields are strings"""
        Validators().is_str_or_int(office_type, office_name, location)
        
        """Check if fields have digits"""
        Validators().is_digit(office_name, office_type, location)
        
        """Checks if fields are empty"""
        Validators().is_space_or_empty(office_type, office_name, location, 
        office_type=office_type, office_name=office_name, location=location)
        
        """Checks if office type is valid"""
        Validators().valid_office_type(office_type, types_of_offices)

        """Checks if office type matches office name"""
        Validators().valid_legilative_office(office_type, office_name)

        Validators().valid_executive_office(office_type, office_name)
        
        Validators().valid_executive_location(office_type, location)

        Validators().valid_county_office(office_type, office_name)

        if PoliticalOffices().check_office_exists_by_name(office_name, location):
            abort(Serializer.error_fn(400, 'Political office already exists'))

        office = PoliticalOffices().create_office(office_name, office_type, location)
        return Serializer.json_success('Political office created successfully', office, 201), 201

    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #6
0
def post_party():
    if User().i_am_admin(get_jwt_identity()):
        """route for creating a new political party"""
        try:
            data = request.get_json()
            party_name = data['party_name']
            hqAddress = data['hqAddress']
            logoUrl = data['logoUrl']
        
        except KeyError:
            abort(Serializer.error_fn(400, 'Check if all fields exist'))
        
        """Check if fields are strings"""
        Validators().is_str_or_int(party_name, hqAddress, logoUrl)
        
        """Check if hqAddress are has digits only"""
        Validators().all_digits(hqAddress)
        
        """Check if hqAddress are has both digits and letters"""
        Validators().is_digit_or_letter(hqAddress)

        """Check if party name has letters and spaces"""
        Validators().is_alpha_or_space(party_name)

        """Checks if fields are empty"""
        Validators().is_space_or_empty(party_name, hqAddress, logoUrl, 
        party_name=party_name, hqAddress=hqAddress, logoUrl=logoUrl)
        
        """Checks if logo url is valid"""
        Validators().valid_logo_url(logoUrl)

        if PoliticalParties().check_party_exists_by_name(party_name):
            abort(Serializer.error_fn(400, 'Political party already exists'))
        
        party = PoliticalParties().create(party_name, hqAddress, logoUrl)
        
        return Serializer.json_success('Political party created successfully', party, 201), 201      

    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #7
0
def vote():
    """Route for casting a vote"""
    current_user = get_jwt_identity()
    if not User().i_am_admin(current_user):
        try:
            user_id = current_user["user_id"]

        except KeyError:
            abort(Serializer.error_fn(400, 'You are not registered'))

        try:
            data = request.get_json()
            office = data['office']
            candidate = data['candidate']

        except KeyError:
            abort(Serializer.error_fn(400, 'Check if all fields exist'))
        """Checks if fields are integers"""
        Validators().is_int(office, candidate)

        if not User().find_by_user_id(candidate):
            abort(Serializer.error_fn(400, 'User does not exist'))

        if not PoliticalOffices().get_one_office(office):
            abort(Serializer.error_fn(400, 'Office does not exist'))

        candidate_registered = Candidate().check_candidate_registered(
            candidate, office)
        if not candidate_registered:
            abort(Serializer.error_fn(400, 'Candidate not registered'))

        if Vote().has_voted(office, user_id):
            abort(
                Serializer.error_fn(
                    400,
                    'You have already voted for a candidate in this office'))

        response = Vote().cast_vote(office, user_id, candidate)
        return Serializer.json_success('You successfully cast your vote',
                                       response, 201), 201

    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #8
0
def post_candidate(office_id):
    if User().i_am_admin(get_jwt_identity()):
        """ Route for registering a candidate"""
        try:
            data = request.get_json()
            party = data['party']
            candidate = data['candidate']

        except KeyError:
            abort(Serializer.error_fn(400, 'Check if all fields exist'))
        """Checks if fields are integers"""
        Validators().is_int(party, candidate, office_id)
        """cannot register admin as a candidate"""
        if User().get_admin_by_id(candidate):
            abort(
                Serializer.error_fn(
                    401,
                    'You are not authorized to register admin as a candidate'))

        if not User().find_by_user_id(candidate):
            abort(Serializer.error_fn(404, 'Candidate does not exist'))

        if not PoliticalOffices().get_one_office(office_id):
            abort(Serializer.error_fn(404, 'Office does not exist'))

        if not PoliticalParties().get_one_party(party):
            abort(Serializer.error_fn(404, 'Party does not exist'))

        if Candidate().check_candidate_registered(candidate, office_id):
            abort(Serializer.error_fn(400, 'Candidate is already registered'))

        candidate = Candidate().register_candidate(office_id, party, candidate)
        return Serializer.json_success('Candidate registered successfully',
                                       candidate, 201), 201

    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #9
0
def update_party(party_id):
    if User().i_am_admin(get_jwt_identity()):
        try:
            data = request.get_json()
            party_name = data['party_name']
        
        except KeyError:
            abort(Serializer.error_fn(400, 'Party name key is missing'))
        
        edit_party = PoliticalParties().edit_party(party_id, party_name)
        if not edit_party:
            abort(Serializer.error_fn(404, 'Political party cannot be found'))

        if party_name == "" or party_name.isspace():
            abort(Serializer.error_fn(400, 'Party name is required'))
        
        if not all(x.isalpha() or x.isspace() for x in party_name):
            abort(Serializer.error_fn(400, 'Name should only have letters and spaces'))
        
        return Serializer.json_success('Political party updated successfully', edit_party, 200), 200

    abort(Serializer.error_fn(401, 'User not authorized to make this request'))
Beispiel #10
0
def get_party(party_id):
    party = PoliticalParties().get_one_party(party_id)
    if party:
       return Serializer.json_success('Political party retrieved successfully', party, 200), 200
    
    abort(Serializer.error_fn(404, 'Political party cannot be found'))
Beispiel #11
0
def get_parties():
    parties = PoliticalParties().get_all_parties()
    if parties:
        return Serializer.json_success('All political parties retrieved successfully', parties, 200), 200
    
    abort(Serializer.error_fn(400, 'Political parties cannot be found'))
Beispiel #12
0
def get_office(office_id):
    office = PoliticalOffices().get_one_office(office_id)
    if office:
        return Serializer.json_success('Political office retrieved successfully', office, 200), 200
    
    abort(Serializer.error_fn(404, 'Political office cannot be found'))
Beispiel #13
0
def get_all_offices():
    offices = PoliticalOffices().get_all_offices()
    if offices:     
        return Serializer.json_success('All political offices retrieved successfully', offices, 200), 200
    
    abort(Serializer.error_fn(404, 'Political office cannot be found'))
Beispiel #14
0
def get_results(office_id):
    """Get election results"""
    results = Vote().results_per_office(office_id)
    return Serializer.json_success("Election results retrieved successfully",
                                   results, 200), 200