add manifest link
[wolnelektury.git] / src / funding / forms.py
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.
4 #
5 from django import forms
6 from django.utils import formats
7 from django.utils.translation import ugettext_lazy as _, ugettext, get_language
8
9 from newsletter.forms import NewsletterForm
10 from .models import Funding
11 from .widgets import PerksAmountWidget
12 from . import app_settings
13
14
15 class FundingForm(NewsletterForm):
16     required_css_class = 'required'
17
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=_(
24             "We'll use it to contact you about the <strong>details needed for your perks</strong>,<br/>"
25             "and to 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)
27
28     def __init__(self, offer, *args, **kwargs):
29         self.offer = offer
30         super(FundingForm, self).__init__(*args, **kwargs)
31         self.fields['amount'].widget.form_instance = self
32
33     def clean_amount(self):
34         if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
35             min_amount = app_settings.MIN_AMOUNT
36             if isinstance(min_amount, float):
37                 min_amount = formats.number_format(min_amount, 2)
38             raise forms.ValidationError(
39                 ugettext("The minimum amount is %(amount)s PLN.") % {
40                     'amount': min_amount})
41         return self.cleaned_data['amount']
42
43     def clean(self):
44         if not self.offer.is_current():
45             raise forms.ValidationError(ugettext("This offer is out of date."))
46         return self.cleaned_data
47
48     def save(self):
49         super(FundingForm, self).save()
50         funding = Funding.objects.create(
51             offer=self.offer,
52             name=self.cleaned_data['name'],
53             email=self.cleaned_data['email'],
54             amount=self.cleaned_data['amount'],
55             language_code=get_language(),
56         )
57         funding.perks = funding.offer.get_perks(funding.amount)
58         return funding