general: get local-region-code from hobo (#77972) #349

Merged
fpeters merged 1 commits from wip/77972-hobo-local-region-code into main 2023-05-30 10:21:44 +02:00
8 changed files with 31 additions and 9 deletions

View File

@ -2073,7 +2073,7 @@ def test_form_phone_prefill_phone_fr_validation(pub, nocache):
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'FR')
pub.site_options.set('options', 'local-region-code', 'FR')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
formdef = create_formdef()
@ -2095,7 +2095,7 @@ def test_form_phone_prefill_phone_validation(pub, nocache):
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'BE')
pub.site_options.set('options', 'local-region-code', 'BE')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
formdef = create_formdef()

View File

@ -4778,7 +4778,7 @@ def test_fts_phone(pub):
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'BE')
pub.site_options.set('options', 'local-region-code', 'BE')
assert formdef.data_class().count([FtsMatch('023456789')]) == 1

View File

@ -299,6 +299,17 @@ def test_configure_site_options(setuptest, alt_tempdir):
pub.load_site_options()
assert pub.get_site_option('xxx', 'variables') is None
# check phone region code
service['variables']['local_country_code'] = '32'
hobo_cmd.configure_site_options(service, pub, ignore_timestamp=True)
pub.load_site_options()
assert pub.get_site_option('local-region-code') == 'BE'
service['variables']['local_country_code'] = '33'
hobo_cmd.configure_site_options(service, pub, ignore_timestamp=True)
pub.load_site_options()
assert pub.get_site_option('local-region-code') == 'FR'
def test_update_configuration(setuptest):
pub, hobo_cmd = setuptest

View File

@ -94,7 +94,7 @@ def test_prefill_user_phone_fr_validation(user):
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'FR')
pub.site_options.set('options', 'local-region-code', 'FR')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
field = fields.Field()
@ -107,7 +107,7 @@ def test_prefill_user_phone_validation(user):
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'BE')
pub.site_options.set('options', 'local-region-code', 'BE')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)
field = fields.Field()

View File

@ -965,7 +965,7 @@ def test_wcsextrastringwidget_mobile_local():
pub.load_site_options()
if not pub.site_options.has_section('options'):
pub.site_options.add_section('options')
pub.site_options.set('options', 'default-country-code', 'BE')
pub.site_options.set('options', 'local-region-code', 'BE')
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
pub.site_options.write(fd)

View File

@ -23,6 +23,7 @@ import sys
import tempfile
import urllib.parse
import phonenumbers
from django.utils.encoding import force_bytes
from quixote import cleanup
@ -530,6 +531,10 @@ class CmdCheckHobos(Command):
value = force_str(value)
config.set('variables', key, value)
if variables.get('local_country_code'):
region_code = phonenumbers.region_code_for_country_code(int(variables.get('local_country_code')))
config.set('options', 'local-region-code', region_code)
if 'api-secrets' not in config.sections():
config.add_section('api-secrets')
if 'wscall-secrets' not in config.sections():

View File

@ -227,6 +227,12 @@ class WcsPublisher(QommonPublisher):
def get_enabled_languages(self):
return self.cfg.get('language', {}).get('languages') or []
def get_phone_local_region_code(self):
# default-country-code is a legacy setting (despite its name it contained a region code)
return (
self.get_site_option('local-region-code') or self.get_site_option('default-country-code') or 'FR'
)
def import_zip(self, fd):
results = {
'formdefs': 0,

View File

@ -855,7 +855,7 @@ def validate_phone_fr(string_value):
def validate_mobile_phone_local(string_value):
region_code = get_publisher().get_site_option('default-country-code') or 'FR'
region_code = get_publisher().get_phone_local_region_code()
country_codes = []
if region_code == 'FR':
country_codes = [33, 262, 508, 590, 594, 596]
@ -879,7 +879,7 @@ def validate_mobile_phone_local(string_value):
def get_formatted_phone(number, country_code=None):
if not country_code:
country_code = get_publisher().get_site_option('default-country-code') or 'FR'
country_code = get_publisher().get_phone_local_region_code()
try:
pn = phonenumbers.parse(number)
except phonenumbers.NumberParseException:
@ -895,7 +895,7 @@ def get_formatted_phone(number, country_code=None):
def normalize_phone_number_for_fts(value):
country_code = get_publisher().get_site_option('default-country-code') or 'FR'
country_code = get_publisher().get_phone_local_region_code()
try:
pn = phonenumbers.parse(value)
except phonenumbers.NumberParseException: