dc0e6236d312b2e62e78b8d6902aeab212e56563
[wolnelektury.git] / apps / funding / forms.py
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
7
8
9 class FundingForm(forms.Form):
10     required_css_class = 'required'
11
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)
19
20     def __init__(self, offer, *args, **kwargs):
21         self.offer = offer
22         super(FundingForm, self).__init__(*args, **kwargs)
23         self.fields['amount'].widget.form_instance = self
24
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']
34
35     def clean(self):
36         if not self.offer.is_current():
37             raise forms.ValidationError(ugettext("This offer is out of date."))
38         return self.cleaned_data
39
40     def save(self):
41         funding = Funding.objects.create(
42             offer=self.offer,
43             name=self.cleaned_data['name'],
44             email=self.cleaned_data['email'],
45             amount=self.cleaned_data['amount'],
46             language_code = get_language(),
47         )
48         funding.perks = funding.offer.get_perks(funding.amount)
49         return funding
50