annotate polls/forms.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents ee87ea74d46b
children
rev   line source
gremmie@1 1 """Forms for the Polls application."""
gremmie@1 2
gremmie@1 3 from django import forms
bgneal@439 4 from django.db.models import F
gremmie@1 5
gremmie@1 6 from polls.models import Choice
gremmie@1 7
gremmie@1 8
gremmie@1 9 class VoteForm(forms.Form):
bgneal@439 10 """Form for voting in a poll."""
bgneal@439 11 choices = forms.ModelChoiceField(label='', empty_label=None,
bgneal@439 12 queryset=Choice.objects.none(), widget=forms.RadioSelect)
gremmie@1 13
bgneal@439 14 def __init__(self, poll, *args, **kwargs):
bgneal@439 15 self.user = kwargs.pop('user', None)
bgneal@439 16 self.user_choice = kwargs.pop('user_choice', None)
bgneal@439 17 super(VoteForm, self).__init__(*args, **kwargs)
bgneal@439 18 self.fields['choices'].queryset = poll.choice_set.all()
gremmie@1 19
bgneal@439 20 def clean(self):
bgneal@439 21 if self.user_choice:
bgneal@439 22 raise forms.ValidationError("You've already voted in this poll!")
bgneal@439 23 return self.cleaned_data
bgneal@439 24
bgneal@439 25 def save(self):
bgneal@439 26 choice = self.cleaned_data['choices']
bgneal@439 27 Choice.objects.filter(id=choice.id).update(votes=F('votes') + 1)
bgneal@439 28 choice.voters.add(self.user)