6eba5175b62e899f02ffd0d38341b9defaa60faf
[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. <br/>"
14             "Leave empty if you prefer to remain anonymous. <br/>"
15             "If we need any data for your perks, we'll get to you by e-mail anyway."))
16     email = forms.EmailField(label=_("Contact e-mail"),
17         help_text=_("Won't be publicised. <br/>"
18             "We'll use it to contact you about your perks and fundraiser status and payment updates.<br/> "
19             "Leave empty if you prefer not to be contacted by us."), 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'] <= 0:
28             raise forms.ValidationError(__("Enter positive amount."))
29         return self.cleaned_data['amount']
30
31     def clean(self):
32         if not self.offer.is_current():
33             raise forms.ValidationError(__("This offer is out of date."))
34         return self.cleaned_data
35
36     def save(self):
37         return Funding.objects.create(
38             offer=self.offer,
39             name=self.cleaned_data['name'],
40             email=self.cleaned_data['email'],
41             amount=self.cleaned_data['amount'],
42         )
43