bb155b98a784b82309fadc616eba2b1f21fb1ba6
[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 the <strong>details needed for your perks</strong>,<br/>"
18             "and to send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
19             "Your e-mail won't be publicised."), required=False)
20
21     def __init__(self, offer, *args, **kwargs):
22         self.offer = offer
23         super(FundingForm, self).__init__(*args, **kwargs)
24         self.fields['amount'].widget.form_instance = self
25
26     def clean_amount(self):
27         if self.cleaned_data['amount'] < settings.FUNDING_MIN_AMOUNT:
28             min_amount = settings.FUNDING_MIN_AMOUNT
29             if isinstance(settings.FUNDING_MIN_AMOUNT, float):
30                 min_amount = formats.number_format(settings.FUNDING_MIN_AMOUNT, 2)
31             raise forms.ValidationError(
32                 ugettext("The minimum amount is %(amount)s PLN.") % {
33                     'amount': min_amount})
34         return self.cleaned_data['amount']
35
36     def clean(self):
37         if not self.offer.is_current():
38             raise forms.ValidationError(ugettext("This offer is out of date."))
39         return self.cleaned_data
40
41     def save(self):
42         funding = Funding.objects.create(
43             offer=self.offer,
44             name=self.cleaned_data['name'],
45             email=self.cleaned_data['email'],
46             amount=self.cleaned_data['amount'],
47             language_code = get_language(),
48         )
49         funding.perks = funding.offer.get_perks(funding.amount)
50         return funding
51