def count_votes_by_option(votation_id, option_id): ar = [] jud_array = judgement_dao.load_judgement_by_votation(votation_id) for j in range(len(jud_array)): n = db.session.query(Vote).filter(Vote.votation_id == votation_id, Vote.option_id == option_id, Vote.jud_value == j).count() ar.append( n ) return ar
def test_insert_votation_and_options_1(self): descr = 'Votation and options 1 automated test ' + str(random.randint(0,500)) v = Votation( \ votation_description = descr , \ description_url = "" , \ votation_type = votation_dao.TYPE_MAJORITY_JUDGMENT , \ promoter_user_id = 1 , \ begin_date = datetime(2018,1,1) , \ end_date = datetime(2018,1,15) , \ votation_status = 2 , \ list_voters = 0) txt_options = "option_test_A\noption_test_B\n option_test_C" txt_judgements = "judgment_test_A\njudgment_test_B\n judgment_test_C" result = votation_bo.insert_votation_with_options(v,txt_options,txt_judgements) self.assertEqual(MSG_OK,result[1]) ar = votation_dao.load_votations() check = False for w in ar: if w.votation_description == descr: check = True break self.assertTrue(check) opt_ar = option_dao.load_options_by_votation(w.votation_id) self.assertEqual(3,len(opt_ar)) jud_ar = judgement_dao.load_judgement_by_votation(w.votation_id) self.assertEqual(3,len(jud_ar)) votation_bo.deltree_votation_by_id(w.votation_id)
def __counts_votes_by_options(votation_id,option_id): """ Count votes for an option. Returns dict {jud_value: count, ...} """ d = {} jud_array = judgement_dao.load_judgement_by_votation(votation_id) for j in jud_array: n = __counts_votes_by_options_and_jud(votation_id,option_id, j.jud_value) d[j.jud_value] = n return d
def count_votes_by_option(votation_id, option_id): """ It returns an array of counts. The array contains one count for each rank for one option. """ jud_array = judgement_dao.load_judgement_by_votation(votation_id) ar = [] for j in range(len(jud_array)): n = db.session.query(Vote).filter(Vote.votation_id == votation_id, Vote.option_id == option_id, Vote.jud_value == j).count() ar.append(n) return ar
def test_text_ok(self): votation_id = self.__votation__.votation_id self.assertTrue( judgement_dao.save_judgements_from_text( votation_id, """A X B""")) ar = judgement_dao.load_judgement_by_votation(votation_id) self.assertEqual(3, len(ar)) self.assertEqual("B", ar[0].jud_name) self.assertEqual("X", ar[1].jud_name) self.assertEqual("A", ar[2].jud_name) self.assertEqual(0, ar[0].jud_value) self.assertEqual(1, ar[1].jud_value) self.assertEqual(2, ar[2].jud_value)
def votation_detail_maj_jud(v): options_array = option_dao.load_options_by_votation(v.votation_id) juds_array = judgement_dao.load_judgement_by_votation(v.votation_id) counting = None is_voter = voter_dao.is_voter(v.votation_id, current_user.u.user_id) if v.votation_status == votation_dao.STATUS_ENDED: counting = vote_maj_jud.votation_counting(v) return render_template('majority_jud/votation_detail_template.html', pagetitle=_("Election details"), \ v=v, \ states=votation_dao.states, options_array=options_array,juds_array=juds_array, \ count_voters=voter_dao.count_voters(v.votation_id), \ count_votes=vote_dao.count_votes(v.votation_id), \ votation_timing=votation_dao.votation_timing(v),counting=counting, \ type_description=votation_dao.TYPE_DESCRIPTION, \ is_voter=is_voter)
def setUp(self): self.__votation__ = Votation( \ votation_description = 'Simple Votation with voters for vote test ' + str(random.randint(0,50000)) , \ description_url = "" , \ votation_type = votation_dao.TYPE_SIMPLE_MAJORITY , \ promoter_user_id = 1 , \ begin_date = datetime(2018,1,1) , \ end_date = datetime(2018,1,15) , \ votation_status = 2 , \ list_voters = 1) self.assertTrue(votation_dao.insert_votation_dto(self.__votation__)) o1 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option1') self.assertTrue(option_dao.insert_dto(o1)) o2 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option2') self.assertTrue(option_dao.insert_dto(o2)) o3 = Option(votation_id=self.__votation__.votation_id, \ option_name = 'test.option3') self.assertTrue(option_dao.insert_dto(o3)) self.__option1 = o1 self.__option2 = o2 self.__option3 = o3 self.assertIsNotNone(o1.option_id) self.assertIsNotNone(o2.option_id) self.assertIsNotNone(o3.option_id) # set juds j1 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 0, jud_name = "bad") j2 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 1, jud_name = "medium") j3 = Judgement(votation_id = self.__votation__.votation_id, \ jud_value = 2, jud_name = "good") judgement_dao.insert_dto(j1) judgement_dao.insert_dto(j2) judgement_dao.insert_dto(j3) db.session.commit() jud_array = judgement_dao.load_judgement_by_votation( self.__votation__.votation_id) self.__jud1 = jud_array[0] self.__jud2 = jud_array[1] self.__jud3 = jud_array[2] voter1 = Voter(user_id=1, votation_id=self.__votation__.votation_id, voted=0) voter_dao.insert_dto(voter1) db.session.commit() return super().setUp()
def save_vote(votation_id, vote_key, array_judgements): option_array = option_dao.load_options_by_votation(votation_id) jud_array = judgement_dao.load_judgement_by_votation(votation_id) if len(array_judgements) != len(option_array): return False valid_jud = [] for j in jud_array: valid_jud.append(j.jud_value) i = 0 for o in option_array: if array_judgements[i] not in valid_jud: return False v = Vote(vote_key = vote_key, \ votation_id = votation_id, \ option_id = o.option_id, \ jud_value = array_judgements[i]) insert_dto(v) i += 1 return True
def votemajjud(v): options_array = option_dao.load_options_by_votation(v.votation_id) if request.method == 'GET': return render_template('majority_jud/vote_template.html', pagetitle=_("Vote"), \ v=v, options_array=options_array,words_array=judgement_dao.load_judgement_by_votation(v.votation_id)) if request.method == 'POST': vote_key = request.form["vote_key"] vote_array = [] for c in options_array: param = "v_" + str(c.option_id) vote_array.append(int(request.form[param])) result = vote_maj_jud.save_votes(current_user.u.user_id, vote_key, v.votation_id, vote_array) if result: message = (_("Your vote has been registered"), MSG_OK) else: message = (_("Error. Vote NOT registered. Wrong Password?"), MSG_KO) return render_template('thank_you_template.html', pagetitle=_("Vote registering"), message=message)
def votation_counting(v): results = vote_dao.counts_votes_by_votation(v.votation_id) """ Results: {option_id_1 : {jud_value1: count1, jud_value2: count2, ... }, option_id_2: ...} Example {123: {0:23, 1:34, 2:10}, 124: {0:18, 1:21, 2:43} } """ counting = [] option_list = option_dao.load_options_by_votation(v.votation_id) jud_list = judgement_dao.load_judgement_by_votation(v.votation_id) for o in option_list: ar = [] for j in jud_list: ar.append(results[o.option_id][j.jud_value]) m = maj_jud_result(o.option_id,ar) m.option_name = o.option_name counting.append(m) counting.sort() counting.reverse() return counting