Пример #1
0
    def post(self, request, *args, **kwargs):
        poll = self.get_poll(**kwargs)
        form = VoteForm(poll=poll, data=self.request.POST)
        if form.is_valid():
            answer = form.cleaned_data.get('answer')
            #answer = models.Answer.objects.get(pk=answer_pk)

            user = request.user

            if user.is_authenticated():
                voted = models.Vote.objects.filter(user=user,
                                                   poll=poll).exists()
            else:
                session_key = request.session.session_key
                voted = models.Vote.objects.filter(session_key=session_key,
                                                   poll=poll).exists()

            if voted:
                return HttpResponse('Вы уже голосовали. Спасибо!')
            else:
                vote = models.Vote()
                vote.poll = poll
                vote.answer = answer
                if user.is_authenticated():
                    vote.user = user
                else:
                    vote.session_key = session_key
                vote.save()
                return HttpResponse('Спасибо!')
        else:
            return self.render_to_response(self.get_context_data(form=form))
Пример #2
0
    def post(self, request, *args, **kwargs):
        poll = self.get_poll(**kwargs)
        form = VoteForm(poll=poll, data=self.request.POST)
        if form.is_valid():
            answer = form.cleaned_data.get('answer')
            #answer = models.Answer.objects.get(pk=answer_pk)

            user = request.user

            if user.is_authenticated():
                voted = models.Vote.objects.filter(user=user, poll=poll).exists()
            else:
                session_key = request.session.session_key
                voted = models.Vote.objects.filter(session_key=session_key, poll=poll).exists()

            if voted:
                return HttpResponse('Вы уже голосовали. Спасибо!')
            else:
                vote = models.Vote()
                vote.poll = poll
                vote.answer = answer
                if user.is_authenticated():
                    vote.user = user
                else:
                    vote.session_key = session_key
                vote.save()
                return HttpResponse('Спасибо!')
        else:
            return self.render_to_response(self.get_context_data(form=form))
Пример #3
0
def detail(request, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    form = VoteForm(request.POST or None)
    form.fields['choice'].queryset = Choice.objects.filter(poll=poll)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(poll.get_absolute_url() + "results/")
    return render(request, 'polls/detail.html', {'poll': poll, 'form': form})
Пример #4
0
 def test_choice_form_required(self):
     """
     Tests that my choice form requires a choice
     """
     form = VoteForm({})
     self.assertFalse(form.is_valid())
     self.assertIn('choice', form.errors)
     self.assertIn('required', "\n".join(form.errors.get('choice')))
Пример #5
0
 def test_choice_form_required(self):
     """
     Tests that my choice form requires a choice
     """
     form = VoteForm({})
     self.assertFalse(form.is_valid())
     self.assertIn('choice', form.errors)
     self.assertIn('required', "\n".join(form.errors.get('choice')))
Пример #6
0
def detail(request, poll_id):
    poll = get_object_or_404(Poll, pk=poll_id)
    form = VoteForm(request.POST or None)
    form.fields["choice"].queryset = Choice.objects.filter(poll=poll)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(poll.get_absolute_url() + "results/")
    return render(request, "polls/detail.html", {"poll": poll, "form": form})
Пример #7
0
    def test_create_vote(self):
        """
        Verifies that the user instance, and the task_id were assigned to
        correctly to the Vote model instance.
        """
        user = create_user()
        task = Task.objects.create(created_by=user, name='Task name 1')  # pylint: disable=E1101
        # Picking up the first value from Planning cards values tuple.
        value = PLANNING_CARDS[0][0]

        vote_form = VoteForm(user=user, task_id=task.id, data={'value': value})
        self.assertEqual(vote_form.user, user)
        self.assertEqual(vote_form.task_id, task.id)
        self.assertTrue(vote_form.is_valid())

        vote = vote_form.save()
        self.assertEqual(vote.user, user)
        self.assertEqual(vote.task, task)
        self.assertEqual(vote.value, value)
Пример #8
0
def show_poll(context, poll_pk):
    result_context = {}
    user = context['request'].user
    try:
        poll = Poll.objects.get(pk=poll_pk)
    except:
        return

    if user.is_authenticated():
        voted = Vote.objects.filter(user=user, poll=poll).exists()
    else:
        session_key = context['request'].session.session_key
        voted = Vote.objects.filter(session_key=session_key,
                                    poll=poll).exists()
    if not voted:
        form = VoteForm(poll=poll, initial={'poll': poll})
        result_context['poll'] = poll
        result_context['form'] = form
        result_context['submit_url'] = reverse('polls:poll-view',
                                               kwargs={'pk': poll.pk})
        result_context['render_scripts'] = True
    result_context['voted'] = voted
    return result_context