-
-
Notifications
You must be signed in to change notification settings - Fork 995
/
Copy pathforms.py
177 lines (149 loc) · 5.28 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import stripe
from django import forms
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from django_recaptcha.fields import ReCaptchaField
from django_recaptcha.widgets import ReCaptchaV3
from .models import INTERVAL_CHOICES, LEADERSHIP_LEVEL_AMOUNT, DjangoHero, Donation
class DjangoHeroForm(forms.ModelForm):
hero_type = forms.ChoiceField(
required=False,
widget=forms.RadioSelect,
label=_("I am donating as an"),
choices=DjangoHero.HERO_TYPE_CHOICES,
initial=DjangoHero.HERO_TYPE_CHOICES[0][0],
)
name = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"class": "required",
"placeholder": _("Your name or the name of your organization"),
},
),
)
location = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"placeholder": (
_("Where are you located? (optional; will not be displayed)")
),
},
),
)
url = forms.CharField(
required=False,
widget=forms.TextInput(
attrs={
"placeholder": _("Which URL should we link your name to?"),
},
),
)
logo = forms.FileField(
required=False,
help_text=_(
"If you've donated at least US $%d in a calendar year, you can submit your logo and "
"we will display it, too."
)
% LEADERSHIP_LEVEL_AMOUNT,
)
class Meta:
model = DjangoHero
fields = (
"hero_type",
"name",
"location",
"url",
"logo",
"is_visible",
"is_subscribed",
)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.checkbox_fields = []
self.radio_select_fields = []
for name, field in self.fields.items():
if isinstance(field.widget, forms.CheckboxInput):
self.checkbox_fields.append(name)
elif isinstance(field.widget, forms.RadioSelect):
self.radio_select_fields.append(name)
def save(self, commit=True):
hero = super().save(commit=commit)
customer = stripe.Customer.retrieve(hero.stripe_customer_id)
customer.description = hero.name or None
customer.email = hero.email or None
customer.save()
return hero
class StripeTextInput(forms.TextInput):
"""
Inspired by widgets in django-zebra
"""
def _add_data_stripe_attr(self, name, kwargs):
kwargs.setdefault("attrs", {}).update({"data-stripe": name})
return kwargs
def _strip_name_attr(self, widget_string, name):
return widget_string.replace(f'name="{name}"', "")
def render(self, name, *args, **kwargs):
kwargs = self._add_data_stripe_attr(name, kwargs)
rendered = super().render(name, *args, **kwargs)
return mark_safe(self._strip_name_attr(rendered, name))
class DonateForm(forms.Form):
"""
Used to generate the HTML form in the fundraising page.
"""
AMOUNT_CHOICES = (
(25, _("US $25")),
(50, _("US $50")),
(100, _("US $100")),
(250, _("US $250")),
(500, _("US $500")),
(750, _("US $750")),
(1000, _("US $1,000")),
(1250, _("US $1,250")),
(2500, _("US $2,500")),
("custom", _("Other amount")),
)
amount = forms.ChoiceField(choices=AMOUNT_CHOICES)
interval = forms.ChoiceField(choices=INTERVAL_CHOICES)
captcha = ReCaptchaField(widget=ReCaptchaV3(action="form"))
class DonationForm(forms.ModelForm):
"""
Used in the manage donations view.
"""
subscription_amount = forms.DecimalField(
max_digits=9, decimal_places=2, required=True
)
# here we're removing "onetime" option from interval choices:
interval = forms.ChoiceField(choices=INTERVAL_CHOICES[:3], required=True)
class Meta:
model = Donation
fields = ("subscription_amount", "interval")
def save(self, commit=True, *args, **kwargs):
donation = super().save(commit=commit)
interval = self.cleaned_data.get("interval")
amount = self.cleaned_data.get("subscription_amount")
# Send data to Stripe
customer = stripe.Customer.retrieve(donation.stripe_customer_id)
subscription = customer.subscriptions.retrieve(donation.stripe_subscription_id)
# TODO: Setting the plan is deprecated — use Price API instead.
subscription.plan = interval
subscription.quantity = int(amount)
subscription.save()
return donation
class PaymentForm(forms.Form):
"""
Used to validate values when configuring the Stripe Session.
`amount` can be any integer, so a ChoiceField is not appropriate.
"""
# NOTE: `action` needs to match the one used in stripe-donation.js
captcha = ReCaptchaField(widget=ReCaptchaV3(action="form"))
amount = forms.IntegerField(
required=True,
min_value=1, # Minimum payment from Stripe API
max_value=999_999, # Reject clearly unrealistic amounts.
)
interval = forms.ChoiceField(
required=True,
choices=INTERVAL_CHOICES,
)