Example #1
0
def cast_vote(voter_url, choices=None):
    conn, headers, poll_info = get_poll_info(voter_url)
    csrf_token = poll_info['token']
    headers['Cookie'] += "; csrftoken=%s" % csrf_token
    headers[
        'Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
    headers['Referer'] = voter_url

    poll_data = poll_info['poll_data']
    pk = poll_data['public_key']
    p = int(pk['p'])
    g = int(pk['g'])
    q = int(pk['q'])
    y = int(pk['y'])
    candidates = poll_data['questions'][0]['answers']
    cast_path = poll_data['cast_url']

    parties = None
    try:
        parties, nr_groups = parties_from_candidates(candidates)
    except FormatError as e:
        pass

    if parties:
        if randint(0, 19) == 0:
            choices = []
        else:
            party_choice = choice(list(parties.keys()))
            party = parties[party_choice]
            party_candidates = [
                k for k in list(party.keys()) if isinstance(k, int)
            ]
            min_choices = party['opt_min_choices']
            max_choices = party['opt_max_choices']
            shuffle(party_candidates)
            nr_choices = randint(min_choices, max_choices)
            choices = party_candidates[:nr_choices]
            choices.sort()
        vote, encoded, rand = generate_vote(p, g, q, y, choices)
        #for c in choices:
        #    print "Voting for", c, party[c]
        #ballot = gamma_decode_to_party_ballot(encoded, candidates,
        #                                      parties, nr_groups)
        #print "valid", ballot['valid'], ballot['invalid_reason']
        #print " "
    else:
        choices = choices if choices is not None else len(candidates)
        vote, encoded, rand = generate_vote(p, g, q, y, choices)

    do_cast_vote(conn, cast_path, csrf_token, headers, vote)
    return encoded, rand
Example #2
0
def cast_vote(voter_url, choices=None):
    conn, headers, poll_info = get_poll_info(voter_url)
    csrf_token = poll_info['token']
    headers['Cookie'] += "; csrftoken=%s" % csrf_token
    voter_path = conn.path
    poll_data = poll_info['poll_data']
    pk = poll_data['public_key']
    p = int(pk['p'])
    g = int(pk['g'])
    q = int(pk['q'])
    y = int(pk['y'])
    candidates = poll_data['questions'][0]['answers']
    cast_path = poll_data['cast_url']

    parties = None
    try:
        parties, nr_groups = parties_from_candidates(candidates)
    except FormatError as e:
        pass

    if parties:
        if randint(0,19) == 0:
            choices = []
        else:
            party_choice = choice(parties.keys())
            party = parties[party_choice]
            party_candidates = [k for k in party.keys() if isinstance(k, int)]
            min_choices = party['opt_min_choices']
            max_choices = party['opt_max_choices']
            shuffle(party_candidates)
            nr_choices = randint(min_choices, max_choices)
            choices = party_candidates[:nr_choices]
            choices.sort()
        vote, encoded, rand = generate_vote(p, g, q, y, choices)
        #for c in choices:
        #    print "Voting for", c, party[c]
        #ballot = gamma_decode_to_party_ballot(encoded, candidates,
        #                                      parties, nr_groups)
        #print "valid", ballot['valid'], ballot['invalid_reason']
        #print " "
    else:
        choices = choices if choices is not None else len(candidates)
        vote, encoded, rand = generate_vote(p, g, q, y, choices)

    do_cast_vote(conn, cast_path, csrf_token, headers, vote)
    return encoded, rand
Example #3
0
def _make_vote(poll_data, choices=None):
    pk = poll_data['public_key']
    p = int(pk['p'])
    g = int(pk['g'])
    q = int(pk['q'])
    y = int(pk['y'])
    candidates = poll_data['questions'][0]['answers']

    parties = None
    try:
        parties, nr_groups = parties_from_candidates(candidates)
    except FormatError as e:
        pass

    if parties:
        if randint(0, 19) == 0:
            choices = []
        else:
            party_choice = choice(parties.keys())
            party = parties[party_choice]
            party_candidates = [k for k in party.keys() if isinstance(k, int)]
            min_choices = party['opt_min_choices']
            max_choices = party['opt_max_choices']
            shuffle(party_candidates)
            nr_choices = randint(min_choices, max_choices)
            choices = party_candidates[:nr_choices]
            choices.sort()
        vote, encoded, rand = generate_vote(p, g, q, y, choices,
                                            len(candidates))
        for c in choices:
            print "Voting for", c, party[c]
        ballot = gamma_decode_to_party_ballot(encoded, candidates, parties,
                                              nr_groups)
        if not ballot['valid']:
            print "valid", ballot['valid'], ballot['invalid_reason']
    else:
        choices = choices if choices is not None else len(candidates)
        vote, encoded, rand = generate_vote(p, g, q, y, choices)

    return vote, encoded, rand