Esempio n. 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)
Esempio n. 2
0
def edit_voting(request, slug=None):
    """Create/Edit voting view."""
    poll, created = get_or_create_instance(Poll, slug=slug)

    can_delete_voting = False
    extra_range_polls = 0
    extra_radio_polls = 0
    current_voting_edit = False
    range_poll_formset = None
    radio_poll_formset = None

    if created:
        poll.created_by = request.user
        extra_range_polls = 1
        extra_radio_polls = 1
    else:
        if not poll.range_polls.exists():
            extra_range_polls = 1
        if not poll.radio_polls.exists():
            extra_radio_polls = 1
        can_delete_voting = True

        if poll.is_current_voting:
            current_voting_edit = True
        if not user_has_poll_permissions(request.user, poll):
            messages.error(request, 'Permission denied.')
            return redirect('voting_list_votings')

    nominee_list = (User.objects
                        .filter(groups__name='Rep',
                                userprofile__registration_complete=True)
                        .order_by('first_name'))
    if current_voting_edit:
        poll_form = forms.PollEditForm(request.POST or None, instance=poll)
    else:
        RangePollFormset = (inlineformset_factory(Poll, RangePoll,
                            formset=forms.BaseRangePollInlineFormSet,
                            extra=extra_range_polls, can_delete=True))
        RadioPollFormset = (inlineformset_factory(Poll, RadioPoll,
                            formset=forms.BaseRadioPollInlineFormSet,
                            extra=extra_radio_polls, can_delete=True))

        range_poll_formset = RangePollFormset(request.POST or None,
                                              instance=poll,
                                              user_list=nominee_list)
        radio_poll_formset = RadioPollFormset(request.POST or None,
                                              instance=poll)
        poll_form = forms.PollAddForm(request.POST or None, instance=poll,
                                      radio_poll_formset=radio_poll_formset,
                                      range_poll_formset=range_poll_formset)

    if poll_form.is_valid():
        poll_form.save()

        if created:
            messages.success(request, 'Voting successfully created.')
            statsd.incr('voting.create_voting')
        else:
            messages.success(request, 'Voting successfully edited.')
            statsd.incr('voting.edit_voting')

        if not current_voting_edit:
            statsd.incr('voting.create_range_poll',
                        poll_form.radio_poll_formset.total_form_count())
            statsd.incr('voting.create_radio_poll',
                        poll_form.range_poll_formset.total_form_count())

        return redirect('voting_edit_voting', slug=poll.slug)

    return render(request, 'edit_voting.html',
                  {'created': created,
                   'poll': poll,
                   'poll_form': poll_form,
                   'range_poll_formset': range_poll_formset,
                   'radio_poll_formset': radio_poll_formset,
                   'can_delete_voting': can_delete_voting,
                   'current_voting_edit': current_voting_edit,
                   'nominee_list': nominee_list})
