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 gettext_lazy as _, gettext, get_language
9 from newsletter.forms import NewsletterForm
10 from club.payment_methods import PayU
11 from .models import Funding
12 from .widgets import PerksAmountWidget
13 from . import app_settings
16 payment_method = PayU(app_settings.PAYU_POS)
19 class FundingForm(NewsletterForm):
20 required_css_class = 'required'
22 amount = forms.DecimalField(label=_("Amount"), decimal_places=2, widget=PerksAmountWidget())
23 name = forms.CharField(
24 label=_("Name"), required=False, help_text=_("Optional name for public list of contributors"))
25 email = forms.EmailField(
26 label=_("Contact e-mail"),
27 help_text=mark_safe(_(
29 "send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
30 "Your e-mail won't be publicised.")), required=False)
32 data_processing_part2 = '''\
33 W przypadku podania danych zostaną one wykorzystane w sposób podany powyżej, a w przypadku wyrażenia dodatkowej zgody
34 adres e-mail zostanie wykorzystany także w celu przesyłania newslettera Wolnych Lektur.'''
36 def __init__(self, request, offer, *args, **kwargs):
38 self.user = request.user if request.user.is_authenticated else None
39 self.client_ip = request.META['REMOTE_ADDR']
40 super(FundingForm, self).__init__(*args, **kwargs)
41 self.fields['amount'].widget.form_instance = self
43 def clean_amount(self):
44 if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
45 min_amount = app_settings.MIN_AMOUNT
46 if isinstance(min_amount, float):
47 min_amount = formats.number_format(min_amount, 2)
48 raise forms.ValidationError(
49 gettext("The minimum amount is %(amount)s PLN.") % {
50 'amount': min_amount})
51 return self.cleaned_data['amount']
54 if not self.offer.is_current():
55 raise forms.ValidationError(gettext("This offer is out of date."))
56 return self.cleaned_data
59 super(FundingForm, self).save()
60 funding = Funding.objects.create(
62 name=self.cleaned_data['name'],
63 email=self.cleaned_data['email'],
64 amount=self.cleaned_data['amount'],
65 language_code=get_language(),
67 pos_id=payment_method.pos_id,
68 customer_ip=self.client_ip,
70 funding.perks.set(funding.offer.get_perks(funding.amount))