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.safestring import mark_safe
8 from django.utils.translation import ugettext_lazy as _, ugettext, get_language
10 from newsletter.forms import NewsletterForm
11 from .models import Funding
12 from .widgets import PerksAmountWidget
13 from . import app_settings
16 class FundingForm(NewsletterForm):
17 required_css_class = 'required'
19 amount = forms.DecimalField(label=_("Amount"), decimal_places=2, widget=PerksAmountWidget())
20 name = forms.CharField(
21 label=_("Name"), required=False, help_text=_("Optional name for public list of contributors"))
22 email = forms.EmailField(
23 label=_("Contact e-mail"),
24 help_text=mark_safe(_(
25 "We'll use it to contact you about the <strong>details needed for your perks</strong>,<br/>"
26 "and to send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
27 "Your e-mail won't be publicised.")), required=False)
29 data_processing_part2 = u'''\
30 W przypadku podania danych zostaną one wykorzystane w sposób podany powyżej, a w przypadku wyrażenia dodatkowej zgody
31 adres e-mail zostanie wykorzystany także w celu przesyłania newslettera Wolnych Lektur.'''
33 def __init__(self, offer, *args, **kwargs):
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(),
62 funding.perks = funding.offer.get_perks(funding.amount)