Esempio n. 3
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)
Esempio n. 4
0
def edit_voting(request, slug=None):
    """Create/Edit voting view."""
    poll, created = get_or_create_instance(Poll, slug=slug)

    can_delete_voting = False
    extra_range_polls = 0
    extra_radio_polls = 0
    current_voting_edit = False
    range_poll_formset = None
    radio_poll_formset = None

    if created:
        poll.created_by = request.user
        extra_range_polls = 1
        extra_radio_polls = 1
    else:
        if not poll.range_polls.exists():
            extra_range_polls = 1
        if not poll.radio_polls.exists():
            extra_radio_polls = 1
        can_delete_voting = True

        if poll.is_current_voting:
            current_voting_edit = True
        if not user_has_poll_permissions(request.user, poll):
            messages.error(request, 'Permission denied.')
            return redirect('voting_list_votings')

    nominee_list = User.objects.filter(groups__name='Rep',
                                       userprofile__registration_complete=True)
    if current_voting_edit:
        poll_form = forms.PollEditForm(request.POST or None, instance=poll)
    else:
        RangePollFormset = (inlineformset_factory(
            Poll,
            RangePoll,
            formset=forms.BaseRangePollInlineFormSet,
            extra=extra_range_polls,
            can_delete=True))
        RadioPollFormset = (inlineformset_factory(
            Poll,
            RadioPoll,
            formset=forms.BaseRadioPollInlineFormSet,
            extra=extra_radio_polls,
            can_delete=True))

        range_poll_formset = RangePollFormset(request.POST or None,
                                              instance=poll,
                                              user_list=nominee_list)
        radio_poll_formset = RadioPollFormset(request.POST or None,
                                              instance=poll)
        poll_form = forms.PollAddForm(request.POST or None,
                                      instance=poll,
                                      radio_poll_formset=radio_poll_formset,
                                      range_poll_formset=range_poll_formset)

    if poll_form.is_valid():
        poll_form.save()

        if created:
            messages.success(request, 'Voting successfully created.')
            statsd.incr('voting.create_voting')
        else:
            messages.success(request, 'Voting successfully edited.')
            statsd.incr('voting.edit_voting')

        if not current_voting_edit:
            statsd.incr('voting.create_range_poll',
                        poll_form.radio_poll_formset.total_form_count())
            statsd.incr('voting.create_radio_poll',
                        poll_form.range_poll_formset.total_form_count())

        return redirect('voting_edit_voting', slug=poll.slug)

    return render(
        request, 'edit_voting.html', {
            'created': created,
            'poll': poll,
            'poll_form': poll_form,
            'range_poll_formset': range_poll_formset,
            'radio_poll_formset': radio_poll_formset,
            'can_delete_voting': can_delete_voting,
            'current_voting_edit': current_voting_edit,
            'nominee_list': nominee_list
        })
Esempio n. 5
0
 def test_invalid_user(self):
     user = UserFactory.create(groups=['Rep'])
     group = Group.objects.get(name='Mentor')
     poll = PollFactory.create(valid_groups=group)
     ok_(not user_has_poll_permissions(user, poll))
Esempio n. 6
0
 def test_admin(self):
     user = UserFactory.create(groups=['Admin'])
     group = Group.objects.get(name='Rep')
     poll = PollFactory.create(valid_groups=group)
     ok_(user_has_poll_permissions(user, poll))
Esempio n. 7
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 = {}

    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 UTC.' %
                                       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 UTC.' % 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)

    poll_comment = PollComment(poll=poll, user=request.user)
    poll_comment_form = forms.PollCommentForm(data=request.POST or None,
                                              instance=poll_comment)

    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()
            if poll.automated_poll and poll_comment_form.is_valid():
                poll_comment_form.save()
                statsd.incr('voting.create_automated_poll_comment')
            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
    data['poll_comment_form'] = poll_comment_form

    return render(request, 'vote_voting.html', data)
Esempio n. 8
0
 def test_admin(self):
     user = UserFactory.create(groups=['Admin'])
     group = Group.objects.get(name='Rep')
     poll = PollFactory.create(valid_groups=group)
     ok_(user_has_poll_permissions(user, poll))
Esempio n. 9
0
 def test_invalid_user(self):
     user = UserFactory.create(groups=['Rep'])
     group = Group.objects.get(name='Mentor')
     poll = PollFactory.create(valid_groups=group)
     ok_(not user_has_poll_permissions(user, poll))
Esempio n. 10
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 = {}

    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 UTC.' %
                                       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 UTC.'
                                   % 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)

    poll_comment = PollComment(poll=poll, user=request.user)
    poll_comment_form = forms.PollCommentForm(data=request.POST or None,
                                              instance=poll_comment)

    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()
            if poll.automated_poll and poll_comment_form.is_valid():
                poll_comment_form.save()
                statsd.incr('voting.create_automated_poll_comment')
            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
    data['poll_comment_form'] = poll_comment_form

    return render(request, 'vote_voting.html', data)
Esempio n. 11
0
 def test_valid_user(self):
     user = UserFactory.create(groups=["Rep"])
     group = Group.objects.get(name="Rep")
     poll = PollFactory.create(valid_groups=group)
     ok_(user_has_poll_permissions(user, poll))