Code

Ticket #3961: za-localflavor.diff

File za-localflavor.diff, 5.3 KB (added by Russell Cloran <russell@…>, 7 years ago)

Patch to add za localflavor

Line 
1Index: django/utils/checksums.py
2===================================================================
3--- django/utils/checksums.py   (revision 0)
4+++ django/utils/checksums.py   (revision 0)
5@@ -0,0 +1,21 @@
6+"""Implementations of useful checksum algorithms"""
7+
8+def luhn(s):
9+    """A Luhn checksum is a simplistic checksum designed to catch typing
10+    errors. Takes a string of digits or an int.
11+
12+    This checksum is commonly used with credit card numbers.
13+
14+    http://en.wikipedia.org/wiki/Luhn_algorithm
15+    """
16+
17+    if isinstance(s, int):
18+        s = str(s)
19+
20+    if not (isinstance(s, basestring) and s.isdigit()):
21+        return False
22+
23+    lookup = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) # sum_of_digits(index * 2)
24+    evens = sum([int(c) for c in s[-1::-2]])
25+    odds = sum([lookup[int(c)] for c in s[-2::-2]])
26+    return ((evens + odds) % 10 == 0)
27Index: django/contrib/localflavor/za/__init__.py
28===================================================================
29Index: django/contrib/localflavor/za/za_provinces.py
30===================================================================
31--- django/contrib/localflavor/za/za_provinces.py       (revision 0)
32+++ django/contrib/localflavor/za/za_provinces.py       (revision 0)
33@@ -0,0 +1,13 @@
34+from django.utils.translation import gettext_lazy as _
35+
36+PROVINCE_CHOICES = (
37+    ('EC', _('Eastern Cape')),
38+    ('FS', _('Free State')),
39+    ('GP', _('Gauteng')),
40+    ('KN', _('KwaZulu-Natal')),
41+    ('LP', _('Limpopo')),
42+    ('MP', _('Mpumalanga')),
43+    ('NC', _('Northern Cape')),
44+    ('NW', _('North West')),
45+    ('WC', _('Western Cape')),
46+)
47Index: django/contrib/localflavor/za/forms.py
48===================================================================
49--- django/contrib/localflavor/za/forms.py      (revision 0)
50+++ django/contrib/localflavor/za/forms.py      (revision 0)
51@@ -0,0 +1,57 @@
52+"""
53+South Africa-specific Form helpers
54+"""
55+
56+from django.newforms import ValidationError
57+from django.newforms.fields import Field, RegexField, EMPTY_VALUES
58+from django.utils.checksums import luhn
59+from django.utils.translation import gettext as _
60+import re
61+from datetime import date
62+
63+id_re = re.compile(r'^(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<end>\d{3})')
64+
65+class ZAIDField(Field):
66+    """A form field for South African ID numbers -- the checksum is validated
67+    using the Luhn checksum, and uses a simlistic (read: not entirely accurate)
68+    check for the birthdate
69+    """
70+
71+    def __init__(self, *args, **kwargs):
72+        super(ZAIDField, self).__init__()
73+        self.error_message = _(u'Enter a valid South African ID number')
74+
75+    def clean(self, value):
76+        # strip spaces and dashes
77+        value = value.strip().replace(' ', '').replace('-', '')
78+
79+        super(ZAIDField, self).clean(value)
80+
81+        if value in EMPTY_VALUES:
82+            return u''
83+
84+        match = re.match(id_re, value)
85+       
86+        if not match:
87+            raise ValidationError(self.error_message)
88+
89+        g = match.groupdict()
90+
91+        try:
92+            # The year 2000 is conveniently a leapyear.
93+            # This algorithm will break in xx00 years which aren't leap years
94+            # There is no way to guess the century of a ZA ID number
95+            d = date(int(g['yy']) + 2000, int(g['mm']), int(g['dd']))
96+        except ValueError:
97+            raise ValidationError(self.error_message)
98+
99+        if not luhn(value):
100+            raise ValidationError(self.error_message)
101+
102+        return value
103+
104+class ZAPostCodeField(RegexField):
105+    def __init__(self, *args, **kwargs):
106+        super(ZAPostCodeField, self).__init__(r'^\d{4}$',
107+            max_length=None, min_length=None,
108+            error_message=_(u'Enter a valid South African postal code'))
109Index: tests/regressiontests/forms/localflavor.py
110===================================================================
111--- tests/regressiontests/forms/localflavor.py  (revision 4953)
112+++ tests/regressiontests/forms/localflavor.py  (working copy)
113@@ -882,4 +882,42 @@
114 Traceback (most recent call last):
115 ...
116 ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.']
117+
118+# ZAIDField #################################################################
119+
120+ZAIDField validates that the date is a valid birthdate and that the value
121+has a valid checksum. It allows spaces and dashes, and returns a plain
122+string of digits.
123+>>> from django.contrib.localflavor.za.forms import ZAIDField
124+>>> f = ZAIDField()
125+>>> f.clean('0002290001003')
126+'0002290001003'
127+>>> f.clean('000229 0001 003')
128+'0002290001003'
129+>>> f.clean('0102290001001')
130+Traceback (most recent call last):
131+...
132+ValidationError: [u'Enter a valid South African ID number']
133+>>> f.clean('811208')
134+Traceback (most recent call last):
135+...
136+ValidationError: [u'Enter a valid South African ID number']
137+>>> f.clean('0002290001004')
138+Traceback (most recent call last):
139+...
140+ValidationError: [u'Enter a valid South African ID number']
141+
142+# ZAPostCodeField ###########################################################
143+>>> from django.contrib.localflavor.za.forms import ZAPostCodeField
144+>>> f = ZAPostCodeField()
145+>>> f.clean('abcd')
146+Traceback (most recent call last):
147+...
148+ValidationError: [u'Enter a valid South African postal code']
149+>>> f.clean('0000')
150+u'0000'
151+>>> f.clean(' 7530')
152+Traceback (most recent call last):
153+...
154+ValidationError: [u'Enter a valid South African postal code']
155 """