c66211a71817c32828cc3bdcb36014d004bf161a
[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 Funding
4 from .widgets import PerksAmountWidget
5
6
7 class DummyForm(forms.Form):
8     required_css_class = 'required'
9
10     amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
11         widget=PerksAmountWidget())
12     name = forms.CharField(label=_("Name"), required=False)
13     anonymous = forms.BooleanField(label=_("Anonymously"),
14         required=False,
15         help_text=_("Check if you don't want your name to be visible publicly."))
16     email = forms.EmailField(label=_("E-mail"),
17         help_text=_("Won't be publicised."), required=False)
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 Funding.objects.create(
36             offer=self.offer,
37             name=self.cleaned_data['name'],
38             email=self.cleaned_data['email'],
39             amount=self.cleaned_data['amount'],
40             anonymous=self.cleaned_data['anonymous'],
41         )
42