Ejemplo n.º 1
0
    def test_view_can_handle_votes_via_POST(self):
        # set up a poll with choices
        poll1 = Poll(question='6 times 7', pub_date=timezone.now())
        poll1.save()
        choice1 = Choice(poll=poll1, choice='42', votes=1)
        choice1.save()
        choice2 = Choice(poll=poll1, choice='The Ultimate Answer', votes=3)
        choice2.save()

        # set up our POST data - keys and values are strings
        post_data = {'vote': str(choice2.id)}

        # make our request to the view
        poll_url = '/poll/polls/%d/' % (poll1.id,)
        response = self.client.post(poll_url, data=post_data)

        # retrieve the updated choice from the database
        choice_in_db = Choice.objects.get(pk=choice2.id)

        # check it's votes have gone up by 1
        self.assertEquals(choice_in_db.votes, 4)
        self.assertEquals(choice_in_db.percentage(), 80)

        # always redirect after a POST - even if, in this case, we go back
        # to the same page.
        self.assertRedirects(response, poll_url)
Ejemplo n.º 2
0
def seed_polls(num_entries=10, choice_min=2, choice_max=5, overwrite = False):
    """
    Seeds num_entries poll with random users as owners
    Each poll will be seeded with # choices from choice_min to choice_max
    """

    if overwrite:
        print("over writing poll")
        Poll.objects.all().delete()
    users = list(User.objects.all())
    count = 0
    for _ in range(num_entries):
        p = Poll(
            owner = random.choice(users),
            text=fake.paragraph(),
            pub_date=datetime.datetime.now()
        )
        p.save()
        num_choices = random.randrange(choice_min,choice_max+1)
        for _ in range(num_choices):
            c = Choice(
                poll=p,
                choice_text=fake.sentence()
            )
            c.save()
            count += 1
            perecent_complete = count / num_entries * 100
            print(
                "Addind {} new poll: {:.2f}%".format(
                    num_entries, perecent_complete
                ),
                end='\r',
                flush=True
            )
        print()
Ejemplo n.º 3
0
    def convert_poll_choices(self):
        cursor = connection.cursor();
        cursor.execute("SELECT * FROM smf_poll_choices");
        rows = cursor.fetchall()

        for row in rows:
            try: 
                poll = Poll.objects.get(old_poll_id=row[0])
                try:
                    choice = Choice()
                    choice.poll = poll
                    choice.old_choice_id = row[1]
                    choice.label = row[2]
                    choice.votes = row[3]
                    choice.save()

                except Exception, e:
                    print str(e)
            except Poll.DoesNotExist, e:
                print str(e)
Ejemplo n.º 4
0
    def convert_poll_choices(self):
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM smf_poll_choices")
        rows = cursor.fetchall()

        for row in rows:
            try:
                poll = Poll.objects.get(old_poll_id=row[0])
                try:
                    choice = Choice()
                    choice.poll = poll
                    choice.old_choice_id = row[1]
                    choice.label = row[2]
                    choice.votes = row[3]
                    choice.save()

                except Exception, e:
                    print str(e)
            except Poll.DoesNotExist, e:
                print str(e)
Ejemplo n.º 5
0
    def test_get_number_of_votes(self, app):
        with app.app_context():
            now = datetime.now()
            p = Poll(id=None, name="Nom", date=now)
            p.save()

            assert p.number_of_votes() == 0

            c1 = Choice(id=None, choice="Premier choix", poll=p)
            c1.save()

            c2 = Choice(id=None, choice="Deuxième choix", poll=p)
            c2.save()

            assert p.number_of_votes() == 0

            c1.cast_vote()
            c1.cast_vote()
            c2.cast_vote()

            assert p.number_of_votes() == 3
Ejemplo n.º 6
0
    def test_form_renders_poll_choices_as_radio_inputs(self):
        # set up a poll with a couple of choices
        poll1 = Poll(question="6 times 7", pub_date=timezone.now())
        poll1.save()
        choice1 = Choice(poll=poll1, choice="42", votes=0)
        choice1.save()
        choice2 = Choice(poll=poll1, choice="The Ultimate Answer", votes=0)
        choice2.save()

        # set up another poll to make sure we only see the right choices
        poll2 = Poll(question="time", pub_date=timezone.now())
        poll2.save()
        choice3 = Choice(poll=poll2, choice="PM", votes=0)
        choice3.save()

        # build a voting form for poll1
        form = PollVoteForm(poll=poll1)

        # check it has a single field called 'vote', which has right choices:
        self.assertEquals(form.fields.keys(), ["vote"])

        # choices are tuples in the format (choice_number, choice_text):
        self.assertEquals(form.fields["vote"].choices, [(choice1.id, choice1.choice), (choice2.id, choice2.choice)])

        # check it uses radio inputs to render
        self.assertIn('input type="radio"', form.as_p())
Ejemplo n.º 7
0
 def setup_choices(self, question):
     choice1 = Choice(code="a", question=question, text="a")
     choice2 = Choice(code="b", question=question, text="a")
     choice3 = Choice(code="c", question=question, text="a")
     choice1.save()
     choice2.save()
     choice3.save()
Ejemplo n.º 8
0
 def setup_choices(self,question):
     choice1 = Choice(code= 'a',question=question, text="a")
     choice2 = Choice(code= 'b',question=question, text="a")
     choice3 = Choice(code= 'c',question=question, text="a")
     choice1.save()
     choice2.save()
     choice3.save()
Ejemplo n.º 9
0
    def test_page_shows_choices_using_form(self):
        # set up a poll with choices
        poll1 = Poll(question='time', pub_date=timezone.now())
        poll1.save()
        choice1 = Choice(poll=poll1, choice="PM", votes=0)
        choice1.save()
        choice2 = Choice(poll=poll1, choice="Gardener's", votes=0)
        choice2.save()

        response = self.client.get('/poll/polls/%d/' % (poll1.id, ))

        # check we've passed in a form of the right type
        self.assertTrue(isinstance(response.context['form'], PollVoteForm))
        self.assertTrue(response.context['poll'], poll1)
        self.assertTemplateUsed(response, 'poll.html')

        # and check the check the form is being used in the template,
        # by checking for the choice text
        self.assertIn(choice1.choice, response.content.replace(''', "'"))
        self.assertIn(choice2.choice, response.content.replace(''', "'"))
        self.assertIn('csrf', response.content)
        self.assertIn('0 %', response.content)
Ejemplo n.º 10
0
    def create_new_choice_for_poll_from_post_data(cls, poll, post_data):
        choice = Choice(id=None, choice=post_data['choice'], poll=poll)
        choice.save()

        return choice