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(_(
25 "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, request, offer, *args, **kwargs):
34 self.user = request.user if request.user.is_authenticated else None
35 super(FundingForm, self).__init__(*args, **kwargs)
36 self.fields['amount'].widget.form_instance = self
38 def clean_amount(self):
39 if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
40 min_amount = app_settings.MIN_AMOUNT
41 if isinstance(min_amount, float):
42 min_amount = formats.number_format(min_amount, 2)
43 raise forms.ValidationError(
44 ugettext("The minimum amount is %(amount)s PLN.") % {
45 'amount': min_amount})
46 return self.cleaned_data['amount']
49 if not self.offer.is_current():
50 raise forms.ValidationError(ugettext("This offer is out of date."))
51 return self.cleaned_data
54 super(FundingForm, self).save()
55 funding = Funding.objects.create(
57 name=self.cleaned_data['name'],
58 email=self.cleaned_data['email'],
59 amount=self.cleaned_data['amount'],
60 language_code=get_language(),
63 funding.perks.set(funding.offer.get_perks(funding.amount))