Пример #1
0
    def test_poll_vote_signal(self):
        """
        POST, poll_vote
        """
        def topic_poll_pre_vote_handler(sender, poll, user, **kwargs):
            self._poll = poll
            self._user = user._wrapped
        topic_poll_pre_vote.connect(topic_poll_pre_vote_handler)

        def topic_poll_post_vote_handler(sender, poll, user, **kwargs):
            self._poll2 = poll
            self._user2 = user._wrapped
        topic_poll_post_vote.connect(topic_poll_post_vote_handler)

        utils.login(self)
        poll = TopicPoll.objects.create(topic=self.topic)
        choice = TopicPollChoice.objects.create(poll=poll, description="op1")
        form_data = {'choices': choice.pk, }
        response = self.client.post(reverse('foro:poll-vote', kwargs={'pk': poll.pk, }),
                                    form_data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(repr(self._poll), repr(poll))
        self.assertEqual(repr(self._user), repr(self.user))
        self.assertEqual(repr(self._poll2), repr(poll))
        self.assertEqual(repr(self._user2), repr(self.user))
    def test_poll_vote_signal(self):
        """
        POST, poll_vote
        """
        def topic_poll_pre_vote_handler(sender, poll, user, **kwargs):
            self._poll = poll
            self._user = user._wrapped

        topic_poll_pre_vote.connect(topic_poll_pre_vote_handler)

        def topic_poll_post_vote_handler(sender, poll, user, **kwargs):
            self._poll2 = poll
            self._user2 = user._wrapped

        topic_poll_post_vote.connect(topic_poll_post_vote_handler)

        utils.login(self)
        poll = TopicPoll.objects.create(topic=self.topic)
        choice = TopicPollChoice.objects.create(poll=poll, description="op1")
        form_data = {
            'choices': choice.pk,
        }
        response = self.client.post(
            reverse('foro:poll-vote', kwargs={
                'pk': poll.pk,
            }), form_data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(repr(self._poll), repr(poll))
        self.assertEqual(repr(self._user), repr(self.user))
        self.assertEqual(repr(self._poll2), repr(poll))
        self.assertEqual(repr(self._user2), repr(self.user))
Пример #3
0
@python_2_unicode_compatible
class TopicPollVote(models.Model):

    choice = models.ForeignKey(TopicPollChoice, verbose_name=_("poll choice"), related_name='votes')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_("voter"))

    date = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ('user', 'choice')
        verbose_name = _("poll vote")
        verbose_name_plural = _("poll votes")

    def __str__(self):
        return "poll vote %s" % self.pk


def poll_pre_vote(sender, poll, user, **kwargs):
    TopicPollChoice.objects.filter(poll=poll, votes__user=user)\
        .update(vote_count=F('vote_count') - 1)


def poll_post_vote(sender, poll, user, **kwargs):
    TopicPollChoice.objects.filter(poll=poll, votes__user=user)\
        .update(vote_count=F('vote_count') + 1)


topic_poll_pre_vote.connect(poll_pre_vote, dispatch_uid=__name__)
topic_poll_post_vote.connect(poll_post_vote, dispatch_uid=__name__)