diff --git a/fields/string.py b/fields/string.py index 4c34731..091575c 100644 --- a/fields/string.py +++ b/fields/string.py @@ -221,6 +221,23 @@ class String(Field): def MODULO_97_COMPLEMENT(obj, value): return String._MODULO_97(obj, value, True) 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 def IBAN(obj, value): '''Checks that p_value corresponds to a valid IBAN number. IBAN stands diff --git a/shared/utils.py b/shared/utils.py index 573c183..f71e5d5 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -227,12 +227,19 @@ def normalizeString(s, usage='fileName'): res = s return res -# ------------------------------------------------------------------------------ def normalizeText(s): '''Normalizes p_s: remove special chars, lowerizes it, etc, for indexing purposes.''' 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=' '): '''Returns a string representation of number p_n, which can be a float