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