diff options
Diffstat (limited to 'addons/phone_validation/tools/phone_validation.py')
| -rw-r--r-- | addons/phone_validation/tools/phone_validation.py | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/addons/phone_validation/tools/phone_validation.py b/addons/phone_validation/tools/phone_validation.py index e1df861ac8c..91ee9ba6b37 100644 --- a/addons/phone_validation/tools/phone_validation.py +++ b/addons/phone_validation/tools/phone_validation.py @@ -17,7 +17,7 @@ try: try: phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True) except phonenumbers.phonenumberutil.NumberParseException as e: - raise UserError(_('Unable to parse %s:\n%s') % (number, e)) + raise UserError(_('Unable to parse %s: %s') % (number, str(e))) if not phonenumbers.is_possible_number(phone_nbr): raise UserError(_('Impossible number %s: probably invalid number of digits') % number) @@ -77,45 +77,41 @@ except ImportError: def phone_sanitize_numbers(numbers, country_code, country_phone_code, force_format='E164'): + """ Given a list of numbers, return parsezd and sanitized information + + :return dict: {number: { + 'sanitized': sanitized and formated number or False (if cannot format) + 'code': 'empty' (number was a void string), 'invalid' (error) or False (sanitize ok) + 'msg': error message when 'invalid' + }} + """ + if not isinstance(numbers, (list)): + raise NotImplementedError() result = dict.fromkeys(numbers, False) for number in numbers: if not number: result[number] = {'sanitized': False, 'code': 'empty', 'msg': False} continue try: + stripped = number.strip() sanitized = phone_format( - number, country_code, country_phone_code, + stripped, country_code, country_phone_code, force_format=force_format, raise_exception=True) except Exception as e: - result[number] = {'sanitized': False, 'code': 'invalid', 'msg': e} + result[number] = {'sanitized': False, 'code': 'invalid', 'msg': str(e)} else: result[number] = {'sanitized': sanitized, 'code': False, 'msg': False} return result def phone_sanitize_numbers_w_record(numbers, record, country=False, record_country_fname='country_id', force_format='E164'): + if not isinstance(numbers, (list)): + raise NotImplementedError() if not country: - if record and hasattr(record, record_country_fname) and record[record_country_fname]: + if record and record_country_fname and hasattr(record, record_country_fname) and record[record_country_fname]: country = record[record_country_fname] elif record: country = record.env.company.country_id country_code = country.code if country else None country_phone_code = country.phone_code if country else None return phone_sanitize_numbers(numbers, country_code, country_phone_code, force_format=force_format) - - -def phone_sanitize_numbers_string_w_record(numbers_str, record, country=False, record_country_fname='country_id', force_format='E164'): - found_numbers = [number.strip() for number in numbers_str.split(',')] - return phone_sanitize_numbers_w_record(found_numbers, record, country=country, record_country_fname=record_country_fname, force_format=force_format) - - -def phone_get_sanitized_records_number(records, number_fname='mobile', country_fname='country_id', force_format='E164'): - res = dict.fromkeys(records.ids, False) - for record in records: - number = record[number_fname] - res[record.id] = phone_sanitize_numbers_w_record([number], records, record_country_fname=country_fname,force_format=force_format)[number]['sanitized'] - return res - - -def phone_get_sanitized_record_number(record, number_fname='mobile', country_fname='country_id', force_format='E164'): - return phone_get_sanitized_records_number(record, number_fname, country_fname, force_format=force_format)[record.id] |
