Example #1
0
 def get(self, request, *args, **kwargs):
     form = VoteForm(data=request.GET, initial=self.get_initial())
     if form.is_valid():
         vote = self.process_form(form)
         return HttpResponseRedirect(self.get_success_url(vote))
     else:
         return HttpResponseRedirect(self.get_failure_url())
Example #2
0
File: views.py Project: omus/vote
def ballot():
    user = g.user

    if not api.is_open:
        flash('Voting is currently closed.')
        return redirect(url_for('results'))

    form = VoteForm()

    if form.is_submitted():
        if form.validate_on_submit():
            ballot = form.ballot.data.split('|')
            if api.is_open:
                api.vote(user, *ballot)
            else:
                flash('Voting closed before your ballot was submitted.')
                return redirect(url_for('results'))

        else:
            flash('Unable to validate form: {}'.format(form.errors))

    if user.is_admin():
        admin_links = [
            ('Close', url_for('close')),
            ('Clear', url_for('clear')),
        ]
    else:
        admin_links = []

    return render_template(
        'vote.html', title='Vote', user=user, options=user.no_preferences,
        votes=user.preferences, form=form, admin_links=admin_links,
        voters=api.list_users(voted=True), info=app.config.get('INFO_TEXT')
    )
Example #3
0
    def _post_valid_form(self, form: VoteForm):
        form.save(self.user)
        request = self.request
        election_id = self.election_id
        user = self.user

        # Delete session ballot data
        if BALLOT_FORM_NAME in request.session:
            del request.session[BALLOT_FORM_NAME]

        messages.success(request, f'Vote submitted for "{user.username}"!')
        response = redirect('view-results', election_id)

        # Set a cookie for anonymous voters
        if not self.user_handler.is_authenticated:
            messages.success(request, "Setting a cookie")

            election_ids = request.COOKIES.get(ELECTION_IDS_COOKIE, '')
            election_ids += f',{election_id}'
            response.set_cookie(ELECTION_IDS_COOKIE, election_ids)
        return response
def vote(request):
    """Create or update a game rating.

    The game must be in the user's ballot.

    """

    ballot = get_object_or_404(Ballot, creator=request.user)

    form = VoteForm(request.POST)

    if form.is_valid():
        game = get_object_or_404(Game, pk=form.cleaned_data['game'].id)
        if not game in ballot.get_games():
            return HttpResponseBadRequest(mimetype='application/json')
        vote, created = Vote.objects.get_or_create(creator=request.user,
                                                   game=game)
        vote.score = form.cleaned_data['score']
        vote.save()
        return HttpResponse(mimetype='application/json')
    return HttpResponseBadRequest(mimetype='application/json')
Example #5
0
def vote_action(request, pk):

    if request.method == 'POST':
        secret = request.POST.get('code', '-').strip()
        email = request.POST.get('email', '')

        delegate = models.Delegate.objects.filter(email__iexact=email)

        if not delegate.exists():
            votation = models.Votation.objects.get(pk=pk)
            form = VoteForm(request.POST, votation=votation)
            return render(request, 'vote/vote_form.html', {
                'form': form,
                'votation': votation,
            })

        delegate = delegate[0]

        if not check_password(secret, delegate.secret):
            votation = models.Votation.objects.get(pk=pk)
            form = VoteForm(request.POST, votation=votation)
            return render(
                request, 'vote/vote_form.html', {
                    'form': form,
                    'votation': votation,
                    'error_code': _("Wrong code."),
                })

    with transaction.atomic():

        votation = models.Votation.objects.select_for_update().get(pk=pk)
        form = VoteForm(votation=votation)

        if request.method == 'POST':
            form = VoteForm(request.POST, votation=votation)

            if form.is_valid() and request.POST.get('confirm') == '1':
                options = form.cleaned_data['options']
                voteset = models.VoteSet.objects.create(
                    votation=votation, checked=not form.cleaned_data['other'])

                if votation.counted_votation:
                    total = len(options)
                    for i, option in options:
                        models.Vote.objects.create(
                            votation=votation,
                            vote=option,
                            count=total - i + 1,
                            secret=form.cleaned_data['code'],
                            section=form.cleaned_data['delegate'].section,
                            voteset=voteset,
                        )
                else:
                    for option in options:
                        models.Vote.objects.create(
                            votation=votation,
                            vote=option,
                            secret=form.cleaned_data['code'],
                            section=form.cleaned_data['delegate'].section,
                            voteset=voteset,
                        )

                if len(options) == 0:
                    models.Vote.objects.create(
                        votation=votation,
                        vote='-',
                        secret=form.cleaned_data['code'],
                        section=form.cleaned_data['delegate'].section,
                        voteset=voteset,
                    )
                votation.voted.add(form.cleaned_data['delegate'])
                votation.save()
                return render(request, 'vote/success.html', {
                    'options': options,
                    'votation': votation,
                })

            if form.is_valid():
                if votation.counted_votation:
                    form.fields['ordered_input'].widget.attrs[
                        'readonly'] = "readonly"
                else:
                    form.fields['options'].widget.attrs[
                        'readonly'] = "readonly"
                return render(
                    request, 'vote/confirm.html', {
                        'data': form.cleaned_data,
                        'form': form,
                        'votation': votation,
                    })

    return render(request, 'vote/vote_form.html', {
        'form': form,
        'votation': votation,
    })