1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django import forms
6 from django.utils import formats
7 from django.utils.translation import ugettext_lazy as _, ugettext, get_language
8 from .models import Funding
9 from .widgets import PerksAmountWidget
10 from . import app_settings
13 class FundingForm(forms.Form):
14 required_css_class = 'required'
16 amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
17 widget=PerksAmountWidget())
18 name = forms.CharField(label=_("Name"), required=False,
19 help_text=_("Optional name for public list of contributors"))
20 email = forms.EmailField(label=_("Contact e-mail"),
21 help_text=_("We'll use it to contact you about the <strong>details needed for your perks</strong>,<br/>"
22 "and to send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
23 "Your e-mail won't be publicised."), required=False)
25 def __init__(self, offer, *args, **kwargs):
27 super(FundingForm, self).__init__(*args, **kwargs)
28 self.fields['amount'].widget.form_instance = self
30 def clean_amount(self):
31 if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
32 min_amount = app_settings.MIN_AMOUNT
33 if isinstance(min_amount, float):
34 min_amount = formats.number_format(min_amount, 2)
35 raise forms.ValidationError(
36 ugettext("The minimum amount is %(amount)s PLN.") % {
37 'amount': min_amount})
38 return self.cleaned_data['amount']
41 if not self.offer.is_current():
42 raise forms.ValidationError(ugettext("This offer is out of date."))
43 return self.cleaned_data
46 funding = Funding.objects.create(
48 name=self.cleaned_data['name'],
49 email=self.cleaned_data['email'],
50 amount=self.cleaned_data['amount'],
51 language_code = get_language(),
53 funding.perks = funding.offer.get_perks(funding.amount)