Example #1
0
    def compute_results(self):
        candidates = self.poll.questions_data[0]['answers']
        candidates_count = len(candidates)
        count_id = 0

        ballots_data = self.poll.result[0]
        ballots = []
        for ballot in ballots_data:
            if ballot is None:
                continue

            decoded = gamma_decode(ballot, candidates_count, candidates_count)
            ballot = to_absolute_answers(decoded, candidates_count)
            ballots.append(ballot)

        computed = self.get_count_method()(ballots, range(candidates_count))
        results = {
            'wins_and_beats': computed[1],
            'ballots': ballots,
            'blanks': len(filter(lambda x: len(x) == 0, ballots))
        }

        self.poll.stv_results = json.dumps(results)
        self.poll.save()

        # build docs
        self.generate_json_file()
        for lang in settings.LANGUAGES:
            self.generate_result_docs(lang)
            self.generate_csv_file(lang)
Example #2
0
    def compute_results(self):
        candidates = self.poll.questions_data[0]['answers']
        candidates_count =  len(candidates)
        count_id = 0

        ballots_data = self.poll.result[0]
        ballots = []
        for ballot in ballots_data:
            if ballot is None:
                continue

            decoded = gamma_decode(ballot, candidates_count, candidates_count)
            ballot = to_absolute_answers(decoded, candidates_count)
            ballots.append(ballot)

        computed = self.get_count_method()(ballots, range(candidates_count))
        results = {
            'wins_and_beats': computed[1],
            'ballots': ballots,
            'blanks': len(filter(lambda x: len(x) == 0, ballots))
        }

        self.poll.stv_results = json.dumps(results)
        self.poll.save()

        # build docs
        self.generate_json_file()
        for lang in settings.LANGUAGES:
            self.generate_result_docs(lang)
            self.generate_csv_file(lang)
Example #3
0
    def compute_results(self):
        cands_data = self.poll.questions_data[0]['answers']
        cands_count = len(cands_data)
        constituencies = {}
        count_id = 0

        for item in cands_data:
            cand_and_dep = item.split(':')
            constituencies[str(count_id)] = cand_and_dep[1]
            count_id += 1

        seats = self.poll.eligibles_count
        droop = self.poll.stv_droop
        rnd_gen = None  # TODO: should be generated and stored on poll freeze
        if self.poll.has_department_limit:
            quota_limit = self.poll.department_limit
        else:
            quota_limit = 0
        ballots_data = self.poll.result[0]
        ballots = []
        for ballot in ballots_data:
            if not ballot:
                continue
            ballot = to_absolute_answers(
                gamma_decode(ballot, cands_count, cands_count), cands_count)
            ballot = [str(i) for i in ballot]
            ballots.append(Ballot(ballot))

        stv_stream = StringIO.StringIO()
        stv_logger = logging.Logger(self.poll.uuid)
        handler = logging.StreamHandler(stv_stream)
        stv_logger.addHandler(handler)
        stv_logger.setLevel(logging.DEBUG)
        self.poll.logger.info(
            "Count STV results [seats:{} droop:{} quota_limit:{}]".format(
                seats, droop, quota_limit))
        results = count_stv(ballots,
                            seats,
                            droop,
                            constituencies,
                            quota_limit,
                            rnd_gen,
                            logger=stv_logger)
        results = list(results[0:2])
        handler.close()
        stv_stream.seek(0)
        results.append(stv_stream.read())
        stv_stream.close()
        self.poll.stv_results = json.dumps(results)
        self.poll.save()

        # build docs
        self.generate_json_file()
        for lang in settings.LANGUAGES:
            #self.generate_csv_file(lang)
            self.generate_result_docs(lang)
            self.generate_csv_file(lang)
Example #4
0
def extract_ecounting_ballots(zeus_results, nr_candidates):
    ballots = []
    append = ballots.append
    for i, encoded in enumerate(zeus_results):
        selection = gamma_decode(encoded, nr_candidates, nr_candidates)
        answers = to_absolute_answers(selection, nr_candidates)
        votes = [{ 'rank': j + 1, 'candidateTmpId': c }
                 for j, c in enumerate(answers)]
        ballot = { 'ballotSerialNumber': i + 1, 'votes': votes }
        append(ballot)

    return ballots
Example #5
0
 def get_stv_ballots(self):
     candidates = self.do_get_candidates()
     cands_count = len(candidates)
     ballots_data = self.poll.result[0]
     ballots = []
     for ballot in ballots_data:
         if ballot is None:
             return ballot
         ballot = to_absolute_answers(
             gamma_decode(ballot, cands_count, cands_count), cands_count)
         ballot = [candidates[i] for i in ballot]
         ballots.append(ballot)
     return ballots
Example #6
0
    def handle(self, *args, **options):
        poll_uuid = args[0]

        poll = Poll.objects.get(uuid=poll_uuid)
        ballots = poll.result[0]
        candidates = poll.questions_data[0]['answers']
        nr_cand = len(candidates)
        results = '\n'.join('|'.join(
            strforce(candidates[x]).replace('|', '^')
            for x in to_absolute_answers(
                gamma_decode(ballot, nr_cand, nr_cand), nr_cand))
                            for ballot in ballots)
        print(results)
