<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2011 SYLEAM (<http://syleam.fr/>)
#    Copyright (C) 2013 Julius Network Solutions SARL <<EMAIL>>
#    Copyright (C) 2015 <NAME> <<EMAIL>>
#
#    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 <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api, _
from openerp.exceptions import Warning
import urllib
import logging
from openerp.addons.server_environment import serv_config
_logger = logging.getLogger(__name__)


PRIORITY_LIST = [
    ('0', '0'),
    ('1', '1'),
    ('2', '2'),
    ('3', '3')
]

CLASSES_LIST = [
    ('0', 'Flash'),
    ('1', 'Phone display'),
    ('2', 'SIM'),
    ('3', 'Toolkit')
]


class partner_sms_send(models.Model):
    _name = "partner.sms.send"

    @api.model
    def _default_get_mobile(self):
        partner_pool = self.env['res.partner']
        active_ids = self._context.get('active_ids')
        res = {}
        if len(active_ids) > 1:
            raise Warning(_('You can only select one partner'))
        for partner in partner_pool.browse(active_ids):
            res = partner.mobile
        return res

    @api.model
    def _default_get_gateway(self):
        sms_obj = self.env['sms.smsclient']
        gateways = sms_obj.search([], limit=1)
        return gateways or False

    @api.multi
    def onchange_gateway(self, gateway_id):
        sms_obj = self.env['sms.smsclient']
        if not gateway_id:
            return {}
        gateway = sms_obj.browse(gateway_id)
        return {
            'value': {
                'validity': gateway.validity,
                'classes': gateway.classes,
                'deferred': gateway.deferred,
                'priority': gateway.priority,
                'coding': gateway.coding,
                'tag': gateway.tag,
                'nostop': gateway.nostop,
            }
        }

    mobile_to = fields.Char('To', required=True,
                            default=_default_get_mobile)
    app_id = fields.Char('API ID')
    user = fields.Char('Login')
    password = fields.Char('Password')
    text = fields.Text('SMS Message', required=True)
    gateway = fields.Many2one('sms.smsclient', 'SMS Gateway', required=True,
                              default=_default_get_gateway
                              )
    validity = fields.Integer('Validity',
                              help='the maximum time -in minute(s)-'
                                   'before the message is dropped')
    classes = fields.Selection(
        CLASSES_LIST,
        'Class',
        help='the sms class: flash(0), phone display(1), SIM(2), toolkit(3)')
    deferred = fields.Integer('Deferred',
                              help='the time -in minute(s)- '
                                   'to wait before sending the message')
    priority = fields.Selection(PRIORITY_LIST, 'Priority',
                                help='The priority of the message')
    coding = fields.Selection([
        ('1', '7 bit'),
        ('2', 'Unicode')
        ], 'Coding', help='The SMS coding: 1 for 7 bit or 2 for unicode')
    tag = fields.Char('Tag', help='an optional tag')
    nostop = fields.Boolean('NoStop',
                            help='Do not display STOP clause in the message,'
                                 'this requires that this is not an '
                                 'advertising message')

    @api.multi
    def sms_send(self):
        client_obj = self.env['sms.smsclient']
        for data in self:
            if not data.gateway:
                raise Warning(_('No Gateway Found'))
            else:
                client_obj._send_message(data)
        return {}


class SMSClient(models.Model):
    _name = 'sms.smsclient'
    _description = 'SMS Client'

    @api.model
    def get_method(self):
        return []

    @api.multi
    def get_provider_conf(self, name, args):
        res = {}
        for sms_provider in self:
            global_section_name = 'sms_provider'
            # default vals
            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):
                    if config_vals.get('url_service'):
                        config_vals['url'] = int(config_vals['url_service'])
                    if config_vals.get('sms_account'):
                        config_vals['sms_account'] = int(config_vals['sms_account'])
                    if config_vals.get('login'):
                        config_vals['login_provider'] = int(config_vals['login'])
                    if config_vals.get('password'):
                        config_vals['password_provider'] = int(config_vals['password'])
                    if config_vals.get('from'):
                        config_vals['from_provider'] = int(config_vals['from'])
                        res[mail_server.id] = config_vals
        return res


    name = fields.Char('Gateway Name', required=True)
    url = fields.Char('Gateway URL',
                      required=True, help='Base url for message',
                      compute='get_provider_conf'
                      )
    url_visible = fields.Boolean(default=False)
    history_line = fields.One2many('sms.smsclient.history',
                                   'gateway_id', 'History')
    method = fields.Selection(string='API Method',
                              selection='get_method',
                              index=True,
                              )
    state = fields.Selection([
        ('new', 'Not Verified'),
        ('waiting', 'Waiting for Verification'),
        ('confirm', 'Verified'),
        ], 'Gateway Status', index=True, readonly=True, default='new')
    users_id = fields.Many2many('res.users',
                                'res_smsserver_group_rel',
                                'sid',
                                'uid',
                                '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)
<commit_msg>Fix server environment support<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#    Copyright (C) 2011 SYLEAM (<http://syleam.fr/>)
#    Copyright (C) 2013 Julius Network Solutions SARL <<EMAIL>>
#    Copyright (C) 2015 <NAME> <<EMAIL>>
#
#    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 <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api, _
from openerp.exceptions import Warning
import urllib
import logging
from openerp.addons.server_environment import serv_config
_logger = logging.getLogger(__name__)


PRIORITY_LIST = [
    ('0', '0'),
    ('1', '1'),
    ('2', '2'),
    ('3', '3')
]

CLASSES_LIST = [
    ('0', 'Flash'),
    ('1', 'Phone display'),
    ('2', 'SIM'),
    ('3', 'Toolkit')
]


class partner_sms_send(models.Model):
    _name = "partner.sms.send"

    @api.model
    def _default_get_mobile(self):
        partner_pool = self.env['res.partner']
        active_ids = self._context.get('active_ids')
        res = {}
        if len(active_ids) > 1:
            raise Warning(_('You can only select one partner'))
        for partner in partner_pool.browse(active_ids):
            res = partner.mobile
        return res

    @api.model
    def _default_get_gateway(self):
        sms_obj = self.env['sms.smsclient']
        gateways = sms_obj.search([], limit=1)
        return gateways or False

    @api.multi
    def onchange_gateway(self, gateway_id):
        sms_obj = self.env['sms.smsclient']
        if not gateway_id:
            return {}
        gateway = sms_obj.browse(gateway_id)
        return {
            'value': {
                'validity': gateway.validity,
                'classes': gateway.classes,
                'deferred': gateway.deferred,
                'priority': gateway.priority,
                'coding': gateway.coding,
                'tag': gateway.tag,
                'nostop': gateway.nostop,
            }
        }

    mobile_to = fields.Char('To', required=True,
                            default=_default_get_mobile)
    app_id = fields.Char('API ID')
    user = fields.Char('Login')
    password = fields.Char('Password')
    text = fields.Text('SMS Message', required=True)
    gateway = fields.Many2one('sms.smsclient', 'SMS Gateway', required=True,
                              default=_default_get_gateway
                              )
    validity = fields.Integer('Validity',
                              help='the maximum time -in minute(s)-'
                                   'before the message is dropped')
    classes = fields.Selection(
        CLASSES_LIST,
        'Class',
        help='the sms class: flash(0), phone display(1), SIM(2), toolkit(3)')
    deferred = fields.Integer('Deferred',
                              help='the time -in minute(s)- '
                                   'to wait before sending the message')
    priority = fields.Selection(PRIORITY_LIST, 'Priority',
                                help='The priority of the message')
    coding = fields.Selection([
        ('1', '7 bit'),
        ('2', 'Unicode')
        ], 'Coding', help='The SMS coding: 1 for 7 bit or 2 for unicode')
    tag = fields.Char('Tag', help='an optional tag')
    nostop = fields.Boolean('NoStop',
                            help='Do not display STOP clause in the message,'
                                 'this requires that this is not an '
                                 'advertising message')

    @api.multi
    def sms_send(self):
        client_obj = self.env['sms.smsclient']
        for data in self:
            if not data.gateway:
                raise Warning(_('No Gateway Found'))
            else:
                client_obj._send_message(data)
        return {}


class SMSClient(models.Model):
    _name = 'sms.smsclient'
    _description = 'SMS Client'

    @api.model
    def get_method(self):
        return []

    @api.multi
    def _get_provider_conf(self):
        for sms_provider in self:
            global_section_name = 'sms_provider'
            # default vals
            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))
                    if config_vals.get('url_service'):
                        sms_provider.url = config_vals['url_service']
                    if config_vals.get('sms_account'):
                        sms_provider.sms_account = config_vals['sms_account']
                    if config_vals.get('login'):
                        sms_provider.login_provider = config_vals['login']
                    if config_vals.get('password'):
                        sms_provider.password_provider = config_vals['password']
                    if config_vals.get('from'):
                        sms_provider.from_provider = config_vals['from']


    name = fields.Char('Gateway Name', required=True)
    url = fields.Char('Gateway URL',
                      help='Base url for message',
                      compute='_get_provider_conf'
                      )
    url_visible = fields.Boolean(default=False)
    history_line = fields.One2many('sms.smsclient.history',
                                   'gateway_id', 'History')
    method = fields.Selection(string='API Method',
                              selection='get_method',
                              index=True,
                              )
    state = fields.Selection([
        ('new', 'Not Verified'),
        ('waiting', 'Waiting for Verification'),
        ('confirm', 'Verified'),
        ], 'Gateway Status', index=True, readonly=True, default='new')
    users_id = fields.Many2many('res.users',
                                'res_smsserver_group_rel',
                                'sid',
                                'uid',
                                '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)

{'fraction_non_alphanumeric': 0.12675869392089195, 'fraction_numerical': 0.006503849216883462, 'mean_word_length': 1.9675068924773533, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7528868', 'n_tokens_mistral': 4189, 'n_tokens_neox': 3972, 'n_words': 978}