forked from wsijp/cartridge-card
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
75 lines (53 loc) · 2.67 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
from django.utils.translation import ugettext_lazy as _
from django import forms
from datetime import date
from cartridge.shop.forms import OrderForm
from cartridge.shop.utils import (make_choices, set_locale, set_shipping,
clear_session)
class WidgetNonameMixin(object):
# taken from Widget class
def build_attrs(self, extra_attrs=None, **kwargs):
"Helper function for building an attribute dictionary."
if 'name' in kwargs:
del kwargs['name']
attrs = dict(self.attrs, **kwargs)
if extra_attrs:
attrs.update(extra_attrs)
return attrs
class NamelessTextInput(WidgetNonameMixin,forms.TextInput):
pass
class NamelessSelect(WidgetNonameMixin,forms.widgets.Select):
pass
class StripeOrderForm(OrderForm):
"""
Derived order form that implements nameless cc and stripeToken inputs.
Derived form class for Order model.
"""
# "required" is a core Field argument, and is used by the Field.clean method.
# if required=False, then clean does not raise a ValdationError on empty values.
# clean raises this error or returns a cleaned value
card_number = forms.CharField(label=_("Card number"),required=False, widget = NamelessTextInput(attrs={"data-stripe":"number"}) )
card_expiry_month = forms.ChoiceField(label=_("Card expiry month"),
initial="%02d" % date.today().month,
choices=make_choices(["%02d" % i for i in range(1, 13)]),
required=False,
widget=NamelessSelect(attrs={"data-stripe":"exp-month"}))
card_expiry_year = forms.ChoiceField(label=_("Card expiry year"),
required=False,
widget=NamelessSelect(attrs={'data-stripe':'exp-year'})
)
card_ccv = forms.CharField(label=_("CCV"), help_text=_("A security code, "
"usually the last 3 digits found on the back of your card."), required=False, widget = NamelessTextInput(attrs={"data-stripe":"cvc"}))
stripeToken = forms.CharField(widget=forms.HiddenInput())
@classmethod
def preprocess(cls, data):
"""
A preprocessor for the order form data, insets stripeToken.
The default preprocessor that is called from super handles
copying billing fields to shipping fields if "same" checked. StripeToken defaults to None.
"""
data = super(StripeOrderForm,cls).preprocess(data)
# The stripeToken form field is required, hence fill it with something.
if ("stripeToken" in data) and (len(data["stripeToken"]) == 0):
data["stripeToken"] = "None"
return data