Пример #1
0
def view_voting(request, slug):
    """View voting and cast a vote view."""
    user = request.user
    poll = get_object_or_404(Poll, slug=slug)
    # If the user does not belong to a valid poll group
    if not user_has_poll_permissions(user, poll):
        messages.error(request, ('You do not have the permissions to '
                                 'vote on this voting.'))
        return redirect('voting_list_votings')

    range_poll_choice_forms = {}
    radio_poll_choice_forms = {}
    user_voted = False
    comment_form = forms.PollCommentForm()

    data = {'poll': poll, 'comment_form': comment_form}

    # if the voting period has ended, display the results
    if poll.is_past_voting:
        return render(request, 'view_voting.html', data)

    # avoid multiple votes from the same user
    if Vote.objects.filter(poll=poll, user=user).exists():
        if not poll.comments_allowed:
            messages.warning(
                request,
                ('You have already cast your vote for '
                 'this voting. Come back to see the '
                 'results on %s UTC.' % poll.end.strftime('%Y %B %d, %H:%M')))
            return redirect('voting_list_votings')
        # If the poll allows comments, display them along with an info msg
        user_voted = True
        data['user_voted'] = user_voted
        if not request.method == 'POST':
            msg = "You have already cast your vote for this voting."
            messages.info(request, msg)

    # pack the forms for rendering
    for item in poll.range_polls.all():
        range_poll_choice_forms[item] = forms.RangePollChoiceVoteForm(
            choices=item.choices.all())

    for item in poll.radio_polls.all():
        radio_poll_choice_forms[item] = forms.RadioPollChoiceVoteForm(
            radio_poll=item)

    if request.method == 'POST':
        # Process comment form
        if 'comment' in request.POST and poll.comments_allowed:
            comment_form = forms.PollCommentForm(request.POST)
            data['comment_form'] = comment_form
            if comment_form.is_valid():
                obj = comment_form.save(commit=False)
                obj.user = request.user
                obj.poll = poll
                obj.save()
                messages.success(request, 'Comment saved successfully.')
                statsd.incr('voting.create_comment')
                data['comment_form'] = forms.PollCommentForm()
        else:
            # pack the forms for rendering
            for item in poll.range_polls.all():
                range_poll_choice_forms[item] = forms.RangePollChoiceVoteForm(
                    data=request.POST, choices=item.choices.all())

            for item in poll.radio_polls.all():
                radio_poll_choice_forms[item] = forms.RadioPollChoiceVoteForm(
                    data=request.POST, radio_poll=item, user=request.user)
            # Process poll form
            forms_valid = True
            # validate all forms
            for item in (range_poll_choice_forms.values() +
                         radio_poll_choice_forms.values()):
                if not item.is_valid():
                    forms_valid = False
                    break

            if forms_valid and poll.is_current_voting and not user_voted:
                for range_poll_form in range_poll_choice_forms.values():
                    range_poll_form.save()
                for radio_poll_form in radio_poll_choice_forms.values():
                    radio_poll_form.save()

                Vote.objects.create(user=user, poll=poll)
                messages.success(request, ('Your vote has been '
                                           'successfully registered.'))
                statsd.incr('voting.vote_voting')
                return redirect('voting_list_votings')

    data['range_poll_choice_forms'] = range_poll_choice_forms
    data['radio_poll_choice_forms'] = radio_poll_choice_forms

    return render(request, 'vote_voting.html', data)
Пример #2
0
def view_voting(request, slug):
    """View voting and cast a vote view."""
    user = request.user
    now = datetime2pdt()
    poll = get_object_or_404(Poll, slug=slug)
    # If the user does not belong to a valid poll group
    if not (user.groups.filter(Q(id=poll.valid_groups.id) |
                               Q(name='Admin')).exists()):
        messages.error(request, ('You do not have the permissions to '
                                 'vote on this voting.'))
        return redirect('voting_list_votings')

    range_poll_choice_forms = {}
    radio_poll_choice_forms = {}

    data = {'poll': poll}

    # if the voting period has ended, display the results
    if now > poll.end:
        return render(request, 'view_voting.html', data)
    if now < poll.start:
        # Admin can edit future votings
        if user.groups.filter(name='Admin').exists():
            return redirect('voting_edit_voting', slug=poll.slug)
        else:
            messages.warning(request, ('This vote has not yet begun. '
                                       'You can cast your vote on %s PDT.' %
                                       poll.start.strftime('%Y %B %d, %H:%M')))
            return redirect('voting_list_votings')

    # avoid multiple votes from the same user
    if Vote.objects.filter(poll=poll, user=user).exists():
        messages.warning(request, ('You have already cast your vote for this '
                                   'voting. Come back to see the results on '
                                   '%s PDT.'
                                   % poll.end.strftime('%Y %B %d, %H:%M')))
        return redirect('voting_list_votings')

    # pack the forms for rendering
    for item in poll.range_polls.all():
        range_poll_choice_forms[item] = forms.RangePollChoiceVoteForm(
            data=request.POST or None, choices=item.choices.all())

    for item in poll.radio_polls.all():
        radio_poll_choice_forms[item] = forms.RadioPollChoiceVoteForm(
            data=request.POST or None, radio_poll=item)

    if request.method == 'POST':
        forms_valid = True
        # validate all forms
        for item in (range_poll_choice_forms.values()
                     + radio_poll_choice_forms.values()):
            if not item.is_valid():
                forms_valid = False
                break

        if forms_valid:
            for range_poll_form in range_poll_choice_forms.values():
                range_poll_form.save()
            for radio_poll_form in radio_poll_choice_forms.values():
                radio_poll_form.save()
            Vote.objects.create(user=user, poll=poll)
            messages.success(request, ('Your vote has been '
                                       'successfully registered.'))
            return redirect('voting_list_votings')

    data['range_poll_choice_forms'] = range_poll_choice_forms
    data['radio_poll_choice_forms'] = radio_poll_choice_forms

    return render(request, 'vote_voting.html', data)