Example #7
0
def extract_ecounting_ballots(zeus_results, nr_candidates):
    ballots = []
    append = ballots.append
    for i, encoded in enumerate(zeus_results):
        selection = gamma_decode(encoded, nr_candidates, nr_candidates)
        answers = to_absolute_answers(selection, nr_candidates)
        votes = [{
            'rank': j + 1,
            'candidateTmpId': c
        } for j, c in enumerate(answers)]
        ballot = {'ballotSerialNumber': i + 1, 'votes': votes}
        append(ballot)

    return ballots
Example #8
0
    def compute_results(self):
        cands_data = self.poll.questions_data[0]['answers']
        cands_count =  len(cands_data)
        constituencies = {}
        count_id = 0

        for item in cands_data:
            cand_and_dep = item.split(':')
            constituencies[str(count_id)] = cand_and_dep[1]
            count_id += 1

        seats = self.poll.eligibles_count
        droop = False
        rnd_gen = None # TODO: should be generated and stored on poll freeze
        if self.poll.has_department_limit:
            quota_limit = self.poll.department_limit
        else:
            quota_limit = 0
        ballots_data = self.poll.result[0]
        ballots = []
        for ballot in ballots_data:
            if not ballot:
                continue
            ballot = to_absolute_answers(gamma_decode(ballot, cands_count,cands_count),
                                         cands_count)
            ballot = [str(i) for i in ballot]
            ballots.append(Ballot(ballot))

        stv_stream = StringIO.StringIO()
        stv_logger = logging.Logger(self.poll.uuid)
        handler = logging.StreamHandler(stv_stream)
        stv_logger.addHandler(handler)
        stv_logger.setLevel(logging.DEBUG)
        results = count_stv(ballots, seats, droop, constituencies, quota_limit,
                            rnd_gen, logger=stv_logger)
        results = list(results[0:2])
        handler.close()
        stv_stream.seek(0)
        results.append(stv_stream.read())
        stv_stream.close()
        self.poll.stv_results = json.dumps(results)
        self.poll.save()

        # build docs
        self.generate_json_file()
        for lang in settings.LANGUAGES: 
            #self.generate_csv_file(lang)
            self.generate_result_docs(lang)
            self.generate_csv_file(lang)
Example #9
0
    def compute_results(self):
        cands_data = self.poll.questions_data[0]['answers']
        cands_count = len(cands_data)
        ballots_data = self.poll.result[0]
        candidates_dict = {candidate: 0 for candidate in cands_data}

        for ballot in ballots_data:
            if not ballot:
                continue
            ballot = to_absolute_answers(
                gamma_decode(ballot, cands_count, cands_count), cands_count)

            for i in ballot:
                candidates_dict[cands_data[i]] += Fraction(
                    len(cands_data), len(ballot))
Example #10
0
def main_verify(sigfile, randomness=None, plaintext=None):
    with open(sigfile) as f:
        signature = f.read()

    vote_info = verify_vote_signature(signature)
    signed_vote, crypto, trustees, candidates, comments = vote_info

    eb = signed_vote['encrypted_ballot']
    public = eb['public']
    modulus, generator, order = crypto
    beta = eb['beta']
    print 'VERIFIED: Authentic Signature'
    if randomness is None:
        return

    nr_candidates = len(candidates)
    encoded = decrypt_with_randomness(modulus, generator, order,
                                      public, beta, randomness)
    if plaintext is not None:
        if plaintext != encoded:
            print 'FAILED: Plaintext Mismatch'

        ct = encrypt(plaintext, modulus, generator, order, randomness)
        _alpha, _beta, _randomness = ct
        alpha = eb['alpha']
        if (alpha, beta) != (_alpha, _beta):
            print 'FAILED: Invalid Encryption'

    max_encoded = gamma_encoding_max(nr_candidates) + 1
    print "plaintext:       %d" % encoded
    print "max plaintext:   %d" % max_encoded
    if encoded > max_encoded:
        print "FAILED: Invalid Vote. Cannot decode."
        return

    selection = gamma_decode(encoded, nr_candidates)
    choices = to_absolute_answers(selection, nr_candidates)
    print ""
    for i, o in enumerate(choices):
        print "%d: [%d] %s" % (i, o, candidates[o])
    print ""
Example #11
0
def main_verify(sigfile, randomness=None, plaintext=None):
    with open(sigfile) as f:
        signature = f.read()

    vote_info = verify_vote_signature(signature)
    signed_vote, crypto, trustees, candidates, comments = vote_info

    eb = signed_vote['encrypted_ballot']
    public = eb['public']
    modulus, generator, order = crypto
    beta = eb['beta']
    print 'VERIFIED: Authentic Signature'
    if randomness is None:
        return

    nr_candidates = len(candidates)
    encoded = decrypt_with_randomness(modulus, generator, order,
                                      public, beta, randomness)
    if plaintext is not None:
        if plaintext != encoded:
            print 'FAILED: Plaintext Mismatch'

        ct = encrypt(plaintext, modulus, generator, order, randomness)
        _alpha, _beta, _randomness = ct
        alpha = eb['alpha']
        if (alpha, beta) != (_alpha, _beta):
            print 'FAILED: Invalid Encryption'

    max_encoded = gamma_encoding_max(nr_candidates) + 1
    print "plaintext:       %d" % encoded
    print "max plaintext:   %d" % max_encoded
    if encoded > max_encoded:
        print "FAILED: Invalid Vote. Cannot decode."
        return

    selection = gamma_decode(encoded, nr_candidates)
    choices = to_absolute_answers(selection, nr_candidates)
    print ""
    for i, o in enumerate(choices):
        print "%d: [%d] %s" % (i, o, candidates[o])
    print ""