Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ class Poll(models.Model):
pub_date = models.DateTimeField('date published')
ballots = models.IntegerField(default=0)
def total_votes(self):
v = 0
for c in self.choice_set.all():
v += c.votes
return v
return sum(c.votes for c in self.choice_set.all())

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Poll.total_votes refactored with the following changes:

def __unicode__(self):
return self.question

Expand All @@ -17,9 +14,7 @@ class Choice(models.Model):
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def percentage(self):
if self.poll.ballots == 0:
return 0
return self.votes*100/self.poll.ballots
return 0 if self.poll.ballots == 0 else self.votes*100/self.poll.ballots
Comment on lines -20 to +17

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Choice.percentage refactored with the following changes:

def __unicode__(self):
return self.choice_text

Expand Down
14 changes: 7 additions & 7 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class ResultsView(generic.DetailView):
def vote(request, poll_id):
p = get_object_or_404(Poll, pk=poll_id)
for counter,choice in enumerate(p.choice_set.all()):
try:
request.POST['choice'+str(counter+1)]
except (KeyError):
pass
else:
choice.votes += 1
choice.save()
try:
request.POST[f'choice{str(counter + 1)}']
except (KeyError):
pass
else:
choice.votes += 1
choice.save()
Comment on lines -36 to +42

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function vote refactored with the following changes:

p.ballots += 1
p.save()
return HttpResponseRedirect(reverse('approval_polls:results', args=(p.id,)))
Expand Down