Django

Code

Ticket #3961: ticket_3961__rev_6223.diff

File ticket_3961__rev_6223.diff, 4.6 kB (added by __hawkeye__, 10 months ago)

Removed Luhn algorithm implementation, now covered separately in #5475

  • django/contrib/localflavor/za/za_provinces.py

    old new  
     1from django.utils.translation import gettext_lazy as _ 
     2 
     3PROVINCE_CHOICES = ( 
     4    ('EC', _('Eastern Cape')), 
     5    ('FS', _('Free State')), 
     6    ('GP', _('Gauteng')), 
     7    ('KN', _('KwaZulu-Natal')), 
     8    ('LP', _('Limpopo')), 
     9    ('MP', _('Mpumalanga')), 
     10    ('NC', _('Northern Cape')), 
     11    ('NW', _('North West')), 
     12    ('WC', _('Western Cape')), 
     13) 
  • django/contrib/localflavor/za/forms.py

    old new  
     1""" 
     2South Africa-specific Form helpers 
     3""" 
     4 
     5from django.newforms import ValidationError 
     6from django.newforms.fields import Field, RegexField, EMPTY_VALUES 
     7from django.utils.checksums import luhn 
     8from django.utils.translation import gettext as _ 
     9import re 
     10from datetime import date 
     11 
     12id_re = re.compile(r'^(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<end>\d{3})') 
     13 
     14class ZAIDField(Field): 
     15    """A form field for South African ID numbers -- the checksum is validated 
     16    using the Luhn checksum, and uses a simlistic (read: not entirely accurate) 
     17    check for the birthdate 
     18    """ 
     19 
     20    def __init__(self, *args, **kwargs): 
     21        super(ZAIDField, self).__init__() 
     22        self.error_message = _(u'Enter a valid South African ID number') 
     23 
     24    def clean(self, value): 
     25        # strip spaces and dashes 
     26        value = value.strip().replace(' ', '').replace('-', '') 
     27 
     28        super(ZAIDField, self).clean(value) 
     29 
     30        if value in EMPTY_VALUES: 
     31            return u'' 
     32 
     33        match = re.match(id_re, value) 
     34         
     35        if not match: 
     36            raise ValidationError(self.error_message) 
     37 
     38        g = match.groupdict() 
     39 
     40        try: 
     41            # The year 2000 is conveniently a leapyear. 
     42            # This algorithm will break in xx00 years which aren't leap years 
     43            # There is no way to guess the century of a ZA ID number 
     44            d = date(int(g['yy']) + 2000, int(g['mm']), int(g['dd'])) 
     45        except ValueError: 
     46            raise ValidationError(self.error_message) 
     47 
     48        if not luhn(value): 
     49            raise ValidationError(self.error_message) 
     50 
     51        return value 
     52 
     53class ZAPostCodeField(RegexField): 
     54    def __init__(self, *args, **kwargs): 
     55        super(ZAPostCodeField, self).__init__(r'^\d{4}$', 
     56            max_length=None, min_length=None, 
     57            error_message=_(u'Enter a valid South African postal code')) 
  • tests/regressiontests/forms/localflavor.py

    old new  
    20362036... 
    20372037ValidationError: [u'Enter a valid Canadian Social Insurance number in XXX-XXX-XXXX format.'] 
    20382038 
     2039# ZAIDField ################################################################# 
     2040 
     2041ZAIDField validates that the date is a valid birthdate and that the value 
     2042has a valid checksum. It allows spaces and dashes, and returns a plain  
     2043string of digits. 
     2044>>> from django.contrib.localflavor.za.forms import ZAIDField 
     2045>>> f = ZAIDField() 
     2046>>> f.clean('0002290001003') 
     2047'0002290001003' 
     2048>>> f.clean('000229 0001 003') 
     2049'0002290001003' 
     2050>>> f.clean('0102290001001') 
     2051Traceback (most recent call last): 
     2052... 
     2053ValidationError: [u'Enter a valid South African ID number'] 
     2054>>> f.clean('811208') 
     2055Traceback (most recent call last): 
     2056... 
     2057ValidationError: [u'Enter a valid South African ID number'] 
     2058>>> f.clean('0002290001004') 
     2059Traceback (most recent call last): 
     2060... 
     2061ValidationError: [u'Enter a valid South African ID number'] 
     2062 
     2063# ZAPostCodeField ########################################################### 
     2064>>> from django.contrib.localflavor.za.forms import ZAPostCodeField 
     2065>>> f = ZAPostCodeField() 
     2066>>> f.clean('abcd') 
     2067Traceback (most recent call last): 
     2068... 
     2069ValidationError: [u'Enter a valid South African postal code'] 
     2070>>> f.clean('0000') 
     2071u'0000' 
     2072>>> f.clean(' 7530') 
     2073Traceback (most recent call last): 
     2074... 
     2075ValidationError: [u'Enter a valid South African postal code'] 
     2076 
    20392077## Generic DateField ########################################################## 
    20402078 
    20412079>>> from django.contrib.localflavor.generic.forms import *