Exemplo n.º 1
0
def polling_station_vote(ballot_id):
    ballot = db.session.query(Ballot).get(ballot_id)
    if ballot is None:
        abort(404)
    permit_voting(ballot)

    input_options = pickle.loads(request.form["input_options_data"])
    try:
        validate_options(input_options, ballot)
    except ValidationError as e:
        flash(unicode(e), "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))
    except ValueError as e:
        flash(u"Některý z hlasů má neplatnou hodnotu", "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))

    try:
        vote_timestamp = session.get("vote_timestamp_{}".format(ballot_id),
                                     False)
        if not vote_timestamp:
            raise ValidationError()
        hash_base = compute_hash_base(ballot_id, g.user.id, input_options,
                                      vote_timestamp)
        hash_salt = request.form["hash_salt"]
        h = hashlib.sha1()
        h.update(hash_base.encode("utf-8"))
        h.update(hash_salt.encode("utf-8"))
        hash_digest = h.hexdigest()
    except Exception as e:
        flash(u"Chyba při výpočtu kontrolního řetězce", "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))

    for (option_id, value) in input_options.items():
        vote = Vote()
        vote.ballot_option_id = option_id
        vote.value = value
        vote.hash_digest = hash_digest
        db.session.add(vote)

    voter = Voter()
    voter.ballot_id = ballot_id
    voter.name = g.user.name
    voter.email = g.user.email
    voter.person_id = g.user.id
    voter.voted_at = datetime.datetime.now()
    voter.remote_addr = request.remote_addr
    voter.user_agent = request.user_agent.string
    db.session.add(voter)

    send_mail = "send_confirmation_email" in request.form
    email_body = send_vote_confirmation(ballot, voter, hash_digest, hash_salt,
                                        vote_timestamp, send_mail)

    db.session.commit()

    return render_template('polling_station_vote.html',
                           ballot=ballot,
                           hash_digest=hash_digest,
                           email_body=email_body,
                           really_send=send_mail)
Exemplo n.º 2
0
def polling_station_vote(ballot_id):
    ballot = db.session.query(Ballot).get(ballot_id)
    if ballot is None:
        abort(404)
    permit_voting(ballot)

    input_options = pickle.loads(request.form["input_options_data"])
    try:
        validate_options(input_options, ballot)
    except ValidationError as e:
        flash(unicode(e), "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))
    except ValueError as e:
        flash(u"Některý z hlasů má neplatnou hodnotu", "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))

    try:
        vote_timestamp = session.get(
            "vote_timestamp_{}".format(ballot_id), False)
        if not vote_timestamp:
            raise ValidationError()
        hash_base = compute_hash_base(ballot_id, g.user.id,
                                      input_options, vote_timestamp)
        hash_salt = request.form["hash_salt"]
        h = hashlib.sha1()
        h.update(hash_base.encode("utf-8"))
        h.update(hash_salt.encode("utf-8"))
        hash_digest = h.hexdigest()
    except Exception as e:
        flash(u"Chyba při výpočtu kontrolního řetězce", "danger")
        return redirect(url_for('polling_station_item', ballot_id=ballot_id))

    for (option_id, value) in input_options.items():
        vote = Vote()
        vote.ballot_option_id = option_id
        vote.value = value
        vote.hash_digest = hash_digest
        db.session.add(vote)

    voter = Voter()
    voter.ballot_id = ballot_id
    voter.name = g.user.name
    voter.email = g.user.email
    voter.person_id = g.user.id
    voter.voted_at = datetime.datetime.now()
    voter.remote_addr = request.remote_addr
    voter.user_agent = request.user_agent.string
    db.session.add(voter)

    send_mail = "send_confirmation_email" in request.form
    email_body = send_vote_confirmation(ballot, voter, hash_digest,
                                        hash_salt, vote_timestamp, send_mail)

    db.session.commit()

    return render_template(
        'polling_station_vote.html', ballot=ballot,
        hash_digest=hash_digest, email_body=email_body, really_send=send_mail)
Exemplo n.º 3
0
def register(request, unique_id=None):
    request.session['django_language'] = 'el'
    establishment = None
    voter = None
    if unique_id:
        establishment = get_object_or_404(Establishment,
                                          unique_id=unique_id)
        if establishment.voter_id:
            voter = establishment.voter
        else:
            voter = Voter()
    if request.method == 'POST':
        if unique_id:
            form = RegistrationForm(request.POST, instance=establishment)
        else:
            form = RegistrationForm(request.POST)
        if form.is_valid():
            voter.first_name = form.cleaned_data['voter_first_name']
            voter.surname = form.cleaned_data['voter_surname']
            voter.email = form.cleaned_data['voter_email']
            voter.mobile_phone = form.cleaned_data['voter_mobile_phone']
            voter.save()

            # is_valid changes the instance. Invalidate, in order to avoid
            # storing those changes.
            establishment = Establishment.objects.get(unique_id=unique_id)
            establishment.voter = voter
            establishment.save()
            return render(request, 'roll/thanks.html', {
                'voter_first_name': voter.first_name,
                'voter_surname': voter.surname,
                'voter_email': voter.email,
                'voter_mobile_phone': voter.mobile_phone,
                'registration_url': request.path
            })
    else:
        if unique_id:
            form = RegistrationForm(
                instance=establishment,
                initial={
                    'voter_first_name': voter.first_name,
                    'voter_surname': voter.surname,
                    'voter_email': voter.email,
                    'voter_mobile_phone': voter.mobile_phone,
                })
        else:
            form = RegistrationForm()
    return render(request, 'roll/registration.html', {
        'form': form,
        'voter': voter,
        'form_action': request.path,
    })
Exemplo n.º 4
0
def register(request, unique_id=None):
    establishment = None
    voter = None
    if unique_id:
        establishment = Establishment.objects.get(unique_id=unique_id)
        if establishment.voter_id:
            voter = establishment.voter
        else:
            voter = Voter()
    if request.method == 'POST':
        if unique_id:
            form = RegistrationForm(request.POST, instance=establishment)
        else:
            form = RegistrationForm(request.POST)
        if form.is_valid():
            voter.name = form.cleaned_data['voter_name']
            voter.email = form.cleaned_data['voter_email']
            voter.mobile_phone = form.cleaned_data['voter_mobile_phone']
            voter.save()

            # is_valid changes the instance. Invalidate, in order to avoid
            # storing those changes.
            establishment = Establishment.objects.get(unique_id=unique_id)
            establishment.voter = voter
            establishment.save()
            return render(
                request, 'roll/thanks.html', {
                    'voter_name': voter.name,
                    'voter_email': voter.email,
                    'voter_mobile_phone': voter.mobile_phone,
                    'registration_url': request.path
                })
    else:
        if unique_id:
            form = RegistrationForm(instance=establishment,
                                    initial={
                                        'voter_name': voter.name,
                                        'voter_email': voter.email,
                                        'voter_mobile_phone':
                                        voter.mobile_phone,
                                    })
        else:
            form = RegistrationForm()
    return render(request, 'roll/registration.html', {
        'form': form,
        'voter': voter,
        'form_action': request.path,
    })