Skip to content

Commit

Permalink
Refactored how we save a vote.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastdriven committed Apr 18, 2011
1 parent 673be44 commit 81e2c68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 5 additions & 0 deletions polls/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ def __init__(self, *args, **kwargs):
# This has to be done here (instead of declaratively) because the
# ``Poll`` instance will change from request to request.
self.fields['choice'] = forms.ModelChoiceField(queryset=Choice.objects.filter(poll=self.instance.pk), empty_label=None, widget=forms.RadioSelect)

def save(self):
choice = self.cleaned_data['choice']
choice.record_vote()
return choice
4 changes: 4 additions & 0 deletions polls/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ class Choice(models.Model):

def __unicode__(self):
return self.choice

def record_vote(self):
self.votes += 1
self.save()
5 changes: 1 addition & 4 deletions polls/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ def detail(request, poll_id):
form = PollForm(request.POST, instance=p)

if form.is_valid():
choice = form.cleaned_data['choice']
choice.votes += 1
choice.save()

form.save()
return HttpResponseRedirect(reverse('polls_results', kwargs={'poll_id': p.id}))
else:
form = PollForm(instance=p)
Expand Down

0 comments on commit 81e2c68

Please sign in to comment.