0007-qommon-misc-extend-validate_phone_fr-validation-scop.patch
tests/test_misc.py | ||
---|---|---|
29 | 29 |
normalize_geolocation, |
30 | 30 |
parse_isotime, |
31 | 31 |
simplify, |
32 |
validate_phone_fr, |
|
32 | 33 |
) |
33 | 34 |
from wcs.scripts import Script |
34 | 35 |
from wcs.workflows import Workflow |
... | ... | |
629 | 630 |
'GET', 'https://new.example.com/', cert=None, data=None, headers={}, proxies=None, timeout=28 |
630 | 631 |
) |
631 | 632 |
mock_request.reset_mock() |
633 | ||
634 | ||
635 |
def test_validate_phone_fr(): |
|
636 |
valid = [ |
|
637 |
'0123456789', |
|
638 |
'+33123456789', |
|
639 |
'+590690000102', |
|
640 |
] |
|
641 |
invalid = [ |
|
642 |
'1234559', |
|
643 |
'+32123456789', |
|
644 |
'01+23+45+67+89', |
|
645 |
] |
|
646 | ||
647 |
assert all(validate_phone_fr(pn) for pn in valid) |
|
648 |
assert all(not validate_phone_fr(pn) for pn in invalid) |
wcs/qommon/misc.py | ||
---|---|---|
31 | 31 |
import xml.etree.ElementTree as ET |
32 | 32 |
from contextlib import contextmanager |
33 | 33 | |
34 |
import phonenumbers |
|
34 | 35 |
import requests |
35 | 36 |
from requests.adapters import HTTPAdapter |
36 | 37 | |
... | ... | |
824 | 825 | |
825 | 826 | |
826 | 827 |
def validate_phone_fr(string_value): |
827 |
if not re.match(r'^0[\d\.\s]+$', string_value): |
|
828 |
# leading zero, then digits, dots, or spaces |
|
828 |
french_country_codes = [33, 262, 508, 590, 594, 596] |
|
829 |
pn = None |
|
830 |
try: |
|
831 |
pn = phonenumbers.parse(string_value, 'FR') |
|
832 |
except phonenumbers.NumberParseException: |
|
829 | 833 |
return False |
830 |
return len([x for x in string_value if is_ascii_digit(x)]) == 10
|
|
834 |
return bool(phonenumbers.is_valid_number(pn) and pn.country_code in french_country_codes)
|
|
831 | 835 | |
832 | 836 | |
833 | 837 |
def validate_siren(string_value): |
834 |
- |