def test_insert(self): votation_id = self.__votation__.votation_id u = Option(votation_id=votation_id,option_name = 'test.option') self.assertTrue( option_dao.insert_dto(u)) self.assertGreater(u.option_id, 0) ar = option_dao.load_options_by_votation(votation_id) self.assertEqual(1,len(ar)) u1 = ar[0] self.assertIsNotNone(u1) self.assertEqual(u.votation_id, u1.votation_id) self.assertEqual(u.option_name, u1.option_name) self.assertTrue(option_dao.delete_dto(u1)) ar = option_dao.load_options_by_votation(votation_id) self.assertEqual(0,len(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 votation_counting(v): option_list = option_dao.load_options_by_votation(v.votation_id) counting = [] for o in option_list: ar = count_votes_by_option(v.votation_id, o.option_id) m = maj_jud_result(o.option_id, ar) m.option_name = o.option_name counting.append(m) counting.sort() counting.reverse() return counting
def counts_votes_by_votation(votation_id): """ Count votes. Returns dict of dict {option_id: {jud_value: count, ... } ... } """ result = {} option_array = option_dao.load_options_by_votation(votation_id) for o in option_array: d = __counts_votes_by_options(votation_id, o.option_id) result[o.option_id] = d return result
def test_text_ok(self): votation_id = self.__votation__.votation_id option_dao.delete_options_by_votation(votation_id) self.assertTrue(option_dao.save_options_from_text(votation_id,"""test row 1 test row 2 test row 3""") ) ar = option_dao.load_options_by_votation(votation_id) self.assertEqual(3, len(ar)) self.assertEqual("TEST ROW 1", ar[0].option_name) self.assertEqual("TEST ROW 2", ar[1].option_name) self.assertEqual("TEST ROW 3", ar[2].option_name) option_dao.delete_options_by_votation(votation_id)
def save_vote(user_id, vote_key, votation_id, option_id): """ User choose only one option The jud is always 1 Insert an array of zeros with only one 1. """ option_list = option_dao.load_options_by_votation(votation_id) ar = [] for o in option_list: if o.option_id == option_id: ar.append(1) else: ar.append(0) return vote_bo.save_votes(user_id, vote_key, votation_id, ar)
def votation_detail_simple(v): options_array = option_dao.load_options_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_simple.counting_votes(v.votation_id) return render_template('simple_majority/votation_detail_template.html', pagetitle=_("Election details"), \ v=v, \ states=votation_dao.states, options_array=options_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 votesimplemaj(v): options_array = option_dao.load_options_by_votation(v.votation_id) if request.method == 'GET': return render_template('simple_majority/vote_template.html', pagetitle="Vota", \ v=v, options_array=options_array) if request.method == 'POST': vote_key = request.form["vote_key"] my_vote = request.form["my_vote"] result = vote_simple.save_vote(current_user.u.user_id, vote_key, v.votation_id, int(my_vote)) 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 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