summaryrefslogtreecommitdiff
path: root/addons/phone_validation/tools
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2019-07-19 09:45:24 +0000
committerThibault Delavallée <tde@odoo.com>2019-08-12 12:00:22 +0000
commit7e8a02cd6e9f08e5ff45db804b357bc3537ff1f6 (patch)
tree1494948bab19baf7ef1fb5007cb5f327b6f007c0 /addons/phone_validation/tools
parent22b598bc1b8331907ba0ce77739b64c48d6fcbbd (diff)
[IMP] phone_validation: add blacklist mechanism
PURPOSE SMS are a powerful marketing tool. For instance it is perfect to announce a sale or to communicate a coupon code, to welcome a new customer in a fidelity program, ... Purpose of this task is to integrate SMS sending in batch in mass mailing. It will use same mailing objects but sending SMS instead of emails. Some metrics and flows will have to be slightly updated at the same time. SPECIFICATIONS Purpose of this commit is to add a blacklist mechanism for phone numbers used to send SMS like what already exists for email addresses when sending emails. Define a new phone.blacklist model, holding a number and the state of the blacklist (active field), as well as tools methods to access it. Make it as private as possible, accessing it in sudo once access are granted. Also clean phone validation tools: lessen number of tool functions and update caller to simplify code readability. Some fixes are also included in this commit, notably blank spaces cleaning in phone numbers. Improve phone.validation.mixin to add a tool method computing a sanitized number, in addition to formatting it to national / international. Define a new mail.thread.phone mixin computing the blacklist status of a record. This mixin * inherit from phone.validation.mixin in order to have access to some base phone number parsing capabilities; * computes a sanitized phone number based on ´´_phone_get_number_fields´´. It takes first sanitized value, trying each field returned by the method. That means one sanitized phone number is available per record even if several fields are available; * compute blacklist state of records. It is based on phone.blacklist model and give an easy-to-use field and API to manipulate blacklisted records; * give some API methods : * ``_phone_set_blacklisted``: set recordset as blacklisted; * ``_phone_reset_blacklisted``: reactivate recordset (even if not blacklisted this method can be called safely); Put menus in technical in order to have access to it. Add a Phone / SMS menu below "Email" and use it to store SMS / Phone actions. Finally prepare tests addition by performing some light cleaning while adding blacklist tests. Purpose is to ease future tests related to SMS. LINKS Task 1997464 PR #34424 Original SMS addition: Task 1922163 (4287481)
Diffstat (limited to 'addons/phone_validation/tools')
-rw-r--r--addons/phone_validation/tools/phone_validation.py38
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]