summaryrefslogtreecommitdiff
path: root/payments
diff options
context:
space:
mode:
authorArtur Smęt <artur.smet@mirumee.com>2017-01-16 14:31:08 +0100
committerMarcin Gębala <maarcin.gebala@gmail.com>2017-02-23 13:17:35 +0100
commitb19905a02e33ebd9dd271c9653ed7430191037a7 (patch)
treee8707e8d64f2934a584eac6ce6770bec225c2487 /payments
parente8fa9450397552c28b553ee04bae18e048fe1d3d (diff)
downloaddjango-payments-taler-b19905a02e33ebd9dd271c9653ed7430191037a7.tar.gz
django-payments-taler-b19905a02e33ebd9dd271c9653ed7430191037a7.tar.bz2
django-payments-taler-b19905a02e33ebd9dd271c9653ed7430191037a7.zip
Move choices generation to a method
Diffstat (limited to 'payments')
-rw-r--r--payments/fields.py23
-rw-r--r--payments/stripe/forms.py4
2 files changed, 16 insertions, 11 deletions
diff --git a/payments/fields.py b/payments/fields.py
index 9aa20ef..761365e 100644
--- a/payments/fields.py
+++ b/payments/fields.py
@@ -60,14 +60,8 @@ class CreditCardNumberField(forms.CharField):
return sum(digits) % 10 == 0 if digits else False
-class CreditCardExpiryField(forms.MultiValueField):
-
- EXP_MONTH = [(str(x), '%02d' % (x,)) for x in range(1, 13)]
- EXP_YEAR = [(str(x), str(x)) for x in range(date.today().year,
- date.today().year + 15)]
- EXP_MONTH_CHOICES = [('', _('Month'))] + EXP_MONTH
- EXP_YEAR_CHOICES = [('', _('Year'))] + EXP_YEAR
+class CreditCardExpiryField(forms.MultiValueField):
default_error_messages = {
'invalid_month': 'Enter a valid month.',
@@ -80,13 +74,13 @@ class CreditCardExpiryField(forms.MultiValueField):
fields = (
forms.ChoiceField(
- choices=self.EXP_MONTH_CHOICES,
+ choices=CreditCardExpiryField.get_month_choices(),
error_messages={'invalid': errors['invalid_month']},
widget=forms.Select(
attrs={'autocomplete': 'cc-exp-month',
'required': 'required'})),
forms.ChoiceField(
- choices=self.EXP_YEAR_CHOICES,
+ choices=CreditCardExpiryField.get_year_choices(),
error_messages={'invalid': errors['invalid_year']},
widget=forms.Select(
attrs={'autocomplete': 'cc-exp-year',
@@ -119,6 +113,17 @@ class CreditCardExpiryField(forms.MultiValueField):
return date(year, month, day)
return None
+ @classmethod
+ def get_month_choices(cls):
+ month_choices = [(str(x), '%02d' % (x,)) for x in range(1, 13)]
+ return [('', _('Month'))] + month_choices
+
+ @classmethod
+ def get_year_choices(cls):
+ year_choices = [(str(x), str(x)) for x in range(
+ date.today().year, date.today().year + 15)]
+ return [('', _('Year'))] + year_choices
+
class CreditCardVerificationField(forms.CharField):
diff --git a/payments/stripe/forms.py b/payments/stripe/forms.py
index 0ead8a4..14d631a 100644
--- a/payments/stripe/forms.py
+++ b/payments/stripe/forms.py
@@ -101,11 +101,11 @@ class PaymentForm(StripeFormMixin, CreditCardPaymentFormWithName):
SensitiveSelect(
attrs={'autocomplete': 'cc-exp-month',
'required': 'required'},
- choices=CreditCardExpiryField.EXP_MONTH_CHOICES),
+ choices=CreditCardExpiryField.get_year_choices()),
SensitiveSelect(
attrs={'autocomplete': 'cc-exp-year',
'required': 'required'},
- choices=CreditCardExpiryField.EXP_YEAR_CHOICES)])
+ choices=CreditCardExpiryField.get_year_choices())])
}
for field_name, widget in widget_map.items():
self.fields[field_name].widget = widget