From b19905a02e33ebd9dd271c9653ed7430191037a7 Mon Sep 17 00:00:00 2001 From: Artur Smęt Date: Mon, 16 Jan 2017 14:31:08 +0100 Subject: Move choices generation to a method --- payments/fields.py | 23 ++++++++++++++--------- payments/stripe/forms.py | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) (limited to 'payments') 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 -- cgit v1.2.3