1 from django import forms
2 from django.utils.translation import ugettext_lazy as _, ugettext as __
3 from .models import Funding
4 from .widgets import PerksAmountWidget
7 class FundingForm(forms.Form):
8 required_css_class = 'required'
10 amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
11 widget=PerksAmountWidget())
12 name = forms.CharField(label=_("Name"), required=False,
13 help_text=_("Optional name for public list of contributors"))
14 email = forms.EmailField(label=_("Contact e-mail"),
15 help_text=_("We'll use it to contact you about your perks and fundraiser status and payment updates.<br/> "
16 "Won't be publicised."), required=False)
18 def __init__(self, offer, *args, **kwargs):
20 super(FundingForm, self).__init__(*args, **kwargs)
21 self.fields['amount'].widget.form_instance = self
23 def clean_amount(self):
24 if self.cleaned_data['amount'] <= 0:
25 raise forms.ValidationError(__("Enter positive amount."))
26 return self.cleaned_data['amount']
29 if not self.offer.is_current():
30 raise forms.ValidationError(__("This offer is out of date."))
31 return self.cleaned_data
34 funding = Funding.objects.create(
36 name=self.cleaned_data['name'],
37 email=self.cleaned_data['email'],
38 amount=self.cleaned_data['amount'],
40 funding.perks = funding.offer.get_perks(funding.amount)