1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django import forms
5 from django.utils import formats
6 from django.utils.safestring import mark_safe
7 from django.utils.translation import ugettext_lazy as _, ugettext, get_language
9 from newsletter.forms import NewsletterForm
10 from .models import Funding
11 from .widgets import PerksAmountWidget
12 from . import app_settings
15 class FundingForm(NewsletterForm):
16 required_css_class = 'required'
18 amount = forms.DecimalField(label=_("Amount"), decimal_places=2, widget=PerksAmountWidget())
19 name = forms.CharField(
20 label=_("Name"), required=False, help_text=_("Optional name for public list of contributors"))
21 email = forms.EmailField(
22 label=_("Contact e-mail"),
23 help_text=mark_safe(_(
24 "We'll use it to contact you about the <strong>details needed for your perks</strong>,<br/>"
25 "and to send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
26 "Your e-mail won't be publicised.")), required=False)
28 data_processing_part2 = '''\
29 W przypadku podania danych zostaną one wykorzystane w sposób podany powyżej, a w przypadku wyrażenia dodatkowej zgody
30 adres e-mail zostanie wykorzystany także w celu przesyłania newslettera Wolnych Lektur.'''
32 def __init__(self, offer, *args, **kwargs):
34 super(FundingForm, self).__init__(*args, **kwargs)
35 self.fields['amount'].widget.form_instance = self
37 def clean_amount(self):
38 if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
39 min_amount = app_settings.MIN_AMOUNT
40 if isinstance(min_amount, float):
41 min_amount = formats.number_format(min_amount, 2)
42 raise forms.ValidationError(
43 ugettext("The minimum amount is %(amount)s PLN.") % {
44 'amount': min_amount})
45 return self.cleaned_data['amount']
48 if not self.offer.is_current():
49 raise forms.ValidationError(ugettext("This offer is out of date."))
50 return self.cleaned_data
53 super(FundingForm, self).save()
54 funding = Funding.objects.create(
56 name=self.cleaned_data['name'],
57 email=self.cleaned_data['email'],
58 amount=self.cleaned_data['amount'],
59 language_code=get_language(),
61 funding.perks = funding.offer.get_perks(funding.amount)