Login

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

Author:
pjs
Posted:
June 27, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.