def office_result(office): """get the result set from the database Args: office (integer): the office to get results for """ # verify the token provided by user is valid response, status = get_logged_in_user(request) if status == 200: # sql query to get a user results results_query = get_office_result(office) office_result = db().get_all_rows_of_value(*results_query) if office_result: # serialize office results serialize_results = results_schema.dump(office_result) # json results response response_object = jsonify({ "status": 200, "data": serialize_results }) return response_object, 200 # when no result is returned response_object = jsonify({ "status": 404, "message": "Results requested not found." }) return response_object, 404 # if user could not be verified return response, status
def decorated(*args, **kwargs): # pass flask.request as an argument to the get_logged_in_user() data, status = get_logged_in_user(request) try: # verify data response dictionary contains the 'user' key data.get_json()['user'] except KeyError: return data, status return func(*args, **kwargs)
def save_new_petition(json_data): """Save the petition to the petition table and return appropriate response to endpoint Args: json_data (json): petition details. """ try: data = petition_load_schema.load(json_data) except ValidationError as e: return jsonify({"status": 400, "error": e.messages}), 400 # get petition details from the validated json input office = data["office"] contested_by = data["contested_by"] body = data["body"] evidence = data["evidence"] # check endpoint authorization res, status = get_logged_in_user(request) if status == 200: # get the user id from the decoded token created_by = res.get_json()['user'].get('user_id') new_petition = Petition(office=office, contested_by=contested_by, created_by=created_by, body=body, evidence=evidence) try: # save the petition and return the id created thereafter petition_id = save_changes(new_petition) except IntegrityError: return jsonify({ "status": 404, "message": "Office and candidate referenced does not exist." }), 404 petition_query = Petition.get_petition_by_id(petition_id) petition = db().get_single_row(*petition_query) # serialize the petition details serialized_petition = petitions_dump_schema.dump(petition) response_object = jsonify({"status": 201, "data": serialized_petition}) return response_object, 201 # if status code == 401 return res, status
def save_new_vote(json_data): """Saves a new vote created by the signed on user Args: json_data (json): candidate and office data """ try: data = vote_load_schema.load(json_data) except ValidationError as e: return jsonify({"status": 400, "error": e.messages}), 400 office = data['office'] candidate = data['candidate'] # get authorization header from the request data, status = get_logged_in_user(request) if status == 200: # get the user id from the decoded token user_id = data.get_json()['user'].get('user_id') # create a new vote instance new_vote = Vote(office=office, candidate=candidate) vote_exists = get_vote_cast(user_id=user_id, office_id=office) try: if vote_exists is None: save_changes(_id=user_id, data=new_vote) else: return jsonify({ "status": 409, "error": "Vote already cast for office." }), 409 except IntegrityError: return jsonify({ "status": 404, "error": "Candidate and office referenced does not exist." }), 404 # serialize vote data cast_vote = get_vote_cast(user_id=user_id, office_id=office) response = vote_dump_schema.dump(cast_vote) response_object = jsonify({"status": 201, "data": [response]}) return response_object, 201 else: # json response for authentication error encountered return data, status
def decorated(*args, **kwargs): # pass flask.request as an argument to the get_logged_in_user() data, status = get_logged_in_user(request) try: # verify data response dictionary contains the 'user' key data.get_json()['user'] admin = data.get_json()['user'].get('is_admin') if not admin: # if the admin value is False response_object = jsonify({ 'status': 401, 'message': 'Admin token required.' }) return response_object, 401 except KeyError: return data, status return func(*args, **kwargs)
def save_new_application(json_data): """Create a user office application instance and save the data to the database Args: json_data (json): office user is vying for and the party name """ # deserialize the data input against the application schema # checks if the input values pass the field validation try: data = application_load_schema.load(json_data) except ValidationError as e: return jsonify({ "status": 400, "error": e.messages }), 400 party = data['party'] office = data['office'] # decode the auth token of logged-in user res, status = get_logged_in_user(request) if status == 200: # get user id from decoded token applicant_id = res.get_json()['user'].get('user_id') party_id = get_party_id(party) office_id = get_office_id(office) if party_id and office_id: new_application = Application( party=party_id, office=office_id ) try: save_changes(applicant_id, new_application) except IntegrityError: # Executed if the user has an application # already made before return jsonify({ "status": 400, "error": "User has an application registered already." }), 400 application_registered_query = Application.get_application( applicant_id) application_registered = db().get_single_row(*application_registered_query) response = application_dump_schema.dump(application_registered) response_object = jsonify({ "status": 201, "data": [response] }) return response_object, 201 return jsonify({ "status": 400, "error": "Party or office referenced does not exists." }), 400