1 from django.conf import settings
2 from django import forms
3 from django.utils import formats
4 from django.utils.translation import ugettext_lazy as _, ugettext, get_language
5 from .models import Funding
6 from .widgets import PerksAmountWidget
9 class FundingForm(forms.Form):
10 required_css_class = 'required'
12 amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
13 widget=PerksAmountWidget())
14 name = forms.CharField(label=_("Name"), required=False,
15 help_text=_("Optional name for public list of contributors"))
16 email = forms.EmailField(label=_("Contact e-mail"),
17 help_text=_("We'll use it to contact you about your perks and fundraiser status and payment updates.<br/> "
18 "Won't be publicised."), required=False)
20 def __init__(self, offer, *args, **kwargs):
22 super(FundingForm, self).__init__(*args, **kwargs)
23 self.fields['amount'].widget.form_instance = self
25 def clean_amount(self):
26 if self.cleaned_data['amount'] < settings.FUNDING_MIN_AMOUNT:
27 min_amount = settings.FUNDING_MIN_AMOUNT
28 if isinstance(settings.FUNDING_MIN_AMOUNT, float):
29 min_amount = formats.number_format(settings.FUNDING_MIN_AMOUNT, 2)
30 raise forms.ValidationError(
31 ugettext("The minimum amount is %(amount)s PLN.") % {
32 'amount': min_amount})
33 return self.cleaned_data['amount']
36 if not self.offer.is_current():
37 raise forms.ValidationError(ugettext("This offer is out of date."))
38 return self.cleaned_data
41 funding = Funding.objects.create(
43 name=self.cleaned_data['name'],
44 email=self.cleaned_data['email'],
45 amount=self.cleaned_data['amount'],
46 language_code = get_language(),
48 funding.perks = funding.offer.get_perks(funding.amount)