e055faf93765ef554350d0c04ceaaa6442ddcb4f
[wolnelektury.git] / src / funding / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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
8
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
14
15
16 payment_method = PayU(app_settings.PAYU_POS)
17
18
19 class FundingForm(NewsletterForm):
20     required_css_class = 'required'
21
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(_(
28             "We'll use it to "
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)
31
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.'''
35
36     def __init__(self, request, offer, *args, **kwargs):
37         self.offer = offer
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
42
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']
52
53     def clean(self):
54         if not self.offer.is_current():
55             raise forms.ValidationError(gettext("This offer is out of date."))
56         return self.cleaned_data
57
58     def save(self):
59         super(FundingForm, self).save()
60         funding = Funding.objects.create(
61             offer=self.offer,
62             name=self.cleaned_data['name'],
63             email=self.cleaned_data['email'],
64             amount=self.cleaned_data['amount'],
65             language_code=get_language(),
66             user=self.user,
67             pos_id=payment_method.pos_id,
68             customer_ip=self.client_ip,
69         )
70         funding.perks.set(funding.offer.get_perks(funding.amount))
71         return funding