Login

Custom DateField To Handle Credit Card Exp Date. Format: MM/YY

Author:
pjs
Posted:
June 27, 2008
Language:
Python
Version:
.96
Tags:
datefield credit-card expiration-date
Score:
1 (after 1 ratings)

As users would login to their accounts to update their CC info, the expiration date always threw them off. The default format for displaying a datetime.date object is

YYYY-MM-DD

Obviously the expiration date on your credit card uses the MM/YY format. I finally got around to creating a custom field/widget to handle this particular piece of data.

Use like so...

class CustomerForm(forms.ModelForm):
    cc_exp = DateFieldCCEXP()
    class Meta:
        model = Customer
 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
class TextInputCCEXP(forms.TextInput):
    def render(self, name, value, attrs=None):
        from django.newforms.util import flatatt
        from django.utils.encoding import force_unicode
        from django.utils.safestring import mark_safe

        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            if isinstance(value, datetime.datetime):
                value = value.date()

            if isinstance(value, datetime.date):
                final_attrs['value'] = force_unicode(value.strftime('%m/%y'))
            else:
                final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<input%s />' % flatatt(final_attrs))


class DateFieldCCEXP(forms.DateField):
    default_error_messages = {
        'invalid': u'Please use the format: MM/YY',
    }
    widget = TextInputCCEXP

    def clean(self, value):
        import re
        if value in (None, ''):
            raise forms.ValidationError(self.error_messages['required'])
        if isinstance(value, datetime.datetime):
            return value.date()
        if isinstance(value, datetime.date):
            return value

        if re.search('^\d{1,2}/\d{2}$', value):
            month, year = map(int, value.split('/'))
            return datetime.date((2000 + year), month, 1)

        raise forms.ValidationError(self.error_messages['invalid'])

More like this

  1. Credit Card With Newforms by MasonM 7 years ago
  2. Month / Year dropdown widget by gregb 5 years, 11 months ago
  3. Newforms Validation of Credit Card Numbers by humphreymurray 7 years, 2 months ago
  4. ActiveManager: filter objects depending on publication and/or expiration dates by haplo 7 years, 1 month ago
  5. custom DateField format widget by Gandalfar 8 years, 5 months ago

Comments

Please login first before commenting.