Mercurial > public > sg101
comparison gpp/polls/forms.py @ 439:1f139de929c4
Fixing #216; added anti-ballot stuffing feature to the polls application.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 21 May 2011 19:55:48 +0000 |
parents | dbd703f7d63a |
children |
comparison
equal
deleted
inserted
replaced
438:524fd1b3919a | 439:1f139de929c4 |
---|---|
1 """Forms for the Polls application.""" | 1 """Forms for the Polls application.""" |
2 | 2 |
3 from django import forms | 3 from django import forms |
4 from django.db.models import F | |
4 | 5 |
5 from polls.models import Choice | 6 from polls.models import Choice |
6 | 7 |
7 | 8 |
8 class VoteForm(forms.Form): | 9 class VoteForm(forms.Form): |
9 """Form for voting in a poll.""" | 10 """Form for voting in a poll.""" |
10 choices = forms.ModelChoiceField(label='', empty_label=None, | 11 choices = forms.ModelChoiceField(label='', empty_label=None, |
11 queryset=Choice.objects.none(), widget=forms.RadioSelect) | 12 queryset=Choice.objects.none(), widget=forms.RadioSelect) |
12 | 13 |
13 def __init__(self, poll, *args, **kwargs): | 14 def __init__(self, poll, *args, **kwargs): |
14 super(VoteForm, self).__init__(*args, **kwargs) | 15 self.user = kwargs.pop('user', None) |
15 self.fields['choices'].queryset = poll.choice_set.all() | 16 self.user_choice = kwargs.pop('user_choice', None) |
17 super(VoteForm, self).__init__(*args, **kwargs) | |
18 self.fields['choices'].queryset = poll.choice_set.all() | |
16 | 19 |
20 def clean(self): | |
21 if self.user_choice: | |
22 raise forms.ValidationError("You've already voted in this poll!") | |
23 return self.cleaned_data | |
24 | |
25 def save(self): | |
26 choice = self.cleaned_data['choices'] | |
27 Choice.objects.filter(id=choice.id).update(votes=F('votes') + 1) | |
28 choice.voters.add(self.user) |