Merge lp:~akretion-team/server-env-tools/module-configuration-helper-dbl into lp:~server-env-tools-core-editors/server-env-tools/7.0

Proposed by David BEAL (ak)
Status: Needs review
Proposed branch: lp:~akretion-team/server-env-tools/module-configuration-helper-dbl
Merge into: lp:~server-env-tools-core-editors/server-env-tools/7.0
Diff against target: 233 lines (+218/-0)
3 files modified
configuration_helper/__init__.py (+22/-0)
configuration_helper/__openerp__.py (+82/-0)
configuration_helper/config.py (+114/-0)
To merge this branch: bzr merge lp:~akretion-team/server-env-tools/module-configuration-helper-dbl
Reviewer Review Type Date Requested Status
Yannick Vaucher @ Camptocamp Needs Resubmitting
Florian da Costa (community) test Approve
Review via email: mp+220392@code.launchpad.net

Commit message

[ADD] module configuration_helper

Description of the change

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

To post a comment you must log in.
Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

Used it to create a config and works well.

Code review

l.161 + l.163 + l.172 please remove commented print line

l.226 no need to quote me for this code I took it from
addons/account/res_config.py

review: Needs Fixing (test + code review)
Revision history for this message
David BEAL (ak) (davidbeal) wrote :

@yannick

I have fixed it.

Your code gave me the idea to do this module,
You have done an improvement revision 7

thanks to you

Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

still you can remove l.223

Otherwise LGTM

review: Approve (code review and test)
Revision history for this message
David BEAL (ak) (davidbeal) wrote :
Revision history for this message
Florian da Costa (florian-dacosta) wrote :

I now use it for delivery_carrier_chronopost and it work well!
Thanks David

review: Approve (test)
Revision history for this message
Yannick Vaucher @ Camptocamp (yvaucher-c2c) wrote :

Hello David,

I'm ok to merge this, I even tried to merge it before this project goes on Github. However, it seems you created that module on an empty branch.

Could you redo/rebase this?

Might be better to do it directly in git ?

review: Needs Resubmitting

Unmerged revisions

9. By David BEAL (ak)

[FIX] condition in field_to_populate_as_related() to limit fields related creation

8. By David BEAL (ak)

[FIX] remove print strings

7. By yvaucher-c2c

[MERGE] from lp:~camptocamp/server-env-tools/module-configuration-helper-yvr

6. By David BEAL (ak)

[IMP] add a prefix to related fields in ...config.settings

5. By David BEAL (ak)

[IMP] complete doc

4. By David BEAL (ak)

[IMP] add create method on AbstractConfigSettings

3. By David BEAL (ak)

[FIX] for m2o field

2. By David BEAL (ak)

[FIX] __init__ method + add doc

1. By David BEAL (ak)

