diff --git a/smsclient_core/models/serveraction.py b/smsclient_core/models/serveraction.py index 2235057..ce31e16 100644 --- a/smsclient_core/models/serveraction.py +++ b/smsclient_core/models/serveraction.py @@ -28,5 +28,5 @@ class ServerAction(models.Model): comodel_name='sms.gateway', string='SMS Server', help='Select the SMS Gateway configuration to use with this action.') sms_template_id = fields.Many2one( - comodel_name='email.template', string='SMS Template', + comodel_name='mail.template', string='SMS Template', help='Select the SMS Template configuration to use with this action.') diff --git a/smsclient_core/models/sms_gateway.py b/smsclient_core/models/sms_gateway.py index 4151fb4..3d226db 100644 --- a/smsclient_core/models/sms_gateway.py +++ b/smsclient_core/models/sms_gateway.py @@ -8,7 +8,7 @@ import logging from odoo import models, fields, api, _ -from functools import wraps +from odoo.exceptions import UserError _logger = logging.getLogger(__name__) @@ -26,6 +26,7 @@ CLASSES_LIST = [ ('3', 'Toolkit') ] + class SMSClient(models.Model): _name = 'sms.gateway' _description = 'SMS Client' @@ -34,41 +35,13 @@ class SMSClient(models.Model): def get_method(self): return [] - @api.multi - def _provider_get_provider_conf(self): - for rec in self: - keychain = rec.env['keychain.account'] - if rec._check_permissions: - retrieve = keychain.suspend_security().retrieve - else: - retrieve = keychain.retrieve - accounts = retrieve( - [['namespace', '=', 'SMS_Gateway%s' % rec.provider_type]]) - - return accounts[0] - - # @api.multi - # def _get_provider_conf(self): - # for sms_provider in self: - # global_section_name = 'sms_provider' - # config_vals = {} - # if serv_config.has_section(global_section_name): - # config_vals.update(serv_config.items(global_section_name)) - # custom_section_name = '.'.join((global_section_name, - # sms_provider.name)) - # if serv_config.has_section(custom_section_name): - # config_vals.update(serv_config.items(custom_section_name)) - # for key in config_vals: - # sms_provider[key] = config_vals[key] - name = fields.Char(string='Gateway Name', required=True) - url = fields.Char( - string='Gateway URL', compute='_get_provider_conf', - help='Base url for message') - url_visible = fields.Boolean(default=False) + from_provider = fields.Char(string="From") method = fields.Selection( string='API Method', selection='get_method') + url = fields.Char( + string='Gateway URL', help='Base url for message') state = fields.Selection(selection=[ ('new', 'Not Verified'), ('waiting', 'Waiting for Verification'), @@ -77,16 +50,7 @@ class SMSClient(models.Model): user_ids = fields.Many2many( comodel_name='res.users', string='Users Allowed') - sms_account = fields.Char(compute='_get_provider_conf') - sms_account_visible = fields.Boolean(default=False) - login_provider = fields.Char(compute='_get_provider_conf') - login_provider_visible = fields.Boolean(default=False) - password_provider = fields.Char(compute='_get_provider_conf') - password_provider_visible = fields.Boolean(default=False) - from_provider = fields.Char(compute='_get_provider_conf') - from_provider_visible = fields.Boolean(default=False) code = fields.Char('Verification Code') - code_visible = fields.Boolean(default=False) body = fields.Text( string='Message', help="The message text that will be send along with the" @@ -95,12 +59,10 @@ class SMSClient(models.Model): default=10, help="The maximum time - in minute(s) - before the message " "is dropped.") - validity_visible = fields.Boolean(default=False) classes = fields.Selection( selection=CLASSES_LIST, string='Class', default='1', help='The SMS class: flash(0),phone display(1),SIM(2),toolkit(3)') - classes_visible = fields.Boolean(default=False) deferred = fields.Integer( default=0, help='The time -in minute(s)- to wait before sending the message.') @@ -108,7 +70,6 @@ class SMSClient(models.Model): priority = fields.Selection( selection=PRIORITY_LIST, string='Priority', default='3', help='The priority of the message') - priority_visible = fields.Boolean(default=False) coding = fields.Selection(selection=[ ('1', '7 bit'), ('2', 'Unicode') @@ -118,36 +79,15 @@ class SMSClient(models.Model): 'lenght)', default='1' ) - coding_visible = fields.Boolean(default=False) tag = fields.Char('Tag', help='an optional tag') - tag_visible = fields.Boolean(default=False) nostop = fields.Boolean( default=True, help='Do not display STOP clause in the message, this requires that ' 'this is not an advertising message.') - nostop_visible = fields.Boolean(default=False) char_limit = fields.Boolean('Character Limit', default=True) - char_limit_visible = fields.Boolean(default=False) default_gateway = fields.Boolean(default=False) company_id = fields.Many2one(comodel_name='res.company') - @api.onchange('method') - def onchange_method(self): - if not self.method: - self.url_visible = False - self.sms_account_visible = False - self.login_provider_visible = False - self.password_provider_visible = False - self.from_provider_visible = False - self.validity_visible = False - self.classes_visible = False - self.deferred_visible = False - self.nostop_visible = False - self.priority_visible = False - self.coding_visible = False - self.tag_visible = False - self.char_limit_visible = False - @api.multi def _check_permissions(self): self.ensure_one() @@ -189,7 +129,6 @@ class SmsSms(models.Model): ('cancel', 'Cancel'), ('error', 'Error'), ], string='Message Status', - select=True, readonly=True, default='draft') error = fields.Text( @@ -243,6 +182,11 @@ class SmsSms(models.Model): readonly=True, states={'draft': [('readonly', False)]}) + @api.model + def _convert_to_e164(self, erp_number): + to_dial_number = erp_number.replace(u'\xa0', u' ') + return to_dial_number + @api.onchange('partner_id') def onchange_partner_id(self): self.mobile = self.partner_id.mobile @@ -250,13 +194,19 @@ class SmsSms(models.Model): @api.multi def send(self): for sms in self: + if not sms.gateway_id._check_permissions(): + sms.write( + {'error': 'no permission on gateway', 'state': 'error'}) + sms._cr.commit() + return False if sms.gateway_id.char_limit and len(sms.message) > 160: sms.write({ 'state': 'error', 'error': _('Size of SMS should not be more then 160 char'), }) if not hasattr(sms, "_send_%s" % sms.gateway_id.method): - raise NotImplemented + #may not exist the gateway + raise UserError(_("No method gateway selected")) else: try: with sms._cr.savepoint(): diff --git a/smsclient_core/models/smstemplate.py b/smsclient_core/models/smstemplate.py index c0eeb21..09eecbe 100644 --- a/smsclient_core/models/smstemplate.py +++ b/smsclient_core/models/smstemplate.py @@ -8,7 +8,7 @@ from odoo import models, fields -class email_template(models.Model): +class mail_template(models.Model): _inherit = "mail.template" sms_template = fields.Boolean('SMS Template') diff --git a/smsclient_core/views/sms_gateway_view.xml b/smsclient_core/views/sms_gateway_view.xml index ef2d246..53a6369 100644 --- a/smsclient_core/views/sms_gateway_view.xml +++ b/smsclient_core/views/sms_gateway_view.xml @@ -19,17 +19,9 @@ - - - - - - - - - - + + @@ -40,24 +32,16 @@ - - - - - - - - + + + + - - - - - - - - + + + + @@ -74,7 +58,6 @@ - diff --git a/smsclient_core/wizard/mass_sms.py b/smsclient_core/wizard/mass_sms.py index 51d6de0..1d60efd 100644 --- a/smsclient_core/wizard/mass_sms.py +++ b/smsclient_core/wizard/mass_sms.py @@ -1,23 +1,8 @@ -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2009 Tiny SPRL () -# Copyright (C) 2013 Julius Network Solutions SARL -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see -# -############################################################################## +# coding: utf-8 +# Copyright (C) 2004-2009 Tiny SPRL (). +# Copyright (C) 2013 Julius Network Solutions SARL +# Copyright (C) 2015 Valentin Chemiere +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import models, fields, api diff --git a/smsclient_ovh/README.rst b/smsclient_ovh/README.rst index 70c5cb6..7f9cbd3 100644 --- a/smsclient_ovh/README.rst +++ b/smsclient_ovh/README.rst @@ -1,8 +1,9 @@ .. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg :alt: License: AGPL-3 +=============== SMS client OVH -=========== +=============== This module provide OVH fields to create OVH gateway. @@ -11,16 +12,13 @@ Usage To use this module, you need to: -* go to gateway configuration and select OVH http in method +* go to settings > technical > telephony >gateway configuration and select OVH http in method +* go to settings > technical > keychain and set your credentials For further information, please visit: * https://www.odoo.com/forum/help-1 -Known issues / Roadmap -====================== - -* ... Bug Tracker =========== @@ -37,8 +35,9 @@ Credits Contributors ------------ -* Valentin Chemiere +* Valentin Chemiere * Yvan Party +* MonsieurB Maintainer ---------- diff --git a/smsclient_ovh/__init__.py b/smsclient_ovh/__init__.py index f805f8f..06cfccb 100644 --- a/smsclient_ovh/__init__.py +++ b/smsclient_ovh/__init__.py @@ -1,23 +1,6 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2009 Tiny SPRL (). -# Copyright (C) 2013 Julius Network Solutions SARL -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## +# coding: utf-8 +# Copyright (C) 2015 Sébastien BEAU +# Valentin CHEMIERE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). -from . import sms_gateway +from . import models diff --git a/smsclient_ovh/__manifest__.py b/smsclient_ovh/__manifest__.py new file mode 100644 index 0000000..51278ea --- /dev/null +++ b/smsclient_ovh/__manifest__.py @@ -0,0 +1,32 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2013 Julius Network Solutions SARL +# Copyright (C) 2015 Sébastien BEAU + +{ + 'name': 'SMS Client OVH', + 'version': '10.0.1.0.0', + 'depends': ['base', + 'mail', + 'smsclient_core', + 'base_suspend_security', + 'keychain', + ], + 'author': 'Julius Network Solutions,SYLEAM,OpenERP SA,' + 'Odoo Community Association (OCA),Akretion', + 'images': [ + 'images/sms.jpeg', + 'images/gateway.jpeg', + 'images/gateway_access.jpeg', + 'images/client.jpeg', + 'images/send_sms.jpeg' + ], + 'website': 'http://julius.fr', + 'category': 'Tools', + 'demo': [], + 'data': [ + ], + 'active': False, + 'installable': True, +} + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/smsclient_ovh/__openerp__.py b/smsclient_ovh/__openerp__.py deleted file mode 100644 index f812b64..0000000 --- a/smsclient_ovh/__openerp__.py +++ /dev/null @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2009 OpenERP SA () -# Copyright (C) 2011 SYLEAM () -# Copyright (C) 2013 Julius Network Solutions SARL -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## - -{ - "name": "SMS Client OVH", - "version": "1.0", - "depends": ["base", - "email_template", - "smsclient_core", - ], - "author": "Julius Network Solutions,SYLEAM,OpenERP SA," - "Odoo Community Association (OCA),Akretion", - 'images': [ - 'images/sms.jpeg', - 'images/gateway.jpeg', - 'images/gateway_access.jpeg', - 'images/client.jpeg', - 'images/send_sms.jpeg' - ], - "website": "http://julius.fr", - "category": "Tools", - "demo": [], - "data": [ - ], - "active": False, - "installable": True, -} - -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/smsclient_ovh/data/keychain.xml b/smsclient_ovh/data/keychain.xml new file mode 100644 index 0000000..8562a09 --- /dev/null +++ b/smsclient_ovh/data/keychain.xml @@ -0,0 +1,9 @@ + + + + Ovh sms default + ovh_provider + ovh_default_account + + + \ No newline at end of file diff --git a/smsclient_ovh/models/__init__.py b/smsclient_ovh/models/__init__.py new file mode 100644 index 0000000..53c1588 --- /dev/null +++ b/smsclient_ovh/models/__init__.py @@ -0,0 +1,7 @@ +# coding: utf-8 +# Copyright (C) 2015 Sébastien BEAU +# Valentin CHEMIERE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import sms_gateway +from . import keychain diff --git a/smsclient_ovh/models/keychain.py b/smsclient_ovh/models/keychain.py new file mode 100644 index 0000000..293810a --- /dev/null +++ b/smsclient_ovh/models/keychain.py @@ -0,0 +1,45 @@ +# coding: utf-8 +# Copyright (C) 2015 Sébastien BEAU +# Valentin CHEMIERE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import models, fields +from functools import wraps + + +def implemented_by_provider(func): + """Decorator: call _provider_prefixed method instead. + Usage: + @implemented_by_provider + def _do_something() + def _laposte_do_something() + def _gls_do_something() + At runtime, sms-client._do_something() will try to call + the provider spectific method or fallback to generic _do_something + """ + @wraps(func) + def wrapper(cls, *args, **kwargs): + fun_name = func.__name__ + fun = '_%s%s' % (cls.provider_type, fun_name) + if not hasattr(cls, fun): + fun = '_provider%s' % (fun_name) + # return func(cls, *args, **kwargs) + return getattr(cls, fun)(*args, **kwargs) + return wrapper + + +OVH_KEYCHAIN_NAMESPACE = 'ovh_provider' + + +class AccountProduct(models.Model): + _inherit = 'keychain.account' + + namespace = fields.Selection( + selection_add=[(OVH_KEYCHAIN_NAMESPACE, 'Ovh_sms')]) + + def _ovh_provider_init_data(self): + return {'sms_account': ""} + + def _ovh_provider_validate_data(self, data): + + return True diff --git a/smsclient_ovh/models/sms_gateway.py b/smsclient_ovh/models/sms_gateway.py new file mode 100644 index 0000000..8e9a6ea --- /dev/null +++ b/smsclient_ovh/models/sms_gateway.py @@ -0,0 +1,86 @@ +# coding: utf-8 +# Copyright (C) 2015 Sébastien BEAU +# Valentin CHEMIERE +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import api, models +import requests +import logging +import json + +_logger = logging.getLogger(__name__) + + +class SmsClient(models.Model): + _inherit = "sms.gateway" + + @api.model + def get_method(self): + method = super(SmsClient, self).get_method() + method.append(('http_ovh', 'OVH HTTP'), ) + return method + + @api.multi + def _provider_get_provider_conf(self): + for rec in self: + keychain = rec.env['keychain.account'] + if rec._check_permissions: + retrieve = keychain.suspend_security().retrieve + else: + retrieve = keychain.retrieve + accounts = retrieve( + [['namespace', '=', 'ovh_provider']]) + return accounts[0] + + +class SmsSms(models.Model): + _inherit = "sms.sms" + + @api.model + def _prepare_http_ovh(self): + + keychain_account = self.gateway_id._provider_get_provider_conf() + keychain_json = json.loads(keychain_account['data']) + params = { + 'smsAccount': keychain_json['sms_account'], + 'login': keychain_account['login'], + 'password': keychain_account.get_password(), + 'from': self.gateway_id.from_provider, + 'url': self.gateway_id.url, + 'to': self._convert_to_e164(self.mobile), + 'message': self.message, + } + if self.nostop: + params['noStop'] = 1 + if self.deferred: + params['deferred'] = self.deferred + if self.classes: + params['class'] = self.classes + if self.tag: + params['tag'] = self.tag + if self.coding: + params['smsCoding'] = self.coding + return params + + @api.model + def _convert_to_e164(self, erp_number): + to_dial_number = erp_number.replace(u'\xa0', u'') + return to_dial_number + + @api.multi + def _send_http_ovh(self): + self.ensure_one() + params = self._prepare_http_ovh() + r = requests.get(params['url'], params=params.items()) + params.update({ + 'password': '*****', + 'to': '*****', + 'smsAccount': '*****', + 'login': '*****', + }) + print params + _logger.debug("Call OVH API : %s params %s", + params['url'], params) + response = r.text + if response[0:2] != 'OK': + raise ValueError(response) diff --git a/smsclient_ovh/sms_gateway.py b/smsclient_ovh/sms_gateway.py deleted file mode 100644 index 6d27078..0000000 --- a/smsclient_ovh/sms_gateway.py +++ /dev/null @@ -1,97 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################### -# -# Module for OpenERP -# Copyright (C) 2015 Akretion (http://www.akretion.com). -# @author Valentin CHEMIERE -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################### - -from openerp import api, models, _ -import requests -import logging -_logger = logging.getLogger(__name__) - - -class SmsClient(models.Model): - _inherit = "sms.gateway" - - @api.model - def get_method(self): - method = super(SmsClient, self).get_method() - method.append(('http_ovh', 'OVH HTTP'), ) - return method - - @api.onchange('method') - def onchange_method(self): - super(SmsClient, self).onchange_method() - if self.method == 'http_ovh': - self.url_visible = True - self.sms_account_visible = True - self.login_provider_visible = True - self.password_provider_visible = True - self.from_provider_visible = True - self.validity_visible = True - self.classes_visible = True - self.deferred_visible = True - self.nostop_visible = True - self.priority_visible = True - self.coding_visible = True - self.tag_visible = True - self.char_limit_visible = True - - -class SmsSms(models.Model): - _inherit = "sms.sms" - - @api.model - def _prepare_http_ovh(self): - params = { - 'smsAccount': self.gateway_id.sms_account, - 'login': self.gateway_id.login_provider, - 'password': self.gateway_id.password_provider, - 'from': self.gateway_id.from_provider, - 'to': self.mobile, - 'message': self.message, - } - if self.nostop: - params['noStop'] = 1 - if self.deferred: - params['deferred'] = self.deferred - if self.classes: - params['class'] = self.classes - if self.tag: - params['tag'] = self.tag - if self.coding: - params['smsCoding'] = self.coding - return params - - @api.multi - def _send_http_ovh(self): - self.ensure_one() - params = self._prepare_http_ovh() - r = requests.get(self.gateway_id.url, params=params.items()) - params.update({ - 'password': '*****', - 'to': '*****', - 'smsAccount': '*****', - 'login': '*****', - }) - _logger.debug("Call OVH API : %s params %s", - self.gateway_id.url, params) - response = r.text - if response[0:2] != 'OK': - raise ValueError(response)