Projet

Général

Profil

0008-qommon-misc-add-e164-format-phone-representation-uti.patch

Paul Marillonnet, 02 novembre 2022 10:45

Télécharger (1,89 ko)

Voir les différences:

Subject: [PATCH 08/13] qommon/misc: add e164 format phone representation
 utility (#69838)

 tests/test_misc.py |  7 +++++++
 wcs/qommon/misc.py | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)
tests/test_misc.py
29 29
    normalize_geolocation,
30 30
    parse_isotime,
31 31
    simplify,
32
    try_e164_format,
32 33
    validate_phone_fr,
33 34
)
34 35
from wcs.scripts import Script
......
646 647

  
647 648
    assert all(validate_phone_fr(pn) for pn in valid)
648 649
    assert all(not validate_phone_fr(pn) for pn in invalid)
650

  
651

  
652
def test_try_e164_format():
653
    assert try_e164_format('0123456789') == '+33123456789'
654
    assert try_e164_format('+33123456789') == '+33123456789'
655
    assert try_e164_format('1234559') == '1234559'
wcs/qommon/misc.py
1249 1249
        formdef_slug = match[15:]
1250 1250
        formdef_slug = formdef_slug[:-1]
1251 1251
        yield FormDef.get_by_slug(formdef_slug, ignore_errors=True)
1252

  
1253

  
1254
def try_e164_format(phone):
1255
    default_country_code = settings.DEFAULT_COUNTRY_CODE
1256
    pn = None
1257
    try:
1258
        pn = phonenumbers.parse(phone)
1259
    except phonenumbers.NumberParseException:
1260
        try:
1261
            pn = phonenumbers.parse(phone, default_country_code)
1262
        except phonenumbers.NumberParseException:
1263
            return phone
1264
    if phonenumbers.is_valid_number(pn):
1265
        return phonenumbers.format_number(pn, phonenumbers.PhoneNumberFormat.E164)
1266
    else:
1267
        return phone
1252
-