Skip to content

Commit 53d1445

Browse files
committed
Merge pull request #10 from bealdav/7.0-config-helper
[ADD] config helper module
2 parents cde83d1 + 982a349 commit 53d1445

File tree

3 files changed

+218
-0
lines changed

3 files changed

+218
-0
lines changed

configuration_helper/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
22+
from . import config # noqa
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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': """
31+
Configuration Helper
32+
====================
33+
34+
*This module is intended for developer only. It does nothing used alone.*
35+
36+
This 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+
45+
How 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+
60+
Roadmap
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+
66+
Contributors
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+
}

configuration_helper/config.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
##############################################################################
20+
import re
21+
22+
from openerp.osv import orm, fields
23+
24+
25+
class 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

0 commit comments

Comments
 (0)