From 10cd7cede833d714a74357c3897d4e4ae2efa789 Mon Sep 17 00:00:00 2001 From: Yannick Vaucher Date: Thu, 26 May 2016 08:29:58 +0200 Subject: [PATCH] [MGR] configuration_helper * [PORT] [9.0] configuration_helper Port to API 8.0 and make it compatible with a config definition in API 8.0. Remove some hacks with onchange and write to get and set the related values. * Add a module to define classes in order to test configuration_helper --- configuration_helper/README.rst | 57 +++++++++ configuration_helper/__init__.py | 23 +--- configuration_helper/__openerp__.py | 98 +++------------ configuration_helper/config.py | 114 ------------------ configuration_helper/models/__init__.py | 1 + configuration_helper/models/config.py | 54 +++++++++ test_configuration_helper/README.rst | 30 +++++ test_configuration_helper/__init__.py | 1 + test_configuration_helper/__openerp__.py | 18 +++ test_configuration_helper/models.py | 36 ++++++ test_configuration_helper/tests/__init__.py | 1 + .../tests/test_config.py | 34 ++++++ 12 files changed, 250 insertions(+), 217 deletions(-) create mode 100644 configuration_helper/README.rst delete mode 100644 configuration_helper/config.py create mode 100644 configuration_helper/models/__init__.py create mode 100644 configuration_helper/models/config.py create mode 100644 test_configuration_helper/README.rst create mode 100644 test_configuration_helper/__init__.py create mode 100644 test_configuration_helper/__openerp__.py create mode 100644 test_configuration_helper/models.py create mode 100644 test_configuration_helper/tests/__init__.py create mode 100644 test_configuration_helper/tests/test_config.py diff --git a/configuration_helper/README.rst b/configuration_helper/README.rst new file mode 100644 index 000000000..710ec7501 --- /dev/null +++ b/configuration_helper/README.rst @@ -0,0 +1,57 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +==================== +Configuration Helper +==================== + +*This module is intended for developer only. It does nothing used alone.* + +It helps to create `config.settings` by providing an abstract Class. + +This class: + + * creates automatically related fields in 'whatiwant.config.settings' + using those defined in 'res.company': it avoids duplicated field definitions. + * company_id field with default value is created + * onchange_company_id is defined to update all related fields + * supported fields: char, text, integer, float, datetime, date, boolean, m2o + + +How to use +---------- + +.. code-block:: python + + from . company import ResCompany + + class WhatiwantClassSettings(orm.TransientModel): + _inherit = ['res.config.settings', 'abstract.config.settings'] + _name = 'whatiwant.config.settings' + # fields must be defined in ResCompany class + # related fields are automatically generated from previous definitions + _companyObject = ResCompany + # all prefixed field with _prefix in res.company, will be available in 'whatiwant.config.settings' model + _prefix = 'prefixyouchoose_' + + +Roadmap +------- + * support (or check support) for these field types : o2m, m2m, reference, property, selection + * automatically generate a default view for 'whatiwant.config.settings' (in --debug ?) + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Contributors +------------ + +* Yannick Vaucher +* David BEAL +* Sébastien BEAU diff --git a/configuration_helper/__init__.py b/configuration_helper/__init__.py index bfca433c9..0650744f6 100644 --- a/configuration_helper/__init__.py +++ b/configuration_helper/__init__.py @@ -1,22 +1 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: David BEAL -# Copyright 2014 Akretion -# -# 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 . import config # noqa +from . import models diff --git a/configuration_helper/__openerp__.py b/configuration_helper/__openerp__.py index 3d7c6401a..38709aced 100644 --- a/configuration_helper/__openerp__.py +++ b/configuration_helper/__openerp__.py @@ -1,82 +1,18 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# Author: David BEAL -# Copyright 2014 Akretion -# -# 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': 'Configuration Helper', - 'version': '0.8', - 'author': "Akretion,Odoo Community Association (OCA)", - 'maintainer': 'Akretion', - 'category': 'server', - 'complexity': 'normal', - 'depends': ['base'], - 'description': """ -Configuration Helper -==================== - -*This module is intended for developer only. It does nothing used alone.* - -This module : - - * create automatically related fields in 'whatiwant.config.settings' - using those defined in 'res.company' : it avoid duplicated field definitions. - * company_id field with default value is created - * onchange_company_id is defined to update all related fields - * supported fields: char, text, integer, float, datetime, date, boolean, m2o - - -How to use ----------- - -.. code-block:: python - - from . company import ResCompany - - class WhatiwantClassSettings(orm.TransientModel): - _inherit = ['res.config.settings', 'abstract.config.settings'] - _name = 'whatiwant.config.settings' - # fields must be defined in ResCompany class - # related fields are automatically generated from previous definitions - _companyObject = ResCompany - - -Roadmap -------- - * support (or check support) for these field types : o2m, m2m, reference, property, selection - * automatically generate a default view for 'whatiwant.config.settings' (in --debug ?) - - -Contributors ------------- - -* David BEAL -* Sébastien BEAU -* Yannick Vaucher, Camptocamp, (code refactoring from his module 'delivery_carrier_label_postlogistics') - - """, - 'website': 'http://www.akretion.com/', - 'data': [ - ], - 'tests': [], - 'installable': False, - 'auto_install': False, - 'license': 'AGPL-3', - 'application': True, -} +# © 2014 David BEAL Akretion +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{'name': 'Configuration Helper', + 'version': '9.0.1.0.0', + 'author': "Akretion,Odoo Community Association (OCA)", + 'maintainer': 'Akretion', + 'category': 'server', + 'complexity': 'normal', + 'depends': ['base'], + 'website': 'http://www.akretion.com/', + 'data': [], + 'tests': [], + 'installable': True, + 'auto_install': False, + 'license': 'AGPL-3', + 'application': False, + } diff --git a/configuration_helper/config.py b/configuration_helper/config.py deleted file mode 100644 index 73fd58e2f..000000000 --- a/configuration_helper/config.py +++ /dev/null @@ -1,114 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# Author: David BEAL, Copyright 2014 Akretion -# -# 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 . -# -############################################################################## -import re - -from openerp.osv import orm, fields - - -class AbstractConfigSettings(orm.AbstractModel): - _name = 'abstract.config.settings' - _description = 'Abstract configuration settings' - # prefix field name to differentiate fields in company with those in config - _prefix = 'setting_' - # this is the class name to import in your module - # (it should be ResCompany or res_company, depends of your code) - _companyObject = None - - def _filter_field(self, field_key): - """Inherit in your module to define for which company field - you don't want have a matching related field""" - return True - - def __init__(self, pool, cr): - super(AbstractConfigSettings, self).__init__(pool, cr) - if self._companyObject: - for field_key in self._companyObject._columns: - # allows to exclude some field - if self._filter_field(field_key): - args = ('company_id', field_key) - kwargs = { - 'string': self._companyObject._columns[field_key].string, - 'help': self._companyObject._columns[field_key].help, - 'type': self._companyObject._columns[field_key]._type, - } - if '_obj' in self._companyObject._columns[field_key].__dict__.keys(): - kwargs['relation'] = \ - self._companyObject._columns[field_key]._obj - if '_domain' in \ - self._companyObject._columns[field_key].__dict__.keys(): - kwargs['domain'] = \ - self._companyObject._columns[field_key]._domain - field_key = re.sub('^' + self._prefix, '', field_key) - self._columns[field_key] = \ - fields.related(*args, **kwargs) - - _columns = { - 'company_id': fields.many2one( - 'res.company', - 'Company', - required=True), - } - - def _default_company(self, cr, uid, context=None): - user = self.pool['res.users'].browse(cr, uid, uid, context=context) - return user.company_id.id - - _defaults = { - 'company_id': _default_company, - } - - def field_to_populate_as_related(self, cr, uid, field, company_cols, context=None): - """Only fields which comes from company with the right prefix - must be defined as related""" - if self._prefix + field in company_cols: - return True - return False - - def onchange_company_id(self, cr, uid, ids, company_id, context=None): - " update related fields " - values = {} - values['currency_id'] = False - if not company_id: - return {'value': values} - company_m = self.pool['res.company'] - company = company_m.browse( - cr, uid, company_id, context=context) - company_cols = company_m._columns.keys() - for field in self._columns: - if self.field_to_populate_as_related( - cr, uid, field, company_cols, context=context): - cpny_field = self._columns[field].arg[-1] - if self._columns[field]._type == 'many2one': - values[field] = company[cpny_field]['id'] or False - else: - values[field] = company[cpny_field] - return {'value': values} - - def create(self, cr, uid, values, context=None): - id = super(AbstractConfigSettings, self).create( - cr, uid, values, context=context) - # Hack: to avoid some nasty bug, related fields are not written - # upon record creation. Hence we write on those fields here. - vals = {} - for fname, field in self._columns.iteritems(): - if isinstance(field, fields.related) and fname in values: - vals[fname] = values[fname] - self.write(cr, uid, [id], vals, context) - return id diff --git a/configuration_helper/models/__init__.py b/configuration_helper/models/__init__.py new file mode 100644 index 000000000..d63bc18b6 --- /dev/null +++ b/configuration_helper/models/__init__.py @@ -0,0 +1 @@ +from . import config diff --git a/configuration_helper/models/config.py b/configuration_helper/models/config.py new file mode 100644 index 000000000..1eb51af88 --- /dev/null +++ b/configuration_helper/models/config.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +# © 2014 David BEAL Akretion +# © 2016 Yannick Vaucher (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import re + +from openerp import api, fields, models + + +class AbstractConfigSettings(models.AbstractModel): + _name = 'abstract.config.settings' + _description = 'Abstract configuration settings' + # prefix field name to differentiate fields in company with those in config + _prefix = 'setting_' + # this is the class name to import in your module + # (it should be ResCompany or res_company, depends of your code) + _companyObject = None + _setup_extra_done = False + + company_id = fields.Many2one( + 'res.company', + 'Company', + required=True, + default=lambda self: self.env.user.company_id + ) + + def _filter_field(self, field_key): + """Inherit in your module to define for which company field + you don't want have a matching related field""" + return True + + @api.model + def _setup_base(self, partial): + cls = type(self) + super(AbstractConfigSettings, self)._setup_base(partial) + if not self._companyObject: + return + if cls._setup_extra_done: + return + for field_key in cls._companyObject.__dict__.keys(): + field = cls._companyObject.__dict__[field_key] + if isinstance(field, fields.Field): + # allows to exclude some field + if self._filter_field(field_key): + # fields.agrs contains fields attributes + kwargs = field.args.copy() + kwargs['related'] = 'company_id.' + field_key + field_key = re.sub('^' + self._prefix, '', field_key) + self._add_field(field_key, field.new(**kwargs)) + cls._proper_fields = set(cls._fields) + + self._add_inherited_fields() + cls.pool.model_cache[cls.__bases__] = cls + cls._setup_extra_done = True diff --git a/test_configuration_helper/README.rst b/test_configuration_helper/README.rst new file mode 100644 index 000000000..9f2fc49b3 --- /dev/null +++ b/test_configuration_helper/README.rst @@ -0,0 +1,30 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +============================ +Configuration Helper - TESTS +============================ + +*This module is only intended to test configuration_helper module.* + +Contributors +------------ + +* Yannick Vaucher + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. + diff --git a/test_configuration_helper/__init__.py b/test_configuration_helper/__init__.py new file mode 100644 index 000000000..0650744f6 --- /dev/null +++ b/test_configuration_helper/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/test_configuration_helper/__openerp__.py b/test_configuration_helper/__openerp__.py new file mode 100644 index 000000000..6a5e310f5 --- /dev/null +++ b/test_configuration_helper/__openerp__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# © 2016 Yannick Vaucher (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{'name': 'Configuration Helper - Tests', + 'version': '9.0.1.0.0', + 'author': "Akretion,Odoo Community Association (OCA)", + 'maintainer': 'Akretion', + 'category': 'Tests', + 'complexity': 'normal', + 'depends': ['configuration_helper'], + 'website': 'http://www.akretion.com/', + 'data': [], + 'tests': [], + 'installable': True, + 'auto_install': False, + 'license': 'AGPL-3', + 'application': False, + } diff --git a/test_configuration_helper/models.py b/test_configuration_helper/models.py new file mode 100644 index 000000000..d94f8ede0 --- /dev/null +++ b/test_configuration_helper/models.py @@ -0,0 +1,36 @@ +# -*- coding: utf-8 -*- +# © 2016 Yannick Vaucher (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from openerp import fields, models, osv + + +class ResCompanyA(models.Model): + _inherit = 'res.company' + + prefix_a_name = fields.Char() + prefix_a_integer = fields.Integer() + prefix_a_partner_id = fields.Many2one(comodel_name='res.partner') + + +class ResCompanyB(models.Model): + _inherit = 'res.company' + + _columns = { + 'prefix_b_name': osv.fields.char('name'), + 'prefix_b_integer': osv.fields.integer('int'), + 'prefix_b_partner_id': osv.fields.many2one('res.partner'), + } + + +class MyConfigA(models.TransientModel): + _name = 'a.config.settings' + _inherit = ['res.config.settings', 'abstract.config.settings'] + _prefix = 'prefix_a_' + _companyObject = ResCompanyA + + +class MyConfigB(models.TransientModel): + _name = 'b.config.settings' + _inherit = ['res.config.settings', 'abstract.config.settings'] + _prefix = 'prefix_b_' + _companyObject = ResCompanyB diff --git a/test_configuration_helper/tests/__init__.py b/test_configuration_helper/tests/__init__.py new file mode 100644 index 000000000..7e8d1fc8d --- /dev/null +++ b/test_configuration_helper/tests/__init__.py @@ -0,0 +1 @@ +from . import test_config diff --git a/test_configuration_helper/tests/test_config.py b/test_configuration_helper/tests/test_config.py new file mode 100644 index 000000000..8d14767c2 --- /dev/null +++ b/test_configuration_helper/tests/test_config.py @@ -0,0 +1,34 @@ +# -*- coding: utf-8 -*- +# © 2016 Yannick Vaucher (Camptocamp SA) +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +import openerp.tests.common as common + + +class TestAbstractSettings(common.TransactionCase): + + def setUp(self): + super(TestAbstractSettings, self).setUp() + self.partner_id = self.ref('base.res_partner_12') + self.company = self.env.ref('base.main_company') + + def test_config(self): + wiz = self.env['a.config.settings'].create({}) + wiz.name = 'Toto' + wiz.integer = 11 + wiz.partner_id = self.partner_id + wiz.execute() + + self.assertEqual(self.company.prefix_a_name, wiz.name) + self.assertEqual(self.company.prefix_a_integer, wiz.integer) + self.assertEqual(self.company.prefix_a_partner_id, wiz.partner_id) + + def test_config_old_api(self): + wiz = self.env['b.config.settings'].create({}) + wiz.name = 'Toto' + wiz.integer = 11 + wiz.partner_id = self.partner_id + wiz.execute() + + self.assertEqual(self.company.prefix_b_name, wiz.name) + self.assertEqual(self.company.prefix_b_integer, wiz.integer) + self.assertEqual(self.company.prefix_b_partner_id, wiz.partner_id)