1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from operator import itemgetter
25 from openerp import models, api
26 from openerp.osv import osv, orm, fields
27 from openerp.tools.misc import attrgetter
29 # -------------------------------------------------------------------------
31 # -------------------------------------------------------------------------
35 'float': 'value_float',
36 'boolean': 'value_integer',
37 'integer': 'value_integer',
39 'binary': 'value_binary',
40 'many2one': 'value_reference',
41 'date': 'value_datetime',
42 'datetime': 'value_datetime',
43 'selection': 'value_text',
46 class ir_property(osv.osv):
50 'name': fields.char('Name', select=1),
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),
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'),
63 'type' : fields.selection([('char', 'Char'),
65 ('boolean', 'Boolean'),
66 ('integer', 'Integer'),
69 ('many2one', 'Many2One'),
71 ('datetime', 'DateTime'),
72 ('selection', 'Selection'),
83 def _update_values(self, cr, uid, ids, values):
84 value = values.pop('value', None)
89 type_ = values.get('type')
92 prop = self.browse(cr, uid, ids[0])
95 type_ = self._defaults['type']
97 field = TYPE2FIELD.get(type_)
99 raise osv.except_osv('Error', 'Invalid type')
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')
109 field_id = prop.fields_id
111 field_id = self.pool.get('ir.model.fields').browse(cr, uid, field_id)
113 value = '%s,%d' % (field_id.relation, value)
115 values[field] = value
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)
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)
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:
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:
146 return time.strftime('%Y-%m-%d', time.strptime(record.value_datetime, '%Y-%m-%d %H:%M:%S'))
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)
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))
167 cid = context.get('force_company')
169 company = self.pool.get('res.company')
170 cid = company._company_default_get(cr, uid, model, res[0], context=context)
172 return [('fields_id', '=', res[0]), ('company_id', 'in', [cid, False])]
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
183 domain = self._get_domain(name, model)
185 return dict.fromkeys(ids, False)
187 # retrieve the values for the given ids and the default value, too
188 refs = {('%s,%s' % (model, id)): id for id in ids}
190 domain += [('res_id', 'in', list(refs))]
192 # note: order by 'company_id asc' will return non-null values first
193 props = self.search(domain, order='company_id asc')
196 # for a given res_id, take the first property only
197 id = refs.pop(prop.res_id, None)
199 result[id] = self.get_by_record(prop)
201 # set the default value to the ids that are not in result
202 default_value = result.pop(False, False)
204 result.setdefault(id, default_value)
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).
214 return value.id if isinstance(value, models.BaseModel) else value
219 domain = self._get_domain(name, model)
223 # retrieve the default value for the field
224 default_value = clean(self.get(name, model))
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)),
237 # modify existing properties
239 id = refs.pop(prop.res_id)
240 value = clean(values[id])
241 if value == default_value:
243 elif value != clean(prop.get_by_record(prop)):
244 prop.write({'value': value})
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:
251 'fields_id': field_id,
252 'company_id': company_id,
256 'type': self.env[model]._fields[name].type,
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
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)
278 # retrieve the properties that match the condition
279 domain = self._get_domain(name, model)
282 props = self.search(domain + [(TYPE2FIELD[field.type], operator, value)])
284 # retrieve the records corresponding to the properties that match
286 default_matches = False
289 res_model, res_id = prop.res_id.split(',')
290 good_ids.append(int(res_id))
292 default_matches = True
295 # exclude all records with a property that does not match
297 props = self.search(domain + [('res_id', '!=', False)])
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)]
304 return [('id', 'in', good_ids)]
306 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: