2018-01-16 06:58:15 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2018-01-16 11:34:37 +01:00
|
|
|
# Part of Odoo, Flectra. See LICENSE file for full copyright and licensing details.
|
2018-01-16 06:58:15 +01:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
2018-01-16 11:34:37 +01:00
|
|
|
from flectra import api, fields, models, _
|
|
|
|
from flectra.exceptions import UserError
|
|
|
|
from flectra.addons.iap.models import iap
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2018-01-15 15:21:30 +01:00
|
|
|
try:
|
|
|
|
import phonenumbers
|
|
|
|
_sms_phonenumbers_lib_imported = True
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
_sms_phonenumbers_lib_imported = False
|
2018-04-05 10:25:40 +02:00
|
|
|
_logger.info(
|
|
|
|
"The `phonenumbers` Python module is not available. "
|
|
|
|
"Phone number validation will be skipped. "
|
|
|
|
"Try `pip3 install phonenumbers` to install it."
|
2018-01-15 15:21:30 +01:00
|
|
|
)
|
|
|
|
|
2018-01-16 06:58:15 +01:00
|
|
|
|
|
|
|
class SendSMS(models.TransientModel):
|
|
|
|
_name = 'sms.send_sms'
|
|
|
|
|
|
|
|
recipients = fields.Char('Recipients', required=True)
|
|
|
|
message = fields.Text('Message', required=True)
|
|
|
|
|
2018-01-15 15:21:30 +01:00
|
|
|
def _phone_get_country(self, partner):
|
|
|
|
if 'country_id' in partner:
|
|
|
|
return partner.country_id
|
|
|
|
return self.env.user.company_id.country_id
|
|
|
|
|
|
|
|
def _sms_sanitization(self, partner, field_name):
|
|
|
|
number = partner[field_name]
|
2018-04-05 10:25:40 +02:00
|
|
|
if number and _sms_phonenumbers_lib_imported:
|
2018-01-15 15:21:30 +01:00
|
|
|
country = self._phone_get_country(partner)
|
|
|
|
country_code = country.code if country else None
|
|
|
|
try:
|
|
|
|
phone_nbr = phonenumbers.parse(number, region=country_code, keep_raw_input=True)
|
|
|
|
except phonenumbers.phonenumberutil.NumberParseException:
|
|
|
|
return number
|
|
|
|
if not phonenumbers.is_possible_number(phone_nbr) or not phonenumbers.is_valid_number(phone_nbr):
|
|
|
|
return number
|
|
|
|
phone_fmt = phonenumbers.PhoneNumberFormat.INTERNATIONAL
|
|
|
|
return phonenumbers.format_number(phone_nbr, phone_fmt).replace(' ', '')
|
|
|
|
else:
|
|
|
|
return number
|
|
|
|
|
2018-01-16 06:58:15 +01:00
|
|
|
def _get_records(self, model):
|
|
|
|
if self.env.context.get('active_domain'):
|
|
|
|
records = model.search(self.env.context.get('active_domain'))
|
|
|
|
elif self.env.context.get('active_ids'):
|
|
|
|
records = model.browse(self.env.context.get('active_ids', []))
|
|
|
|
else:
|
|
|
|
records = model.browse(self.env.context.get('active_id', []))
|
|
|
|
return records
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def default_get(self, fields):
|
|
|
|
result = super(SendSMS, self).default_get(fields)
|
|
|
|
|
|
|
|
active_model = self.env.context.get('active_model')
|
|
|
|
model = self.env[active_model]
|
|
|
|
|
|
|
|
records = self._get_records(model)
|
|
|
|
if getattr(records, '_get_default_sms_recipients'):
|
|
|
|
partners = records._get_default_sms_recipients()
|
|
|
|
phone_numbers = []
|
|
|
|
no_phone_partners = []
|
|
|
|
for partner in partners:
|
2018-04-05 10:25:40 +02:00
|
|
|
number = self._sms_sanitization(partner, self.env.context.get('field_name') or 'mobile')
|
2018-01-16 06:58:15 +01:00
|
|
|
if number:
|
|
|
|
phone_numbers.append(number)
|
|
|
|
else:
|
|
|
|
no_phone_partners.append(partner.name)
|
|
|
|
if len(partners) > 1:
|
|
|
|
if no_phone_partners:
|
|
|
|
raise UserError(_('Missing mobile number for %s.') % ', '.join(no_phone_partners))
|
|
|
|
result['recipients'] = ', '.join(phone_numbers)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def action_send_sms(self):
|
|
|
|
numbers = self.recipients.split(',')
|
|
|
|
|
|
|
|
active_model = self.env.context.get('active_model')
|
|
|
|
model = self.env[active_model]
|
|
|
|
records = self._get_records(model)
|
|
|
|
if getattr(records, 'message_post_send_sms'):
|
|
|
|
records.message_post_send_sms(self.message, numbers=numbers)
|
|
|
|
else:
|
|
|
|
self.env['sms.api']._send_sms(numbers, self.message)
|
|
|
|
return True
|