Skip to content

Commit

Permalink
Finish rewriting tutorial 5
Browse files Browse the repository at this point in the history
  • Loading branch information
hjwp committed Apr 9, 2012
1 parent 64ae7bd commit 01a12be
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 433 deletions.
155 changes: 0 additions & 155 deletions mysite/fts/test_polls.py

This file was deleted.

36 changes: 33 additions & 3 deletions mysite/fts/tests.py
Expand Up @@ -218,9 +218,39 @@ def test_voting_on_a_new_poll(self):
# The page refreshes, and he sees that his choice
# has updated the results. they now say
# "100 %: very awesome".
self.fail('TODO')

# The page also says "1 votes"
body_text = self.browser.find_element_by_tag_name('body').text
self.assertIn('100 %: Very awesome', body_text)

# The page also says "1 vote"
self.assertIn('1 vote', body_text)

# But not "1 votes" -- Herbert is impressed at the attention to detail
self.assertNotIn('1 votes', body_text)

# Herbert suspects that the website isn't very well protected
# against people submitting multiple votes yet, so he tries
# to do a little astroturfing
self.browser.find_element_by_css_selector("input[value='1']").click()
self.browser.find_element_by_css_selector("input[type='submit']").click()

# The page refreshes, and he sees that his choice has updated the
# results. it still says # "100 %: very awesome".
body_text = self.browser.find_element_by_tag_name('body').text
self.assertIn('100 %: Very awesome', body_text)

# But the page now says "2 votes"
self.assertIn('2 votes', body_text)

# Cackling manically over his l33t haxx0ring skills, he tries
# voting for a different choice
self.browser.find_element_by_css_selector("input[value='2']").click()
self.browser.find_element_by_css_selector("input[type='submit']").click()

# Now, the percentages update, as well as the votes
body_text = self.browser.find_element_by_tag_name('body').text
self.assertIn('67 %: Very awesome', body_text)
self.assertIn('33 %: Quite awesome', body_text)
self.assertIn('3 votes', body_text)

# Satisfied, he goes back to sleep

Expand Down
11 changes: 11 additions & 0 deletions mysite/polls/models.py
Expand Up @@ -7,7 +7,18 @@ class Poll(models.Model):
def __unicode__(self):
return self.question

def total_votes(self):
return sum(c.votes for c in self.choice_set.all())



class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def percentage(self):
try:
return 100.0 * self.votes / self.poll.total_votes()
except ZeroDivisionError:
return 0
19 changes: 16 additions & 3 deletions mysite/polls/templates/poll.html
Expand Up @@ -4,11 +4,24 @@ <h1>Poll Results</h1>

<h2>{{poll.question}}</h2>

<p>No-one has voted on this poll yet</p>
<ul>
{% for choice in poll.choice_set.all %}
<li>{{ choice.percentage|floatformat:0 }} %: {{ choice.choice }}</li>
{% endfor %}
</ul>

{% if poll.total_votes != 0 %}
<p>{{ poll.total_votes }} vote{{ poll.total_votes|pluralize }}</p>
{% else %}
<p>No-one has voted on this poll yet</p>
{% endif %}

<h3>Add your vote</h3>
{{form.as_p}}
<input type="submit" />
<form method="POST" action="">
{% csrf_token %}
{{form.as_p}}
<input type="submit" />
</form>

</body>
</html>

0 comments on commit 01a12be

Please sign in to comment.