def updateVotingChoice(self, form): ###NEW### global choiceResultArray global activeVotingQuestion if (activeVotingQuestion): choiceResultArray.clear() activeVotingQuestion = False print("array of choices", choiceResultArray) ######## altID = form['altID'] vID = form['vID'] votes = form['votes'] if not VotingChoiceDAO().getVotingChoiceByID(altID): return jsonify(Error="Voting Choice not found."), 404 else: if len(form) != 3: return jsonify(Error="Malformed update request"), 400 else: if votes and altID and vID: VotingChoiceDAO().updateVotingChoice(votes, altID) result = self.mapToUpdateVotingChoiceDict( vID, altID, votes) return jsonify(Choice=result), 201 else: return jsonify( Error="Unexpected attributes in update request"), 400
def insertChoiceJSON(self, json): """ Insert voting alternative :param json: vID, choice, votes :return: json file containing the information of the recently inserted choice """ vID = json.get('vID') choice = json.get('choice') votes = 0 if not VotingQuestionDAO().getVotingQuestionByID(vID): return jsonify(Error="VOTING NOT FOUND"), 404 else: if vID and choice: altID = VotingChoiceDAO().insertVotingChoice( vID, choice, votes) mapped_result = self.builtVotingChoiceDict( altID, vID, choice, votes) return jsonify(Choice=mapped_result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 404
def getVotingChoiceByVID(self, vID): # Handler for getting the voting choices of a voting result = VotingChoiceDAO().getVotingChoiceByVID(vID) mapped_result = [] if not result: return jsonify(Error="NOT FOUND"), 404 else: for r in result: mapped_result.append(self.mapVotingChoiceByVID(r)) return jsonify(Choice=mapped_result)
def updateVotingChoice(self, form): """ Update the voting choice count by adding one to its votes count :param form: altID, mID :return: json containing the recently updated voting choice """ altID = form['altID'] mID = form['mID'] status = VotingQuestionDAO().getActiveVotingQuestionBymID(mID) if not VotingChoiceDAO().getVotingChoiceByID(altID): return jsonify(Error="Voting Choice not found."), 404 else: if len(form) != 2: return jsonify(Error="Malformed update request"), 400 else: if altID and status: VotingChoiceDAO().updateVotingChoice(altID) result = self.mapToUpdateVotingChoiceResult(altID) return jsonify(Choice=result), 201 else: return jsonify( Error="Unexpected attributes in update request"), 400
def getActiveVotingChoiceByvID(self, vID): """ Handler for getting the active voting alternatives of a voting :param vID: voting id :return: """ global activeVotingQuestion global choiceResultArray result = VotingChoiceDAO().getActiveVotingChoiceByVID(vID) mapped_result = [] if not result: return jsonify(Error="ACTIVE VOTING NOT FOUND"), 404 else: for r in result: mapped_result.append(self.mapVotingChoiceByVID(r)) return jsonify(Choice=mapped_result), 200
def getActiveVotingChoiceByvID(self, vID): # Handler for getting the active voting alternatives of a voting global activeVotingQuestion global choiceResultArray result = VotingChoiceDAO().getActiveVotingChoiceByVID(vID) mapped_result = [] if not result: return jsonify(Error="ACTIVE VOTING NOT FOUND"), 404 else: for r in result: mapped_result.append(self.mapVotingChoiceByVID(r)) ##This is new###################################### #Stores the choices when there is an active voting question. Store the choices # once the first load is executed if (activeVotingQuestion == False): print(choiceResultArray) choiceResultArray = mapped_result activeVotingQuestion = True #################################################### return jsonify(Choice=mapped_result), 200
def insertChoiceJSON(self, json): # Insert voting alternative vID = json.get('vID') choice = json.get('choice') votes = 0 if not VotingQuestionDAO().getVotingQuestionByID(vID): return jsonify(Error="VOTING NOT FOUND"), 404 else: if vID and choice: altID = VotingChoiceDAO().insertVotingChoice( vID, choice, votes) mapped_result = self.builtVotingChoiceDict( altID, vID, choice, votes) return jsonify(Choice=mapped_result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 404