[INIT] add configuration_helper module

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'configuration_helper'
2=== added file 'configuration_helper/__init__.py'
3--- configuration_helper/__init__.py 1970-01-01 00:00:00 +0000
4+++ configuration_helper/__init__.py 2014-05-23 12:54:31 +0000
5@@ -0,0 +1,22 @@
6+# -*- coding: utf-8 -*-
7+##############################################################################
8+#
9+# Author: David BEAL
10+# Copyright 2014 Akretion
11+#
12+# This program is free software: you can redistribute it and/or modify
13+# it under the terms of the GNU Affero General Public License as
14+# published by the Free Software Foundation, either version 3 of the
15+# License, or (at your option) any later version.
16+#
17+# This program is distributed in the hope that it will be useful,
18+# but WITHOUT ANY WARRANTY; without even the implied warranty of
19+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+# GNU Affero General Public License for more details.
21+#
22+# You should have received a copy of the GNU Affero General Public License
23+# along with this program. If not, see <http://www.gnu.org/licenses/>.
24+#
25+##############################################################################
26+
27+from . import config # noqa
28
29=== added file 'configuration_helper/__openerp__.py'
30--- configuration_helper/__openerp__.py 1970-01-01 00:00:00 +0000
31+++ configuration_helper/__openerp__.py 2014-05-23 12:54:31 +0000
32@@ -0,0 +1,82 @@
33+# -*- coding: utf-8 -*-
34+##############################################################################
35+#
36+# Author: David BEAL
37+# Copyright 2014 Akretion
38+#
39+# This program is free software: you can redistribute it and/or modify
40+# it under the terms of the GNU Affero General Public License as
41+# published by the Free Software Foundation, either version 3 of the
42+# License, or (at your option) any later version.
43+#
44+# This program is distributed in the hope that it will be useful,
45+# but WITHOUT ANY WARRANTY; without even the implied warranty of
46+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47+# GNU Affero General Public License for more details.
48+#
49+# You should have received a copy of the GNU Affero General Public License
50+# along with this program. If not, see <http://www.gnu.org/licenses/>.
51+#
52+##############################################################################
53+
54+{
55+ 'name': 'Configuration Helper',
56+ 'version': '0.8',
57+ 'author': 'Akretion',
58+ 'maintainer': 'Akretion',
59+ 'category': 'server',
60+ 'complexity': 'normal',
61+ 'depends': ['base'],
62+ 'description': """
63+Configuration Helper
64+====================
65+
66+*This module is intended for developer only. It does nothing used alone.*
67+
68+This module :
69+
70+ * create automatically related fields in 'whatiwant.config.settings'
71+ using those defined in 'res.company' : it avoid duplicated field definitions.
72+ * company_id field with default value is created
73+ * onchange_company_id is defined to update all related fields
74+ * supported fields: char, text, integer, float, datetime, date, boolean, m2o
75+
76+
77+How to use
78+----------
79+
80+.. code-block:: python
81+
82+ from . company import ResCompany
83+
84+ class WhatiwantClassSettings(orm.TransientModel):
85+ _inherit = ['res.config.settings', 'abstract.config.settings']
86+ _name = 'whatiwant.config.settings'
87+ # fields must be defined in ResCompany class
88+ # related fields are automatically generated from previous definitions
89+ _companyObject = ResCompany
90+
91+
92+Roadmap
93+-------
94+ * support (or check support) for these field types : o2m, m2m, reference, property, selection
95+ * automatically generate a default view for 'whatiwant.config.settings' (in --debug ?)
96+
97+
98+Contributors
99+------------
100+
101+* David BEAL <david.beal@akretion.com>
102+* Sébastien BEAU <sebastien.beau@akretion.com>
103+* Yannick Vaucher, Camptocamp, (code refactoring from his module 'delivery_carrier_label_postlogistics')
104+
105+ """,
106+ 'website': 'http://www.akretion.com/',
107+ 'data': [
108+ ],
109+ 'tests': [],
110+ 'installable': True,
111+ 'auto_install': False,
112+ 'license': 'AGPL-3',
113+ 'application': True,
114+}
115
116=== added file 'configuration_helper/config.py'
117--- configuration_helper/config.py 1970-01-01 00:00:00 +0000
118+++ configuration_helper/config.py 2014-05-23 12:54:31 +0000
119@@ -0,0 +1,114 @@
120+# -*- coding: utf-8 -*-
121+##############################################################################
122+#
123+# Author: David BEAL, Copyright 2014 Akretion
124+#
125+# This program is free software: you can redistribute it and/or modify
126+# it under the terms of the GNU Affero General Public License as
127+# published by the Free Software Foundation, either version 3 of the
128+# License, or (at your option) any later version.
129+#
130+# This program is distributed in the hope that it will be useful,
131+# but WITHOUT ANY WARRANTY; without even the implied warranty of
132+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133+# GNU Affero General Public License for more details.
134+#
135+# You should have received a copy of the GNU Affero General Public License
136+# along with this program. If not, see <http://www.gnu.org/licenses/>.
137+#
138+##############################################################################
139+import re
140+
141+from openerp.osv import orm, fields
142+
143+
144+class AbstractConfigSettings(orm.AbstractModel):
145+ _name = 'abstract.config.settings'
146+ _description = 'Abstract configuration settings'
147+ # prefix field name to differentiate fields in company with those in config
148+ _prefix = 'setting_'
149+ # this is the class name to import in your module
150+ # (it should be ResCompany or res_company, depends of your code)
151+ _companyObject = None
152+
153+ def _filter_field(self, field_key):
154+ """Inherit in your module to define for which company field
155+ you don't want have a matching related field"""
156+ return True
157+
158+ def __init__(self, pool, cr):
159+ super(AbstractConfigSettings, self).__init__(pool, cr)
160+ if self._companyObject:
161+ for field_key in self._companyObject._columns:
162+ #allows to exclude some field
163+ if self._filter_field(field_key):
164+ args = ('company_id', field_key)
165+ kwargs = {
166+ 'string': self._companyObject._columns[field_key].string,
167+ 'help': self._companyObject._columns[field_key].help,
168+ 'type': self._companyObject._columns[field_key]._type,
169+ }
170+ if '_obj' in self._companyObject._columns[field_key].__dict__.keys():
171+ kwargs['relation'] = \
172+ self._companyObject._columns[field_key]._obj
173+ if '_domain' in \
174+ self._companyObject._columns[field_key].__dict__.keys():
175+ kwargs['domain'] = \
176+ self._companyObject._columns[field_key]._domain
177+ field_key = re.sub('^' + self._prefix, '', field_key)
178+ self._columns[field_key] = \
179+ fields.related(*args, **kwargs)
180+
181+ _columns = {
182+ 'company_id': fields.many2one(
183+ 'res.company',
184+ 'Company',
185+ required=True),
186+ }
187+
188+ def _default_company(self, cr, uid, context=None):
189+ user = self.pool['res.users'].browse(cr, uid, uid, context=context)
190+ return user.company_id.id
191+
192+ _defaults = {
193+ 'company_id': _default_company,
194+ }
195+
196+ def field_to_populate_as_related(self, cr, uid, field, company_cols, context=None):
197+ """Only fields which comes from company with the right prefix
198+ must be defined as related"""
199+ if self._prefix + field in company_cols:
200+ return True
201+ return False
202+
203+ def onchange_company_id(self, cr, uid, ids, company_id, context=None):
204+ " update related fields "
205+ values = {}
206+ values['currency_id'] = False
207+ if not company_id:
208+ return {'value': values}
209+ company_m = self.pool['res.company']
210+ company = company_m.browse(
211+ cr, uid, company_id, context=context)
212+ company_cols = company_m._columns.keys()
213+ for field in self._columns:
214+ if self.field_to_populate_as_related(
215+ cr, uid, field, company_cols, context=context):
216+ cpny_field = self._columns[field].arg[-1]
217+ if self._columns[field]._type == 'many2one':
218+ values[field] = company[cpny_field]['id'] or False
219+ else:
220+ values[field] = company[cpny_field]
221+ return {'value': values}
222+
223+ def create(self, cr, uid, values, context=None):
224+ id = super(AbstractConfigSettings, self).create(
225+ cr, uid, values, context=context)
226+ # Hack: to avoid some nasty bug, related fields are not written
227+ # upon record creation. Hence we write on those fields here.
228+ vals = {}
229+ for fname, field in self._columns.iteritems():
230+ if isinstance(field, fields.related) and fname in values:
231+ vals[fname] = values[fname]
232+ self.write(cr, uid, [id], vals, context)
233+ return id