[gen] Added a validator for String fields, for validating Belgian NISS numbers.

This commit is contained in:
Gaetan Delannay 2013-07-10 18:05:06 +02:00
parent 369e41b43c
commit 05292356e7
2 changed files with 25 additions and 1 deletions

View file

@ -221,6 +221,23 @@ class String(Field):
def MODULO_97_COMPLEMENT(obj, value): def MODULO_97_COMPLEMENT(obj, value):
return String._MODULO_97(obj, value, True) return String._MODULO_97(obj, value, True)
BELGIAN_ENTERPRISE_NUMBER = MODULO_97_COMPLEMENT BELGIAN_ENTERPRISE_NUMBER = MODULO_97_COMPLEMENT
@staticmethod
def BELGIAN_NISS(obj, value):
'''Returns True if the NISS in p_value is valid.'''
if not value: return True
# Remove any non-digit from nrn
niss = sutils.keepDigits(value)
# NISS must be made of 11 numbers
if len(niss) != 11: return False
# When NRN begins with 0 or 1, it must be prefixed with number "2" for
# checking the modulo 97 complement.
nissForModulo = niss
if niss.startswith('0') or niss.startswith('1'):
nissForModulo = '2'+niss
# Check modulo 97 complement
return String.MODULO_97_COMPLEMENT(None, nissForModulo)
@staticmethod @staticmethod
def IBAN(obj, value): def IBAN(obj, value):
'''Checks that p_value corresponds to a valid IBAN number. IBAN stands '''Checks that p_value corresponds to a valid IBAN number. IBAN stands

View file

@ -227,12 +227,19 @@ def normalizeString(s, usage='fileName'):
res = s res = s
return res return res
# ------------------------------------------------------------------------------
def normalizeText(s): def normalizeText(s):
'''Normalizes p_s: remove special chars, lowerizes it, etc, for indexing '''Normalizes p_s: remove special chars, lowerizes it, etc, for indexing
purposes.''' purposes.'''
return normalizeString(s, usage='extractedText').strip().lower() return normalizeString(s, usage='extractedText').strip().lower()
def keepDigits(s):
'''Returns string p_s whose non-number chars have been removed.'''
if s is None: return s
res = ''
for c in s:
if c.isdigit(): res += c
return res
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
def formatNumber(n, sep=',', precision=2, tsep=' '): def formatNumber(n, sep=',', precision=2, tsep=' '):
'''Returns a string representation of number p_n, which can be a float '''Returns a string representation of number p_n, which can be a float