def candidate_2(election_number=None): data = get_payload() url = str(request.url_rule) if "<election_number>" in url and "user" not in url and app.draw[0]: wanted_candidate = random.choice(app.draw[1]) json_data = dict([("election_number", election_number), ("wanted_candidate", wanted_candidate)]) data = requests.post("http://127.0.0.1:5000/coordinator", data=json_data) return "Voted" elif "<election_number>" in url and "user" not in url: wanted_candidate = random.randrange(1, NO_OF_CANDIDATES + 1) selected_candidate = int(url.split("/")[1]) app.logger.info("Internal API called with candidate ID " + str(selected_candidate) + " and this candidate choose " + str(wanted_candidate) + " candidate ID") json_data = dict([("election_number", election_number), ("wanted_candidate", wanted_candidate)]) data = requests.post("http://127.0.0.1:5000/coordinator", data=json_data) return "Voted" elif "user" in url: wanted_candidate = int(url.split("/")[1]) app.logger.info( "External API called by the user and the choosen candidate is " + str(wanted_candidate) + " candidate ID") json_data = dict([("election_number", election_number), ("wanted_candidate", wanted_candidate)]) data = requests.post("http://127.0.0.1:5000/coordinator", data=json_data) return "Your vote is succefull" else: app.pool.apply_async( initiate_voters, [int(request.url[-1]), data["election_number"], NO_OF_CANDIDATES]) wanted_candidate = random.randrange(1, NO_OF_CANDIDATES + 1) json_data = dict([("candidate_number", NO_OF_CANDIDATES), ("wanted_candidate", wanted_candidate)]) data.update(json_data) return jsonify(data)
def coordinator(): # election_id = id_generator() if request.method == "GET": election_id = id_generator() app.logger.info("Generated a {0} election number".format(election_id)) candidate_api = random.randrange(1, NO_OF_CANDIDATES + 1) app.logger.info( "Coordinator API called and new election ID is {0} and random selected candidate from total {1} candidates is {2}" .format(election_id, NO_OF_CANDIDATES, candidate_api)) data = requests.post("http://127.0.0.1:5000/{candidate_api}".format( candidate_api=candidate_api), data={"election_number": election_id}) save(data, election_id) data = data.json() temp_data = {} temp_data['initiator_number'] = candidate_api temp_data['election_number'] = data['election_number'] return jsonify(temp_data) elif request.method == "POST": data = get_payload() save(data, data['election_number']) return jsonify(data)