# Ticket #3961: za-localflavor.diff

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

• ## django/utils/checksums.py

 """Implementations of useful checksum algorithms""" def luhn(s): """A Luhn checksum is a simplistic checksum designed to catch typing errors. Takes a string of digits or an int. This checksum is commonly used with credit card numbers. http://en.wikipedia.org/wiki/Luhn_algorithm """ if isinstance(s, int): s = str(s) if not (isinstance(s, basestring) and s.isdigit()): return False lookup = (0, 2, 4, 6, 8, 1, 3, 5, 7, 9) # sum_of_digits(index * 2) evens = sum([int(c) for c in s[-1::-2]]) odds = sum([lookup[int(c)] for c in s[-2::-2]]) return ((evens + odds) % 10 == 0)
• ## django/contrib/localflavor/za/za_provinces.py

 from django.utils.translation import gettext_lazy as _ PROVINCE_CHOICES = ( ('EC', _('Eastern Cape')), ('FS', _('Free State')), ('GP', _('Gauteng')), ('KN', _('KwaZulu-Natal')), ('LP', _('Limpopo')), ('MP', _('Mpumalanga')), ('NC', _('Northern Cape')), ('NW', _('North West')), ('WC', _('Western Cape')), )
• ## django/contrib/localflavor/za/forms.py

 """ South Africa-specific Form helpers """ from django.newforms import ValidationError from django.newforms.fields import Field, RegexField, EMPTY_VALUES from django.utils.checksums import luhn from django.utils.translation import gettext as _ import re from datetime import date id_re = re.compile(r'^(?P\d\d)(?P\d\d)(?P
\d\d)(?P\d{4})(?P\d{3})') class ZAIDField(Field): """A form field for South African ID numbers -- the checksum is validated using the Luhn checksum, and uses a simlistic (read: not entirely accurate) check for the birthdate """ def __init__(self, *args, **kwargs): super(ZAIDField, self).__init__() self.error_message = _(u'Enter a valid South African ID number') def clean(self, value): # strip spaces and dashes value = value.strip().replace(' ', '').replace('-', '') super(ZAIDField, self).clean(value) if value in EMPTY_VALUES: return u'' match = re.match(id_re, value) if not match: raise ValidationError(self.error_message) g = match.groupdict() try: # The year 2000 is conveniently a leapyear. # This algorithm will break in xx00 years which aren't leap years # There is no way to guess the century of a ZA ID number d = date(int(g['yy']) + 2000, int(g['mm']), int(g['dd'])) except ValueError: raise ValidationError(self.error_message) if not luhn(value): raise ValidationError(self.error_message) return value class ZAPostCodeField(RegexField): def __init__(self, *args, **kwargs): super(ZAPostCodeField, self).__init__(r'^\d{4}\$', max_length=None, min_length=None, error_message=_(u'Enter a valid South African postal code'))
• ## tests/regressiontests/forms/localflavor.py

 Traceback (most recent call last): ... ValidationError: [u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.'] # ZAIDField ################################################################# ZAIDField validates that the date is a valid birthdate and that the value has a valid checksum. It allows spaces and dashes, and returns a plain string of digits. >>> from django.contrib.localflavor.za.forms import ZAIDField >>> f = ZAIDField() >>> f.clean('0002290001003') '0002290001003' >>> f.clean('000229 0001 003') '0002290001003' >>> f.clean('0102290001001') Traceback (most recent call last): ... ValidationError: [u'Enter a valid South African ID number'] >>> f.clean('811208') Traceback (most recent call last): ... ValidationError: [u'Enter a valid South African ID number'] >>> f.clean('0002290001004') Traceback (most recent call last): ... ValidationError: [u'Enter a valid South African ID number'] # ZAPostCodeField ########################################################### >>> from django.contrib.localflavor.za.forms import ZAPostCodeField >>> f = ZAPostCodeField() >>> f.clean('abcd') Traceback (most recent call last): ... ValidationError: [u'Enter a valid South African postal code'] >>> f.clean('0000') u'0000' >>> f.clean(' 7530') Traceback (most recent call last): ... ValidationError: [u'Enter a valid South African postal code'] """