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
 
   7 class FundingForm(forms.Form):
 
   8     required_css_class = 'required'
 
  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 updates.<br/> "
 
  19             "Leave empty if you prefer not to be contacted by us."), required=False)
 
  21     def __init__(self, offer, *args, **kwargs):
 
  23         super(FundingForm, self).__init__(*args, **kwargs)
 
  24         self.fields['amount'].widget.form_instance = self
 
  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']
 
  32         if not self.offer.is_current():
 
  33             raise forms.ValidationError(__("This offer is out of date."))
 
  34         return self.cleaned_data
 
  37         return Funding.objects.create(
 
  39             name=self.cleaned_data['name'],
 
  40             email=self.cleaned_data['email'],
 
  41             amount=self.cleaned_data['amount'],