Merge branch 'master' of https://github.com/odoo/odoo
[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 from operator import itemgetter
23 import time
24
25 from openerp import models, api
26 from openerp.osv import osv, orm, fields
27 from openerp.tools.misc import attrgetter
28
29 # -------------------------------------------------------------------------
30 # Properties
31 # -------------------------------------------------------------------------
32
33 TYPE2FIELD = {
34     'char': 'value_text',
35     'float': 'value_float',
36     'boolean': 'value_integer',
37     'integer': 'value_integer',
38     'text': 'value_text',
39     'binary': 'value_binary',
40     'many2one': 'value_reference',
41     'date': 'value_datetime',
42     'datetime': 'value_datetime',
43     'selection': 'value_text',
44 }
45
46 class ir_property(osv.osv):
47     _name = 'ir.property'
48
49     _columns = {
50         'name': fields.char('Name', select=1),
51
52         'res_id': fields.char('Resource', help="If not set, acts as a default value for new resources", select=1),
53         'company_id': fields.many2one('res.company', 'Company', select=1),
54         'fields_id': fields.many2one('ir.model.fields', 'Field', ondelete='cascade', required=True, select=1),
55
56         'value_float' : fields.float('Value'),
57         'value_integer' : fields.integer('Value'),
58         'value_text' : fields.text('Value'), # will contain (char, text)
59         'value_binary' : fields.binary('Value'),
60         'value_reference': fields.char('Value'),
61         'value_datetime' : fields.datetime('Value'),
62
63         'type' : fields.selection([('char', 'Char'),
64                                    ('float', 'Float'),
65                                    ('boolean', 'Boolean'),
66                                    ('integer', 'Integer'),
67                                    ('text', 'Text'),
68                                    ('binary', 'Binary'),
69                                    ('many2one', 'Many2One'),
70                                    ('date', 'Date'),
71                                    ('datetime', 'DateTime'),
72                                    ('selection', 'Selection'),
73                                   ],
74                                   'Type',
75                                   required=True,
76                                   select=1),
77     }
78
79     _defaults = {
80         'type': 'many2one',
81     }
82
83     def _update_values(self, cr, uid, ids, values):
84         value = values.pop('value', None)
85         if not value:
86             return values
87
88         prop = None
89         type_ = values.get('type')
90         if not type_:
91             if ids:
92                 prop = self.browse(cr, uid, ids[0])
93                 type_ = prop.type
94             else:
95                 type_ = self._defaults['type']
96
97         field = TYPE2FIELD.get(type_)
98         if not field:
99             raise osv.except_osv('Error', 'Invalid type')
100
101         if field == 'value_reference':
102             if isinstance(value, orm.BaseModel):
103                 value = '%s,%d' % (value._name, value.id)
104             elif isinstance(value, (int, long)):
105                 field_id = values.get('fields_id')
106                 if not field_id:
107                     if not prop:
108                         raise ValueError()
109                     field_id = prop.fields_id
110                 else:
111                     field_id = self.pool.get('ir.model.fields').browse(cr, uid, field_id)
112
113                 value = '%s,%d' % (field_id.relation, value)
114
115         values[field] = value
116         return values
117
118     def write(self, cr, uid, ids, values, context=None):
119         return super(ir_property, self).write(cr, uid, ids, self._update_values(cr, uid, ids, values), context=context)
120
121     def create(self, cr, uid, values, context=None):
122         return super(ir_property, self).create(cr, uid, self._update_values(cr, uid, None, values), context=context)
123
124     def get_by_record(self, cr, uid, record, context=None):
125         if record.type in ('char', 'text', 'selection'):
126             return record.value_text
127         elif record.type == 'float':
128             return record.value_float
129         elif record.type == 'boolean':
130             return bool(record.value_integer)
131         elif record.type == 'integer':
132             return record.value_integer
133         elif record.type == 'binary':
134             return record.value_binary
135         elif record.type == 'many2one':
136             if not record.value_reference:
137                 return False
138             model, resource_id = record.value_reference.split(',')
139             value = self.pool[model].browse(cr, uid, int(resource_id), context=context)
140             return value.exists()
141         elif record.type == 'datetime':
142             return record.value_datetime
143         elif record.type == 'date':
144             if not record.value_datetime:
145                 return False
146             return time.strftime('%Y-%m-%d', time.strptime(record.value_datetime, '%Y-%m-%d %H:%M:%S'))
147         return False
148
149     def get(self, cr, uid, name, model, res_id=False, context=None):
150         domain = self._get_domain(cr, uid, name, model, context=context)
151         if domain is not None:
152             domain = [('res_id', '=', res_id)] + domain
153             #make the search with company_id asc to make sure that properties specific to a company are given first
154             nid = self.search(cr, uid, domain, limit=1, order='company_id asc', context=context)
155             if not nid: return False
156             record = self.browse(cr, uid, nid[0], context=context)
157             return self.get_by_record(cr, uid, record, context=context)
158         return False
159
160     def _get_domain(self, cr, uid, prop_name, model, context=None):
161         context = context or {}
162         cr.execute('select id from ir_model_fields where name=%s and model=%s', (prop_name, model))
163         res = cr.fetchone()
164         if not res:
165             return None
166
167         cid = context.get('force_company')
168         if not cid:
169             company = self.pool.get('res.company')
170             cid = company._company_default_get(cr, uid, model, res[0], context=context)
171
172         return [('fields_id', '=', res[0]), ('company_id', 'in', [cid, False])]
173
174     @api.model
175     def get_multi(self, name, model, ids):
176         """ Read the property field `name` for the records of model `model` with
177             the given `ids`, and return a dictionary mapping `ids` to their
178             corresponding value.
179         """
180         if not ids:
181             return {}
182
183         domain = self._get_domain(name, model)
184         if domain is None:
185             return dict.fromkeys(ids, False)
186
187         # retrieve the values for the given ids and the default value, too
188         refs = {('%s,%s' % (model, id)): id for id in ids}
189         refs[False] = False
190         domain += [('res_id', 'in', list(refs))]
191
192         # note: order by 'company_id asc' will return non-null values first
193         props = self.search(domain, order='company_id asc')
194         result = {}
195         for prop in props:
196             # for a given res_id, take the first property only
197             id = refs.pop(prop.res_id, None)
198             if id is not None:
199                 result[id] = self.get_by_record(prop)
200
201         # set the default value to the ids that are not in result
202         default_value = result.pop(False, False)
203         for id in ids:
204             result.setdefault(id, default_value)
205
206         return result
207
208     @api.model
209     def set_multi(self, name, model, values):
210         """ Assign the property field `name` for the records of model `model`
211             with `values` (dictionary mapping record ids to their value).
212         """
213         def clean(value):
214             return value.id if isinstance(value, models.BaseModel) else value
215
216         if not values:
217             return
218
219         domain = self._get_domain(name, model)
220         if domain is None:
221             raise Exception()
222
223         # retrieve the default value for the field
224         default_value = clean(self.get(name, model))
225
226         # retrieve the properties corresponding to the given record ids
227         self._cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", (name, model))
228         field_id = self._cr.fetchone()[0]
229         company_id = self.env['res.company']._company_default_get(model, field_id)
230         refs = {('%s,%s' % (model, id)): id for id in values}
231         props = self.search([
232             ('fields_id', '=', field_id),
233             ('company_id', '=', company_id),
234             ('res_id', 'in', list(refs)),
235         ])
236
237         # modify existing properties
238         for prop in props:
239             id = refs.pop(prop.res_id)
240             value = clean(values[id])
241             if value == default_value:
242                 prop.unlink()
243             elif value != clean(prop.get_by_record(prop)):
244                 prop.write({'value': value})
245
246         # create new properties for records that do not have one yet
247         for ref, id in refs.iteritems():
248             value = clean(values[id])
249             if value != default_value:
250                 self.create({
251                     'fields_id': field_id,
252                     'company_id': company_id,
253                     'res_id': ref,
254                     'name': name,
255                     'value': value,
256                     'type': self.env[model]._fields[name].type,
257                 })
258
259     @api.model
260     def search_multi(self, name, model, operator, value):
261         """ Return a domain for the records that match the given condition. """
262         field = self.env[model]._fields[name]
263         if field.type == 'many2one':
264             comodel = field.comodel_name
265             def makeref(value):
266                 return value and '%s,%s' % (comodel, value)
267             if operator in ('=', '!=', '<=', '<', '>', '>='):
268                 value = makeref(value)
269             elif operator in ('in', 'not in'):
270                 value = map(makeref, value)
271             elif operator in ('=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike'):
272                 # most probably inefficient... but correct
273                 target = self.env[comodel]
274                 target_names = target.name_search(value, operator=operator, limit=None)
275                 target_ids = map(itemgetter(0), target_names)
276                 operator, value = 'in', map(makeref, target_ids)
277
278         # retrieve the properties that match the condition
279         domain = self._get_domain(name, model)
280         if domain is None:
281             raise Exception()
282         props = self.search(domain + [(TYPE2FIELD[field.type], operator, value)])
283
284         # retrieve the records corresponding to the properties that match
285         good_ids = []
286         default_matches = False
287         for prop in props:
288             if prop.res_id:
289                 res_model, res_id = prop.res_id.split(',')
290                 good_ids.append(int(res_id))
291             else:
292                 default_matches = True
293
294         if default_matches:
295             # exclude all records with a property that does not match
296             all_ids = []
297             props = self.search(domain + [('res_id', '!=', False)])
298             for prop in props:
299                 res_model, res_id = prop.res_id.split(',')
300                 all_ids.append(int(res_id))
301             bad_ids = list(set(all_ids) - set(good_ids))
302             return [('id', 'not in', bad_ids)]
303         else:
304             return [('id', 'in', good_ids)]
305
306 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: