Update picture viewer.
[wolnelektury.git] / apps / 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 from .models import Funding
9 from .widgets import PerksAmountWidget
10 from . import app_settings
11
12
13 class FundingForm(forms.Form):
14     required_css_class = 'required'
15
16     amount = forms.DecimalField(label=_("Amount"), decimal_places=2,
17         widget=PerksAmountWidget())
18     name = forms.CharField(label=_("Name"), required=False,
19         help_text=_("Optional name for public list of contributors"))
20     email = forms.EmailField(label=_("Contact e-mail"),
21         help_text=_("We'll use it to contact you about the <strong>details needed for your perks</strong>,<br/>"
22             "and to send you updates about your payment and the fundraiser status (which you can always turn off).<br/>"
23             "Your e-mail won't be publicised."), required=False)
24
25     def __init__(self, offer, *args, **kwargs):
26         self.offer = offer
27         super(FundingForm, self).__init__(*args, **kwargs)
28         self.fields['amount'].widget.form_instance = self
29
30     def clean_amount(self):
31         if self.cleaned_data['amount'] < app_settings.MIN_AMOUNT:
32             min_amount = app_settings.MIN_AMOUNT
33             if isinstance(min_amount, float):
34                 min_amount = formats.number_format(min_amount, 2)
35             raise forms.ValidationError(
36                 ugettext("The minimum amount is %(amount)s PLN.") % {
37                     'amount': min_amount})
38         return self.cleaned_data['amount']
39
40     def clean(self):
41         if not self.offer.is_current():
42             raise forms.ValidationError(ugettext("This offer is out of date."))
43         return self.cleaned_data
44
45     def save(self):
46         funding = Funding.objects.create(
47             offer=self.offer,
48             name=self.cleaned_data['name'],
49             email=self.cleaned_data['email'],
50             amount=self.cleaned_data['amount'],
51             language_code = get_language(),
52         )
53         funding.perks = funding.offer.get_perks(funding.amount)
54         return funding
55