[IMP] use fields_get() instead of _all_columns.get(): this method has the advantage...
[odoo/odoo.git] / openerp / addons / base / res / ir_property.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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 import time
23
24 from openerp.osv import osv,fields
25 from openerp.tools.misc import attrgetter
26
27 # -------------------------------------------------------------------------
28 # Properties
29 # -------------------------------------------------------------------------
30
31 class ir_property(osv.osv):
32     _name = 'ir.property'
33
34     def _models_field_get(self, cr, uid, field_key, field_value, context=None):
35         get = attrgetter(field_key, field_value)
36         obj = self.pool.get('ir.model.fields')
37         ids = obj.search(cr, uid, [('view_load','=',1)], context=context)
38         res = set()
39         for o in obj.browse(cr, uid, ids, context=context):
40             res.add(get(o))
41         return list(res)
42
43     def _models_get(self, cr, uid, context=None):
44         return self._models_field_get(cr, uid, 'model', 'model_id.name', context)
45
46     def _models_get2(self, cr, uid, context=None):
47         return self._models_field_get(cr, uid, 'relation', 'relation', context)
48
49
50     _columns = {
51         'name': fields.char('Name', size=128, select=1),
52
53         'res_id': fields.reference('Resource', selection=_models_get, size=128,
54                                    help="If not set, acts as a default value for new resources", select=1),
55         'company_id': fields.many2one('res.company', 'Company', select=1),
56         'fields_id': fields.many2one('ir.model.fields', 'Field', ondelete='cascade', required=True, select=1),
57
58         'value_float' : fields.float('Value'),
59         'value_integer' : fields.integer('Value'),
60         'value_text' : fields.text('Value'), # will contain (char, text)
61         'value_binary' : fields.binary('Value'),
62         'value_reference': fields.reference('Value', selection=_models_get2, size=128),
63         'value_datetime' : fields.datetime('Value'),
64
65         'type' : fields.selection([('char', 'Char'),
66                                    ('float', 'Float'),
67                                    ('boolean', 'Boolean'),
68                                    ('integer', 'Integer'),
69                                    ('text', 'Text'),
70                                    ('binary', 'Binary'),
71                                    ('many2one', 'Many2One'),
72                                    ('date', 'Date'),
73                                    ('datetime', 'DateTime'),
74                                   ],
75                                   'Type',
76                                   required=True,
77                                   select=1),
78     }
79
80     _defaults = {
81         'type': 'many2one',
82     }
83
84     def _update_values(self, cr, uid, ids, values):
85         value = values.pop('value', None)
86         if not value:
87             return values
88
89         prop = None
90         type_ = values.get('type')
91         if not type_:
92             if ids:
93                 prop = self.browse(cr, uid, ids[0])
94                 type_ = prop.type
95             else:
96                 type_ = self._defaults['type']
97
98         type2field = {
99             'char': 'value_text',
100             'float': 'value_float',
101             'boolean' : 'value_integer',
102             'integer': 'value_integer',
103             'text': 'value_text',
104             'binary': 'value_binary',
105             'many2one': 'value_reference',
106             'date' : 'value_datetime',
107             'datetime' : 'value_datetime',
108         }
109         field = type2field.get(type_)
110         if not field:
111             raise osv.except_osv('Error', 'Invalid type')
112
113         if field == 'value_reference':
114             if isinstance(value, osv.orm.browse_record):
115                 value = '%s,%d' % (value._name, value.id)
116             elif isinstance(value, (int, long)):
117                 field_id = values.get('fields_id')
118                 if not field_id:
119                     if not prop:
120                         raise ValueError()
121                     field_id = prop.fields_id
122                 else:
123                     field_id = self.pool.get('ir.model.fields').browse(cr, uid, field_id)
124
125                 value = '%s,%d' % (field_id.relation, value)
126
127         values[field] = value
128         return values
129
130
131     def write(self, cr, uid, ids, values, context=None):
132         return super(ir_property, self).write(cr, uid, ids, self._update_values(cr, uid, ids, values), context=context)
133
134     def create(self, cr, uid, values, context=None):
135         return super(ir_property, self).create(cr, uid, self._update_values(cr, uid, None, values), context=context)
136
137     def get_by_record(self, cr, uid, record, context=None):
138         if record.type in ('char', 'text'):
139             return record.value_text
140         elif record.type == 'float':
141             return record.value_float
142         elif record.type == 'boolean':
143             return bool(record.value_integer)
144         elif record.type == 'integer':
145             return record.value_integer
146         elif record.type == 'binary':
147             return record.value_binary
148         elif record.type == 'many2one':
149             return record.value_reference
150         elif record.type == 'datetime':
151             return record.value_datetime
152         elif record.type == 'date':
153             if not record.value_datetime:
154                 return False
155             return time.strftime('%Y-%m-%d', time.strptime(record.value_datetime, '%Y-%m-%d %H:%M:%S'))
156         return False
157
158     def get(self, cr, uid, name, model, res_id=False, context=None):
159         domain = self._get_domain(cr, uid, name, model, context=context)
160         if domain is not None:
161             domain = [('res_id', '=', res_id)] + domain
162             nid = self.search(cr, uid, domain, context=context)
163             if not nid: return False
164             record = self.browse(cr, uid, nid[0], context=context)
165             return self.get_by_record(cr, uid, record, context=context)
166         return False
167
168     def _get_domain_default(self, cr, uid, prop_name, model, context=None):
169         domain = self._get_domain(cr, uid, prop_name, model, context=context)
170         if domain is None:
171             return None
172         return ['&', ('res_id', '=', False)] + domain
173
174     def _get_domain(self, cr, uid, prop_name, model, context=None):
175         context = context or {}
176         cr.execute('select id from ir_model_fields where name=%s and model=%s', (prop_name, model))
177         res = cr.fetchone()
178         if not res:
179             return None
180
181         if 'force_company' in context and context['force_company']:
182             cid = context['force_company']
183         else:
184             company = self.pool.get('res.company')
185             cid = company._company_default_get(cr, uid, model, res[0], context=context)
186
187         domain = ['&', ('fields_id', '=', res[0]),
188                   '|', ('company_id', '=', cid), ('company_id', '=', False)]
189         return domain
190
191 ir_property()
192
193
194
195 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
196