Fundraising in PDF.
[wolnelektury.git] / src / funding / forms.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. 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=_("Kwota"), decimal_places=2, widget=PerksAmountWidget())
23     name = forms.CharField(
24         label=_("Imię i nazwisko na listę darczyńców"),
25         required=False,
26         help_text=_("Opcjonalnie imię i nazwisko lub pseudonim do publicznej listy darczyńców.")
27     )
28     email = forms.EmailField(
29         label=_("E-mail kontaktowy"),
30         help_text=mark_safe(_(
31             "Użyjemy go do informowania Cię o zmianach statusu płatności i zbiórki "
32             "(z czego zawsze możesz zrezygnować).<br/>"
33             "Twój adres e-mail nie będzie upubliczniony.")),
34         required=False)
35
36     data_processing_part2 = _('''\
37 W przypadku podania danych zostaną one wykorzystane w sposób podany powyżej, a w przypadku wyrażenia dodatkowej zgody 
38 adres e-mail zostanie wykorzystany także w celu przesyłania newslettera Wolnych Lektur.''')
39
40     def __init__(self, request, offer, *args, **kwargs):
41         self.offer = offer
42         self.user = request.user if request.user.is_authenticated else None
43         self.client_ip = request.META['REMOTE_ADDR']
44         super(FundingForm, self).__init__(*args, **kwargs)
45         self.fields['amount'].widget.form_instance = self
46
47     def clean_amount(self):
48         if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
49             min_amount = app_settings.MIN_AMOUNT
50             if isinstance(min_amount, float):
51                 min_amount = formats.number_format(min_amount, 2)
52             raise forms.ValidationError(
53                 gettext("Minimalna kwota wpłaty to %(amount)s zł.") % {
54                     'amount': min_amount})
55         return self.cleaned_data['amount']
56
57     def clean(self):
58         if not self.offer.is_current():
59             raise forms.ValidationError(gettext("Ta zbiórka jest już nieaktywna."))
60         return self.cleaned_data
61
62     def save(self):
63         super(FundingForm, self).save()
64         funding = Funding.objects.create(
65             offer=self.offer,
66             name=self.cleaned_data['name'],
67             email=self.cleaned_data['email'],
68             amount=self.cleaned_data['amount'],
69             language_code=get_language(),
70             user=self.user,
71             pos_id=payment_method.pos_id,
72             customer_ip=self.client_ip,
73         )
74         funding.perks.set(funding.offer.get_perks(funding.amount))
75         return funding