DRYs up the way to define a Stripe-style form field.
This commit is contained in:
parent
830864df2c
commit
cbf3f5814b
1 changed files with 52 additions and 57 deletions
|
@ -1,6 +1,4 @@
|
||||||
from functools import partial
|
import copy
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse
|
||||||
|
@ -19,40 +17,47 @@ class NoRenderWidget(forms.widgets.HiddenInput):
|
||||||
return "<!-- no widget: " + name + " -->"
|
return "<!-- no widget: " + name + " -->"
|
||||||
|
|
||||||
|
|
||||||
def secure_striped(widget):
|
def secure_striped(field):
|
||||||
''' Calls stripe() with secure=True. '''
|
''' Calls stripe() with secure=True. '''
|
||||||
return striped(widget, True)
|
return striped(field, True)
|
||||||
|
|
||||||
|
|
||||||
def striped(WidgetClass, secure=False):
|
def striped(field, secure=False):
|
||||||
''' Takes a given widget and overrides the render method to be suitable
|
|
||||||
for stripe.js.
|
|
||||||
|
|
||||||
Arguments:
|
oldwidget = field.widget
|
||||||
widget: The widget class
|
field.widget = StripeWidgetProxy(oldwidget, secure)
|
||||||
|
return field
|
||||||
|
|
||||||
secure: if True, only the `data-stripe` attribute will be set. Name
|
|
||||||
will be set to None.
|
|
||||||
|
|
||||||
'''
|
class StripeWidgetProxy(widgets.Widget):
|
||||||
|
|
||||||
class StripedWidget(WidgetClass):
|
def __init__(self, underlying, secure=False):
|
||||||
|
self.underlying = underlying
|
||||||
|
self.secure = secure
|
||||||
|
|
||||||
|
def __deepcopy__(self, memo):
|
||||||
|
copy_underlying = copy.deepcopy(self.underlying, memo)
|
||||||
|
return type(self)(copy_underlying)
|
||||||
|
|
||||||
|
def __getattribute__(self, attr):
|
||||||
|
spr = super(StripeWidgetProxy, self).__getattribute__
|
||||||
|
if attr in ("underlying", "render", "secure", "__deepcopy__"):
|
||||||
|
return spr(attr)
|
||||||
|
else:
|
||||||
|
return getattr(self.underlying, attr)
|
||||||
|
|
||||||
def render(self, name, value, attrs=None):
|
def render(self, name, value, attrs=None):
|
||||||
|
|
||||||
|
print "RENDER: " + name
|
||||||
if not attrs:
|
if not attrs:
|
||||||
attrs = {}
|
attrs = {}
|
||||||
|
|
||||||
attrs["data-stripe"] = name
|
attrs["data-stripe"] = name
|
||||||
|
|
||||||
if secure:
|
if self.secure:
|
||||||
name = ""
|
name = ""
|
||||||
|
|
||||||
return super(StripedWidget, self).render(
|
return self.underlying.render(name, value, attrs=attrs)
|
||||||
name, value, attrs=attrs
|
|
||||||
)
|
|
||||||
|
|
||||||
return StripedWidget
|
|
||||||
|
|
||||||
|
|
||||||
class CreditCardForm(forms.Form):
|
class CreditCardForm(forms.Form):
|
||||||
|
@ -67,33 +72,29 @@ class CreditCardForm(forms.Form):
|
||||||
|
|
||||||
media = property(_media)
|
media = property(_media)
|
||||||
|
|
||||||
number = forms.CharField(
|
number = secure_striped(forms.CharField(
|
||||||
required=False,
|
required=False,
|
||||||
label="Credit card Number",
|
label="Credit card Number",
|
||||||
help_text="Your credit card number, with or without spaces.",
|
help_text="Your credit card number, with or without spaces.",
|
||||||
max_length=255,
|
max_length=255,
|
||||||
widget=secure_striped(widgets.TextInput)(),
|
))
|
||||||
)
|
exp_month = secure_striped(forms.IntegerField(
|
||||||
exp_month = forms.IntegerField(
|
|
||||||
required=False,
|
required=False,
|
||||||
label="Card expiry month",
|
label="Card expiry month",
|
||||||
min_value=1,
|
min_value=1,
|
||||||
max_value=12,
|
max_value=12,
|
||||||
widget=secure_striped(widgets.TextInput)(),
|
))
|
||||||
)
|
exp_year = secure_striped(forms.IntegerField(
|
||||||
exp_year = forms.IntegerField(
|
|
||||||
required=False,
|
required=False,
|
||||||
label="Card expiry year",
|
label="Card expiry year",
|
||||||
help_text="The expiry year for your card in 4-digit form",
|
help_text="The expiry year for your card in 4-digit form",
|
||||||
min_value=lambda: timezone.now().year,
|
min_value=lambda: timezone.now().year,
|
||||||
widget=secure_striped(widgets.TextInput)(),
|
))
|
||||||
)
|
cvc = secure_striped(forms.CharField(
|
||||||
cvc = forms.CharField(
|
|
||||||
required=False,
|
required=False,
|
||||||
min_length=3,
|
min_length=3,
|
||||||
max_length=4,
|
max_length=4,
|
||||||
widget=secure_striped(widgets.TextInput)(),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
stripe_token = forms.CharField(
|
stripe_token = forms.CharField(
|
||||||
max_length=255,
|
max_length=255,
|
||||||
|
@ -101,48 +102,42 @@ class CreditCardForm(forms.Form):
|
||||||
widget=NoRenderWidget(),
|
widget=NoRenderWidget(),
|
||||||
)
|
)
|
||||||
|
|
||||||
name = forms.CharField(
|
name = striped(forms.CharField(
|
||||||
required=True,
|
required=True,
|
||||||
label="Cardholder name",
|
label="Cardholder name",
|
||||||
help_text="The cardholder's name, as it appears on the credit card",
|
help_text="The cardholder's name, as it appears on the credit card",
|
||||||
max_length=255,
|
max_length=255,
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_line1 = striped(forms.CharField(
|
||||||
address_line1 = forms.CharField(
|
|
||||||
required=True,
|
required=True,
|
||||||
label="Cardholder account address, line 1",
|
label="Cardholder account address, line 1",
|
||||||
max_length=255,
|
max_length=255,
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_line2 = striped(forms.CharField(
|
||||||
address_line2 = forms.CharField(
|
|
||||||
required=False,
|
required=False,
|
||||||
label="Cardholder account address, line 2",
|
label="Cardholder account address, line 2",
|
||||||
max_length=255,
|
max_length=255,
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_city = striped(forms.CharField(
|
||||||
address_city = forms.CharField(
|
|
||||||
required=True,
|
required=True,
|
||||||
label="Cardholder account city",
|
label="Cardholder account city",
|
||||||
max_length=255,
|
max_length=255,
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_state = striped(forms.CharField(
|
||||||
address_state = forms.CharField(
|
|
||||||
required=True,
|
required=True,
|
||||||
max_length=255,
|
max_length=255,
|
||||||
label="Cardholder account state or province",
|
label="Cardholder account state or province",
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_zip = striped(forms.CharField(
|
||||||
address_zip = forms.CharField(
|
|
||||||
required=True,
|
required=True,
|
||||||
max_length=255,
|
max_length=255,
|
||||||
label="Cardholder account postal code",
|
label="Cardholder account postal code",
|
||||||
widget=striped(widgets.TextInput),
|
))
|
||||||
)
|
address_country = striped(LazyTypedChoiceField(
|
||||||
address_country = LazyTypedChoiceField(
|
|
||||||
label="Cardholder account country",
|
label="Cardholder account country",
|
||||||
choices=countries,
|
choices=countries,
|
||||||
widget=striped(CountrySelectWidget),
|
widget=CountrySelectWidget,
|
||||||
)
|
))
|
||||||
|
|
||||||
|
|
||||||
'''{
|
'''{
|
||||||
|
|
Loading…
Reference in a new issue