Mercurial > public > sg101
comparison polls/forms.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/polls/forms.py@1f139de929c4 |
children |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """Forms for the Polls application.""" | |
2 | |
3 from django import forms | |
4 from django.db.models import F | |
5 | |
6 from polls.models import Choice | |
7 | |
8 | |
9 class VoteForm(forms.Form): | |
10 """Form for voting in a poll.""" | |
11 choices = forms.ModelChoiceField(label='', empty_label=None, | |
12 queryset=Choice.objects.none(), widget=forms.RadioSelect) | |
13 | |
14 def __init__(self, poll, *args, **kwargs): | |
15 self.user = kwargs.pop('user', None) | |
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() | |
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) |