Пример #1
0
 def test_vote_load_initial_multi(self):
     """
     TopicPollVoteManyForm
     """
     form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi)
     form.load_initial()
     self.assertDictEqual(form.initial, {"choices": [self.poll_multi_choice, self.poll_multi_choice2]})
Пример #2
0
 def test_vote_load_initial_multi(self):
     """
     TopicPollVoteManyForm
     """
     form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi)
     form.load_initial()
     self.assertDictEqual(form.initial, {'choices': [self.poll_multi_choice, self.poll_multi_choice2], })
Пример #3
0
 def test_vote_load_initial_single(self):
     """
     TopicPollVoteManyForm
     """
     form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
     form.load_initial()
     self.assertDictEqual(form.initial, {"choices": self.poll_choice})
Пример #4
0
 def test_vote_load_initial_single(self):
     """
     TopicPollVoteManyForm
     """
     form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
     form.load_initial()
     self.assertDictEqual(form.initial, {'choices': self.poll_choice, })
Пример #5
0
    def test_create_vote_multi(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form_data = {'choices': [self.poll_multi_choice.pk, self.poll_multi_choice2.pk], }
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi, data=form_data)
        self.assertTrue(form.is_valid())
Пример #6
0
    def test_create_vote_multi(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form_data = {"choices": [self.poll_multi_choice.pk, self.poll_multi_choice2.pk]}
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi, data=form_data)
        self.assertTrue(form.is_valid())
Пример #7
0
    def test_vote_load_initial_empty(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
        form.load_initial()
        self.assertEqual(form.initial, {})
Пример #8
0
    def test_create_vote_multi_invalid(self):
        """
        Limit selected choices to choice_limit
        """
        TopicPollVote.objects.all().delete()

        form_data = {"choices": [self.poll_multi_choice.pk, self.poll_multi_choice2.pk, self.poll_multi_choice3.pk]}
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi, data=form_data)
        self.assertFalse(form.is_valid())
Пример #9
0
    def test_vote_load_initial_empty(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
        form.load_initial()
        self.assertEqual(form.initial, {})
Пример #10
0
    def test_vote_poll_closed(self):
        """
        Cant vote on closed poll
        """
        self.poll.is_closed = True
        self.poll.save()

        form_data = {"choices": self.poll_choice.pk}
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll, data=form_data)
        self.assertFalse(form.is_valid())
Пример #11
0
    def test_vote_poll_closed(self):
        """
        Cant vote on closed poll
        """
        self.poll.is_closed = True
        self.poll.save()

        form_data = {'choices': self.poll_choice.pk, }
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll, data=form_data)
        self.assertFalse(form.is_valid())
Пример #12
0
    def test_create_vote_single(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form_data = {"choices": self.poll_choice.pk}
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll, data=form_data)
        self.assertTrue(form.is_valid())
        form.save_m2m()
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice)), 1)
Пример #13
0
    def test_create_vote_multi_invalid(self):
        """
        Limit selected choices to choice_limit
        """
        TopicPollVote.objects.all().delete()

        form_data = {'choices': [self.poll_multi_choice.pk,
                                 self.poll_multi_choice2.pk,
                                 self.poll_multi_choice3.pk], }
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi, data=form_data)
        self.assertFalse(form.is_valid())
Пример #14
0
    def test_create_vote_single(self):
        """
        TopicPollVoteManyForm
        """
        TopicPollVote.objects.all().delete()

        form_data = {'choices': self.poll_choice.pk, }
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll, data=form_data)
        self.assertTrue(form.is_valid())
        form.save_m2m()
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice)), 1)
Пример #15
0
def render_poll_form(topic, user, next=None):
    try:
        poll = topic.poll
    except models.ObjectDoesNotExist:
        return {}

    form = TopicPollVoteManyForm(user=user, poll=poll)

    if user.is_authenticated():
        form.load_initial()

    return {'form': form, 'poll': poll, 'next': next}
Пример #16
0
def render_poll_form(topic, user, next=None):
    try:
        poll = topic.poll
    except models.ObjectDoesNotExist:
        return {}

    form = TopicPollVoteManyForm(user=user, poll=poll)

    if user.is_authenticated():
        form.load_initial()

    return {"form": form, "poll": poll, "next": next}
Пример #17
0
    def test_update_vote_single(self):
        """
        TopicPollVoteManyForm
        """
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice2)), 0)
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice)), 2)

        form_data = {'choices': self.poll_choice2.pk, }
        form = TopicPollVoteManyForm(user=self.user, poll=self.poll, data=form_data)
        self.assertTrue(form.is_valid())
        form.save_m2m()
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice2)), 1)
        self.assertEqual(len(TopicPollVote.objects.filter(choice=self.poll_choice)), 1)
Пример #18
0
def poll_vote(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        form.save_m2m()
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #19
0
def poll_vote(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user,
                                 poll=poll,
                                 data=request.POST)

    if form.is_valid():
        form.save_m2m()
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #20
0
def poll_vote(request, pk):
    # TODO: check if user has access to this topic/poll
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        topic_poll_pre_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        form.save_m2m()
        topic_poll_post_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #21
0
def poll_vote(request, pk):
    # TODO: check if user has access to this topic/poll
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        topic_poll_pre_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        form.save_m2m()
        topic_poll_post_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        return redirect(request.POST.get('next', poll.get_absolute_url()))

    messages.error(request, utils.render_form_errors(form))
    return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #22
0
    def test_vote_load_initial_choice_limit(self):
        """
        Load initial for a single choice poll that was previously a multi choice poll
        """
        # multi to single
        self.poll_multi.choice_limit = 1

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi)
        form.load_initial()
        self.assertDictEqual(form.initial, {'choices': self.poll_multi_choice, })

        # single to multi
        self.poll.choice_limit = 2

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
        form.load_initial()
        self.assertDictEqual(form.initial, {'choices': [self.poll_choice, ], })
Пример #23
0
    def test_vote_load_initial_choice_limit(self):
        """
        Load initial for a single choice poll that was previously a multi choice poll
        """
        # multi to single
        self.poll_multi.choice_limit = 1

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll_multi)
        form.load_initial()
        self.assertDictEqual(form.initial, {"choices": self.poll_multi_choice})

        # single to multi
        self.poll.choice_limit = 2

        form = TopicPollVoteManyForm(user=self.user, poll=self.poll)
        form.load_initial()
        self.assertDictEqual(form.initial, {"choices": [self.poll_choice]})