|
| 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