← Back to team overview

openerp-community-reviewer team mailing list archive

[Merge] lp:~akretion-team/server-env-tools/module-configuration-helper-dbl into lp:server-env-tools

 

David BEAL has proposed merging lp:~akretion-team/server-env-tools/module-configuration-helper-dbl into lp:server-env-tools.

Commit message:
[ADD] module configuration_helper

Requested reviews:
  Server Environment And Tools Core Editors (server-env-tools-core-editors)

For more details, see:
https://code.launchpad.net/~akretion-team/server-env-tools/module-configuration-helper-dbl/+merge/220392

add module configuration_helper


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
-- 
https://code.launchpad.net/~akretion-team/server-env-tools/module-configuration-helper-dbl/+merge/220392
Your team Server Environment And Tools Core Editors is requested to review the proposed merge of lp:~akretion-team/server-env-tools/module-configuration-helper-dbl into lp:server-env-tools.
=== added directory 'configuration_helper'
=== added file 'configuration_helper/__init__.py'
--- configuration_helper/__init__.py	1970-01-01 00:00:00 +0000
+++ configuration_helper/__init__.py	2014-05-21 08:40:35 +0000
@@ -0,0 +1,22 @@
+# -*- 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 <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from . import config   # noqa

=== added file 'configuration_helper/__openerp__.py'
--- configuration_helper/__openerp__.py	1970-01-01 00:00:00 +0000
+++ configuration_helper/__openerp__.py	2014-05-21 08:40:35 +0000
@@ -0,0 +1,82 @@
+# -*- 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 <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+{
+    'name': 'Configuration Helper',
+    'version': '0.8',
+    'author': 'Akretion',
+    '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 <david.beal@xxxxxxxxxxxx>
+* Sébastien BEAU <sebastien.beau@xxxxxxxxxxxx>
+* Yannick Vaucher, Camptocamp, (code refactoring from his module 'delivery_carrier_label_postlogistics')
+
+ """,
+    'website': 'http://www.akretion.com/',
+    'data': [
+    ],
+    'tests': [],
+    'installable': True,
+    'auto_install': False,
+    'license': 'AGPL-3',
+    'application': True,
+}

=== added file 'configuration_helper/config.py'
--- configuration_helper/config.py	1970-01-01 00:00:00 +0000
+++ configuration_helper/config.py	2014-05-21 08:40:35 +0000
@@ -0,0 +1,117 @@
+# -*- 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 <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+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:
+            #print '    self._companyObject._columns.keys()', self._companyObject._columns.keys(), '\n\n'
+            for field_key in self._companyObject._columns:
+                #print 'field_key', field_key
+                #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,
+                    }
+                    #print '\n  __dict__:', self._companyObject._columns[field_key].__dict__, '\n  keys:', self._companyObject._columns[field_key].__dict__.keys()
+                    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 related_field_to_populate(self, cr, uid, field, context=None):
+        """Field with prefix name as 'module_' allows to
+        install module.
+        'company_id' field is used to store values
+        in the right companies"""
+        if field != 'company_id' and field[:6] != 'module':
+            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 = self.pool['res.company'].browse(
+            cr, uid, company_id, context=context)
+        for field in self._columns:
+            if self.related_field_to_populate(cr, uid, field, 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):
+        "code from Yannick Vaucher (Camptocamp) "
+        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


Follow ups