f566e8ef95b72cd958d34c20cbba03fde3159422
[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 FundingForm(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         help_text=_("Optional name for public list of contributors"))
14     email = forms.EmailField(label=_("Contact e-mail"),
15         help_text=_("We'll use it to contact you about your perks and fundraiser status and payment updates.<br/> "
16             "Won't be publicised."), required=False)
17
18     def __init__(self, offer, *args, **kwargs):
19         self.offer = offer
20         super(FundingForm, self).__init__(*args, **kwargs)
21         self.fields['amount'].widget.form_instance = self
22
23     def clean_amount(self):
24         if self.cleaned_data['amount'] <= 0:
25             raise forms.ValidationError(__("Enter positive amount."))
26         return self.cleaned_data['amount']
27
28     def clean(self):
29         if not self.offer.is_current():
30             raise forms.ValidationError(__("This offer is out of date."))
31         return self.cleaned_data
32
33     def save(self):
34         return Funding.objects.create(
35             offer=self.offer,
36             name=self.cleaned_data['name'],
37             email=self.cleaned_data['email'],
38             amount=self.cleaned_data['amount'],
39         )
40