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
=== 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-23 12:54:31 +0000
@@ -0,0 +1,22 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# Author: David BEAL
5# Copyright 2014 Akretion
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21
22from . import config # noqa
023
=== added file 'configuration_helper/__openerp__.py'
--- configuration_helper/__openerp__.py 1970-01-01 00:00:00 +0000
+++ configuration_helper/__openerp__.py 2014-05-23 12:54:31 +0000
@@ -0,0 +1,82 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# Author: David BEAL
5# Copyright 2014 Akretion
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU Affero General Public License as
9# published by the Free Software Foundation, either version 3 of the
10# License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU Affero General Public License for more details.
16#
17# You should have received a copy of the GNU Affero General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20##############################################################################
21
22{
23 'name': 'Configuration Helper',
24 'version': '0.8',
25 'author': 'Akretion',
26 'maintainer': 'Akretion',
27 'category': 'server',
28 'complexity': 'normal',
29 'depends': ['base'],
30 'description': """
31Configuration Helper
32====================
33
34*This module is intended for developer only. It does nothing used alone.*
35
36This module :
37
38 * create automatically related fields in 'whatiwant.config.settings'
39 using those defined in 'res.company' : it avoid duplicated field definitions.
40 * company_id field with default value is created
41 * onchange_company_id is defined to update all related fields
42 * supported fields: char, text, integer, float, datetime, date, boolean, m2o
43
44
45How to use
46----------
47
48.. code-block:: python
49
50 from . company import ResCompany
51
52 class WhatiwantClassSettings(orm.TransientModel):
53 _inherit = ['res.config.settings', 'abstract.config.settings']
54 _name = 'whatiwant.config.settings'
55 # fields must be defined in ResCompany class
56 # related fields are automatically generated from previous definitions
57 _companyObject = ResCompany
58
59
60Roadmap
61-------
62 * support (or check support) for these field types : o2m, m2m, reference, property, selection
63 * automatically generate a default view for 'whatiwant.config.settings' (in --debug ?)
64
65
66Contributors
67------------
68
69* David BEAL <david.beal@akretion.com>
70* Sébastien BEAU <sebastien.beau@akretion.com>
71* Yannick Vaucher, Camptocamp, (code refactoring from his module 'delivery_carrier_label_postlogistics')
72
73 """,
74 'website': 'http://www.akretion.com/',
75 'data': [
76 ],
77 'tests': [],
78 'installable': True,
79 'auto_install': False,
80 'license': 'AGPL-3',
81 'application': True,
82}
083
=== added file 'configuration_helper/config.py'
--- configuration_helper/config.py 1970-01-01 00:00:00 +0000
+++ configuration_helper/config.py 2014-05-23 12:54:31 +0000
@@ -0,0 +1,114 @@
1# -*- coding: utf-8 -*-
2##############################################################################
3#
4# Author: David BEAL, Copyright 2014 Akretion
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Affero General Public License as
8# published by the Free Software Foundation, either version 3 of the
9# License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Affero General Public License for more details.
15#
16# You should have received a copy of the GNU Affero General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18#
19##############################################################################
20import re
21
22from openerp.osv import orm, fields
23
24
25class AbstractConfigSettings(orm.AbstractModel):
26 _name = 'abstract.config.settings'
27 _description = 'Abstract configuration settings'
28 # prefix field name to differentiate fields in company with those in config
29 _prefix = 'setting_'
30 # this is the class name to import in your module
31 # (it should be ResCompany or res_company, depends of your code)
32 _companyObject = None
33
34 def _filter_field(self, field_key):
35 """Inherit in your module to define for which company field
36 you don't want have a matching related field"""
37 return True
38
39 def __init__(self, pool, cr):
40 super(AbstractConfigSettings, self).__init__(pool, cr)
41 if self._companyObject:
42 for field_key in self._companyObject._columns:
43 #allows to exclude some field
44 if self._filter_field(field_key):
45 args = ('company_id', field_key)
46 kwargs = {
47 'string': self._companyObject._columns[field_key].string,
48 'help': self._companyObject._columns[field_key].help,
49 'type': self._companyObject._columns[field_key]._type,
50 }
51 if '_obj' in self._companyObject._columns[field_key].__dict__.keys():
52 kwargs['relation'] = \
53 self._companyObject._columns[field_key]._obj
54 if '_domain' in \
55 self._companyObject._columns[field_key].__dict__.keys():
56 kwargs['domain'] = \
57 self._companyObject._columns[field_key]._domain
58 field_key = re.sub('^' + self._prefix, '', field_key)
59 self._columns[field_key] = \
60 fields.related(*args, **kwargs)
61
62 _columns = {
63 'company_id': fields.many2one(
64 'res.company',
65 'Company',
66 required=True),
67 }
68
69 def _default_company(self, cr, uid, context=None):
70 user = self.pool['res.users'].browse(cr, uid, uid, context=context)
71 return user.company_id.id
72
73 _defaults = {
74 'company_id': _default_company,
75 }
76
77 def field_to_populate_as_related(self, cr, uid, field, company_cols, context=None):
78 """Only fields which comes from company with the right prefix
79 must be defined as related"""
80 if self._prefix + field in company_cols:
81 return True
82 return False
83
84 def onchange_company_id(self, cr, uid, ids, company_id, context=None):
85 " update related fields "
86 values = {}
87 values['currency_id'] = False
88 if not company_id:
89 return {'value': values}
90 company_m = self.pool['res.company']
91 company = company_m.browse(
92 cr, uid, company_id, context=context)
93 company_cols = company_m._columns.keys()
94 for field in self._columns:
95 if self.field_to_populate_as_related(
96 cr, uid, field, company_cols, context=context):
97 cpny_field = self._columns[field].arg[-1]
98 if self._columns[field]._type == 'many2one':
99 values[field] = company[cpny_field]['id'] or False
100 else:
101 values[field] = company[cpny_field]
102 return {'value': values}
103
104 def create(self, cr, uid, values, context=None):
105 id = super(AbstractConfigSettings, self).create(
106 cr, uid, values, context=context)
107 # Hack: to avoid some nasty bug, related fields are not written
108 # upon record creation. Hence we write on those fields here.
109 vals = {}
110 for fname, field in self._columns.iteritems():
111 if isinstance(field, fields.related) and fname in values:
112 vals[fname] = values[fname]
113 self.write(cr, uid, [id], vals, context)
114 return id