Ticket #11350: 11350.diff

File 11350.diff, 5.2 KB (added by Yuval Adam, 14 years ago)

Updated proper diff, with tests and docs

  • tests/regressiontests/forms/localflavor/il.py

    ### Eclipse Workspace Patch 1.0
    #P django-trunk
     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ IL Form Fields
     3
     4tests = r"""
     5# ILPostalCodeField #########################################################
     6
     7>>> from django.contrib.localflavor.il.forms import ILPostalCodeField
     8>>> f = ILPostalCodeField()
     9>>> f.clean('84545x')
     10Traceback (most recent call last):
     11...
     12ValidationError: [u'Enter a postal code in the format XXXXX']
     13>>> f.clean('69973')
     14u'69973'
     15>>> f.clean('699 73')
     16u'69973'
     17>>> f.clean('12345')
     18u'12345'
     19>>> f.clean('123456')
     20Traceback (most recent call last):
     21...
     22ValidationError: [u'Enter a postal code in the format XXXXX']
     23>>> f.clean('1234')
     24Traceback (most recent call last):
     25...
     26ValidationError: [u'Enter a postal code in the format XXXXX']
     27>>> f.clean('123 4')
     28Traceback (most recent call last):
     29...
     30ValidationError: [u'Enter a postal code in the format XXXXX']
     31
     32# ILIDNumberField ########################################################
     33
     34>>> from django.contrib.localflavor.il.forms import ILIDNumberField
     35>>> f = ILIDNumberField()
     36>>> f.clean('39337423')
     37u'39337423'
     38>>> f.clean('039337423')
     39u'039337423'
     40>>> f.clean('0091')
     41u'0091'
     42>>> f.clean('123465789')
     43Traceback (most recent call last):
     44...
     45ValidationError: [u'Enter a valid ID number.']
     46>>> f.clean('012346578')
     47Traceback (most recent call last):
     48...
     49ValidationError: [u'Enter a valid ID number.']
     50>>> f.clean('0001')
     51Traceback (most recent call last):
     52...
     53ValidationError: [u'Enter a valid ID number.']
     54"""
  • django/contrib/localflavor/il/forms.py

     
     1"""
     2Israeli-specific form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import RegexField, Field
     7from django.utils.translation import ugettext_lazy as _
     8import re
     9
     10id_number = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$')
     11
     12class ILPostalCodeField(RegexField):
     13    """
     14    A form field that validates its input as an Israeli postal code.
     15    Valid form is XXXXX where X represents integer.
     16    """
     17    default_error_messages = {
     18        'invalid': _(u'Enter a postal code in the format XXXXX'),
     19    }
     20
     21    def __init__(self, *args, **kwargs):
     22        super(ILPostalCodeField, self).__init__(r'^\d{5}$',
     23            max_length=None, min_length=None, *args, **kwargs)
     24
     25    def clean(self, value):
     26        """
     27        Validates the input and returns a string that contains only numbers.
     28        Returns an empty string for empty values.
     29        """
     30        return super(ILPostalCodeField, self).clean(value)
     31
     32class ILIDNumberField(Field):
     33    """
     34    A form field that validates its input as an Israeli identification number.
     35    Valid form is per the Israeli ID specification.
     36    """
     37    default_error_messages = {
     38        'invalid': _(u'Enter a valid ID number.'),
     39    }
     40
     41    def clean(self, value):
     42        super(ILIDNumberField, self).__init__(value)
     43
     44        if value in EMPTY_VALUES:
     45            return u''
     46
     47        match = re.match(id_number, value)
     48        if not match:
     49            raise ValidationError(self.error_messages['invalid'])
     50
     51        number, check = match.groupdict()['number'].zfill(8), int(match.groupdict()['check'])
     52
     53        sum = 0
     54        weight = 1
     55        for digit in number+str(check):
     56            sum += (lambda x: x/10 + x%10)(int(digit)*weight)
     57            weight ^= 3
     58               
     59        if sum%10 != 0:
     60            raise ValidationError(self.error_messages['invalid'])
  • docs/ref/contrib/localflavor.txt

     
    5252    * India_
    5353    * Indonesia_
    5454    * Ireland_
     55    * Israel_
    5556    * Italy_
    5657    * Japan_
    5758    * Kuwait_
     
    99100.. _India: `India (in\_)`_
    100101.. _Indonesia: `Indonesia (id)`_
    101102.. _Ireland: `Ireland (ie)`_
     103.. _Israel: `Israel (il)`_
    102104.. _Italy: `Italy (it)`_
    103105.. _Japan: `Japan (jp)`_
    104106.. _Kuwait: `Kuwait (kw)`_
     
    419421
    420422.. _NIK: http://en.wikipedia.org/wiki/Indonesian_identity_card
    421423
     424Israel (``il``)
     425==============
     426
     427.. class:: il.forms.ILPostalCodeField
     428
     429    A form field that validates its input as an Israeli postal code.
     430    Valid form is XXXXX where X represents integer.
     431
     432.. class:: il.forms.ILIDNumberField
     433
     434    A form field that validates its input as an Israeli identification number.
     435    Valid form is per the Israeli ID specification.
     436
    422437Italy (``it``)
    423438==============
    424439
Back to Top