Merge branch 'master' into funding
[wolnelektury.git] / apps / funding / forms.py
1 from django import forms
2 from django.utils.translation import ugettext_lazy as _, ugettext as __
3 from .models import Offer
4 from .widgets import PerksAmountWidget
5
6
7 class DummyForm(forms.Form):
8     required_css_class = 'required'
9
10     name = forms.CharField(label=_("Name"))
11     anonymous = forms.BooleanField(label=_("Anonymously"),
12         required=False,
13         help_text=_("Check if you don't want your name to be visible publicly."))
14     email = forms.EmailField(label=_("E-mail"),
15         help_text=_("Won't be publicised."))
16     amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
17         widget=PerksAmountWidget())
18
19     def __init__(self, offer, *args, **kwargs):
20         self.offer = offer
21         super(DummyForm, self).__init__(*args, **kwargs)
22         self.fields['amount'].widget.form_instance = self
23
24     def clean_amount(self):
25         if self.cleaned_data['amount'] <= 0:
26             raise forms.ValidationError(__("Enter positive amount."))
27         return self.cleaned_data['amount']
28
29     def clean(self):
30         if not self.offer.is_current():
31             raise forms.ValidationError(__("This offer is out of date."))
32         return self.cleaned_data
33
34     def save(self):
35         return self.offer.fund(
36             name=self.cleaned_data['name'],
37             email=self.cleaned_data['email'],
38             amount=self.cleaned_data['amount'],
39             anonymous=self.cleaned_data['anonymous'],
40         )
41