-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathforms.py
125 lines (100 loc) · 3.71 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#
# Copyright © Michal Čihař <[email protected]>
#
# This file is part of Weblate <https://weblate.org/>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from __future__ import annotations
from django import forms
from django.conf import settings
from django.core.exceptions import ValidationError
from django.utils.translation import gettext
from fakturace.storage import InvoiceStorage
from weblate_web.models import REWARD_LEVELS, REWARDS, Donation, Service, Subscription
from weblate_web.payments.backends import list_backends
from weblate_web.payments.models import RECURRENCE_CHOICES
class MethodForm(forms.Form):
method = forms.ChoiceField(
choices=[],
required=True,
)
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.fields["method"].choices = [ # type: ignore[attr-defined]
(backend.name, backend.verbose) for backend in list_backends()
]
class DonateForm(forms.Form):
recurring = forms.ChoiceField(
choices=RECURRENCE_CHOICES,
initial="",
required=False,
)
amount = forms.IntegerField(
min_value=5,
initial=10,
)
reward = forms.TypedChoiceField(
choices=REWARDS, initial=0, required=False, coerce=int
)
def clean_reward(self):
if "reward" not in self.cleaned_data:
self.cleaned_data["reward"] = 0
elif (
self.cleaned_data.get("amount", 0)
< REWARD_LEVELS[self.cleaned_data["reward"]]
):
raise ValidationError(gettext("Insufficient donation for selected reward!"))
return self.cleaned_data["reward"]
class EditNameForm(forms.ModelForm):
class Meta:
model = Donation
fields = ("link_text",)
class EditLinkForm(forms.ModelForm):
class Meta:
model = Donation
fields = ("link_text", "link_url")
class EditImageForm(forms.ModelForm):
class Meta:
model = Donation
fields = ("link_text", "link_url", "link_image")
class EditDiscoveryForm(forms.ModelForm):
class Meta:
model = Service
fields = ("discover_text", "discover_image")
widgets = {"discover_text": forms.Textarea}
class AddDiscoveryForm(forms.ModelForm):
class Meta:
model = Service
fields = ("site_url", "discover_text", "discover_image")
widgets = {"discover_text": forms.Textarea}
class AddPaymentForm(forms.Form):
subscription = forms.ModelChoiceField(queryset=Subscription.objects.none())
invoice = forms.CharField(max_length=20)
period = forms.ChoiceField(
choices=RECURRENCE_CHOICES,
initial="y",
)
def clean_invoice(self):
if value := self.cleaned_data["invoice"]:
storage = InvoiceStorage(settings.PAYMENT_FAKTURACE)
try:
return storage.get(value)
except Exception as error:
raise ValidationError(
gettext("Invoice was not found: %s") % error
) from error
return None
class AgreementForm(forms.Form):
consent = forms.BooleanField(label="I consent to the agreement", required=True)