2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
2009-10-14 12:32:15 +00:00
|
|
|
#
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2008-06-16 11:00:21 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2009-10-14 12:32:15 +00:00
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
# License, or (at your option) any later version.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2008-11-03 18:27:16 +00:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-10-14 12:32:15 +00:00
|
|
|
# GNU Affero General Public License for more details.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
2009-10-14 12:32:15 +00:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
from operator import itemgetter
|
2010-05-05 10:02:41 +00:00
|
|
|
import time
|
2010-09-24 10:15:45 +00:00
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
from openerp import models, api
|
|
|
|
from openerp.osv import osv, orm, fields
|
2012-12-10 15:27:23 +00:00
|
|
|
from openerp.tools.misc import attrgetter
|
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Properties
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
TYPE2FIELD = {
|
|
|
|
'char': 'value_text',
|
|
|
|
'float': 'value_float',
|
|
|
|
'boolean': 'value_integer',
|
|
|
|
'integer': 'value_integer',
|
|
|
|
'text': 'value_text',
|
|
|
|
'binary': 'value_binary',
|
|
|
|
'many2one': 'value_reference',
|
|
|
|
'date': 'value_datetime',
|
|
|
|
'datetime': 'value_datetime',
|
|
|
|
'selection': 'value_text',
|
|
|
|
}
|
|
|
|
|
2006-12-07 13:41:40 +00:00
|
|
|
class ir_property(osv.osv):
|
2008-07-22 14:24:36 +00:00
|
|
|
_name = 'ir.property'
|
2010-05-04 14:46:42 +00:00
|
|
|
|
2008-07-22 14:24:36 +00:00
|
|
|
_columns = {
|
2014-05-21 09:52:05 +00:00
|
|
|
'name': fields.char('Name', select=1),
|
2010-05-05 10:02:41 +00:00
|
|
|
|
2014-05-21 09:52:05 +00:00
|
|
|
'res_id': fields.char('Resource', help="If not set, acts as a default value for new resources", select=1),
|
2010-09-21 07:30:57 +00:00
|
|
|
'company_id': fields.many2one('res.company', 'Company', select=1),
|
2010-09-21 11:00:38 +00:00
|
|
|
'fields_id': fields.many2one('ir.model.fields', 'Field', ondelete='cascade', required=True, select=1),
|
2010-05-05 10:02:41 +00:00
|
|
|
|
2010-09-21 11:00:38 +00:00
|
|
|
'value_float' : fields.float('Value'),
|
2012-03-22 16:58:37 +00:00
|
|
|
'value_integer' : fields.integer('Value'),
|
2010-09-21 11:00:38 +00:00
|
|
|
'value_text' : fields.text('Value'), # will contain (char, text)
|
|
|
|
'value_binary' : fields.binary('Value'),
|
2014-05-21 09:52:05 +00:00
|
|
|
'value_reference': fields.char('Value'),
|
2010-09-21 11:00:38 +00:00
|
|
|
'value_datetime' : fields.datetime('Value'),
|
2010-05-05 10:02:41 +00:00
|
|
|
|
|
|
|
'type' : fields.selection([('char', 'Char'),
|
|
|
|
('float', 'Float'),
|
2010-06-09 11:25:38 +00:00
|
|
|
('boolean', 'Boolean'),
|
2010-05-05 10:02:41 +00:00
|
|
|
('integer', 'Integer'),
|
|
|
|
('text', 'Text'),
|
|
|
|
('binary', 'Binary'),
|
|
|
|
('many2one', 'Many2One'),
|
|
|
|
('date', 'Date'),
|
|
|
|
('datetime', 'DateTime'),
|
2013-06-24 08:53:11 +00:00
|
|
|
('selection', 'Selection'),
|
2010-05-05 10:02:41 +00:00
|
|
|
],
|
|
|
|
'Type',
|
|
|
|
required=True,
|
|
|
|
select=1),
|
2008-07-22 14:24:36 +00:00
|
|
|
}
|
2006-12-28 14:41:28 +00:00
|
|
|
|
2010-05-05 10:02:41 +00:00
|
|
|
_defaults = {
|
|
|
|
'type': 'many2one',
|
|
|
|
}
|
|
|
|
|
|
|
|
def _update_values(self, cr, uid, ids, values):
|
|
|
|
value = values.pop('value', None)
|
|
|
|
if not value:
|
|
|
|
return values
|
2010-06-10 16:20:17 +00:00
|
|
|
|
|
|
|
prop = None
|
|
|
|
type_ = values.get('type')
|
|
|
|
if not type_:
|
2010-06-10 19:50:53 +00:00
|
|
|
if ids:
|
|
|
|
prop = self.browse(cr, uid, ids[0])
|
|
|
|
type_ = prop.type
|
|
|
|
else:
|
|
|
|
type_ = self._defaults['type']
|
2010-06-10 16:20:17 +00:00
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
field = TYPE2FIELD.get(type_)
|
2010-05-05 10:02:41 +00:00
|
|
|
if not field:
|
|
|
|
raise osv.except_osv('Error', 'Invalid type')
|
|
|
|
|
|
|
|
if field == 'value_reference':
|
2014-07-06 14:44:26 +00:00
|
|
|
if isinstance(value, orm.BaseModel):
|
2010-05-05 10:02:41 +00:00
|
|
|
value = '%s,%d' % (value._name, value.id)
|
|
|
|
elif isinstance(value, (int, long)):
|
|
|
|
field_id = values.get('fields_id')
|
|
|
|
if not field_id:
|
2010-06-10 16:20:17 +00:00
|
|
|
if not prop:
|
2010-05-05 10:02:41 +00:00
|
|
|
raise ValueError()
|
2010-06-10 16:20:17 +00:00
|
|
|
field_id = prop.fields_id
|
2010-05-05 10:02:41 +00:00
|
|
|
else:
|
|
|
|
field_id = self.pool.get('ir.model.fields').browse(cr, uid, field_id)
|
|
|
|
|
|
|
|
value = '%s,%d' % (field_id.relation, value)
|
|
|
|
|
|
|
|
values[field] = value
|
|
|
|
return values
|
|
|
|
|
|
|
|
def write(self, cr, uid, ids, values, context=None):
|
|
|
|
return super(ir_property, self).write(cr, uid, ids, self._update_values(cr, uid, ids, values), context=context)
|
|
|
|
|
|
|
|
def create(self, cr, uid, values, context=None):
|
|
|
|
return super(ir_property, self).create(cr, uid, self._update_values(cr, uid, None, values), context=context)
|
|
|
|
|
2010-09-18 10:19:58 +00:00
|
|
|
def get_by_record(self, cr, uid, record, context=None):
|
2013-06-24 08:53:11 +00:00
|
|
|
if record.type in ('char', 'text', 'selection'):
|
2010-05-05 10:02:41 +00:00
|
|
|
return record.value_text
|
|
|
|
elif record.type == 'float':
|
|
|
|
return record.value_float
|
2010-06-09 11:25:38 +00:00
|
|
|
elif record.type == 'boolean':
|
|
|
|
return bool(record.value_integer)
|
2012-03-22 16:38:50 +00:00
|
|
|
elif record.type == 'integer':
|
2010-05-05 10:02:41 +00:00
|
|
|
return record.value_integer
|
|
|
|
elif record.type == 'binary':
|
|
|
|
return record.value_binary
|
|
|
|
elif record.type == 'many2one':
|
2013-06-24 08:50:49 +00:00
|
|
|
if not record.value_reference:
|
2014-07-06 14:44:26 +00:00
|
|
|
return False
|
2013-06-24 08:50:49 +00:00
|
|
|
model, resource_id = record.value_reference.split(',')
|
2014-07-06 14:44:26 +00:00
|
|
|
value = self.pool[model].browse(cr, uid, int(resource_id), context=context)
|
|
|
|
return value.exists()
|
2010-05-05 10:02:41 +00:00
|
|
|
elif record.type == 'datetime':
|
|
|
|
return record.value_datetime
|
|
|
|
elif record.type == 'date':
|
2010-06-09 11:25:38 +00:00
|
|
|
if not record.value_datetime:
|
|
|
|
return False
|
2010-05-05 10:02:41 +00:00
|
|
|
return time.strftime('%Y-%m-%d', time.strptime(record.value_datetime, '%Y-%m-%d %H:%M:%S'))
|
|
|
|
return False
|
|
|
|
|
2011-11-07 15:19:49 +00:00
|
|
|
def get(self, cr, uid, name, model, res_id=False, context=None):
|
2010-05-05 10:02:41 +00:00
|
|
|
domain = self._get_domain(cr, uid, name, model, context=context)
|
|
|
|
if domain is not None:
|
|
|
|
domain = [('res_id', '=', res_id)] + domain
|
2013-06-24 08:57:31 +00:00
|
|
|
#make the search with company_id asc to make sure that properties specific to a company are given first
|
|
|
|
nid = self.search(cr, uid, domain, limit=1, order='company_id asc', context=context)
|
2010-09-19 12:34:45 +00:00
|
|
|
if not nid: return False
|
|
|
|
record = self.browse(cr, uid, nid[0], context=context)
|
2011-01-10 17:29:49 +00:00
|
|
|
return self.get_by_record(cr, uid, record, context=context)
|
2008-07-22 14:24:36 +00:00
|
|
|
return False
|
2010-05-05 10:02:41 +00:00
|
|
|
|
|
|
|
def _get_domain(self, cr, uid, prop_name, model, context=None):
|
2010-06-21 21:16:27 +00:00
|
|
|
context = context or {}
|
2010-05-05 10:02:41 +00:00
|
|
|
cr.execute('select id from ir_model_fields where name=%s and model=%s', (prop_name, model))
|
|
|
|
res = cr.fetchone()
|
|
|
|
if not res:
|
|
|
|
return None
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
cid = context.get('force_company')
|
|
|
|
if not cid:
|
2010-06-10 13:38:40 +00:00
|
|
|
company = self.pool.get('res.company')
|
2010-06-21 21:16:27 +00:00
|
|
|
cid = company._company_default_get(cr, uid, model, res[0], context=context)
|
2010-05-05 10:02:41 +00:00
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
return [('fields_id', '=', res[0]), ('company_id', 'in', [cid, False])]
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def get_multi(self, name, model, ids):
|
|
|
|
""" Read the property field `name` for the records of model `model` with
|
|
|
|
the given `ids`, and return a dictionary mapping `ids` to their
|
|
|
|
corresponding value.
|
|
|
|
"""
|
|
|
|
if not ids:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
domain = self._get_domain(name, model)
|
|
|
|
if domain is None:
|
|
|
|
return dict.fromkeys(ids, False)
|
|
|
|
|
|
|
|
# retrieve the values for the given ids and the default value, too
|
|
|
|
refs = {('%s,%s' % (model, id)): id for id in ids}
|
|
|
|
refs[False] = False
|
|
|
|
domain += [('res_id', 'in', list(refs))]
|
|
|
|
|
|
|
|
# note: order by 'company_id asc' will return non-null values first
|
|
|
|
props = self.search(domain, order='company_id asc')
|
|
|
|
result = {}
|
|
|
|
for prop in props:
|
|
|
|
# for a given res_id, take the first property only
|
|
|
|
id = refs.pop(prop.res_id, None)
|
|
|
|
if id is not None:
|
|
|
|
result[id] = self.get_by_record(prop)
|
|
|
|
|
|
|
|
# set the default value to the ids that are not in result
|
|
|
|
default_value = result.pop(False, False)
|
|
|
|
for id in ids:
|
|
|
|
result.setdefault(id, default_value)
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def set_multi(self, name, model, values):
|
|
|
|
""" Assign the property field `name` for the records of model `model`
|
|
|
|
with `values` (dictionary mapping record ids to their value).
|
|
|
|
"""
|
|
|
|
def clean(value):
|
|
|
|
return value.id if isinstance(value, models.BaseModel) else value
|
|
|
|
|
|
|
|
if not values:
|
|
|
|
return
|
|
|
|
|
|
|
|
domain = self._get_domain(name, model)
|
|
|
|
if domain is None:
|
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
# retrieve the default value for the field
|
|
|
|
default_value = clean(self.get(name, model))
|
|
|
|
|
|
|
|
# retrieve the properties corresponding to the given record ids
|
|
|
|
self._cr.execute("SELECT id FROM ir_model_fields WHERE name=%s AND model=%s", (name, model))
|
|
|
|
field_id = self._cr.fetchone()[0]
|
2015-05-11 10:10:47 +00:00
|
|
|
company_id = self.env.context.get('force_company') or self.env['res.company']._company_default_get(model, field_id)
|
2014-07-06 14:44:26 +00:00
|
|
|
refs = {('%s,%s' % (model, id)): id for id in values}
|
|
|
|
props = self.search([
|
|
|
|
('fields_id', '=', field_id),
|
|
|
|
('company_id', '=', company_id),
|
|
|
|
('res_id', 'in', list(refs)),
|
|
|
|
])
|
|
|
|
|
|
|
|
# modify existing properties
|
|
|
|
for prop in props:
|
|
|
|
id = refs.pop(prop.res_id)
|
|
|
|
value = clean(values[id])
|
|
|
|
if value == default_value:
|
|
|
|
prop.unlink()
|
|
|
|
elif value != clean(prop.get_by_record(prop)):
|
|
|
|
prop.write({'value': value})
|
|
|
|
|
|
|
|
# create new properties for records that do not have one yet
|
|
|
|
for ref, id in refs.iteritems():
|
|
|
|
value = clean(values[id])
|
|
|
|
if value != default_value:
|
|
|
|
self.create({
|
|
|
|
'fields_id': field_id,
|
|
|
|
'company_id': company_id,
|
|
|
|
'res_id': ref,
|
|
|
|
'name': name,
|
|
|
|
'value': value,
|
|
|
|
'type': self.env[model]._fields[name].type,
|
|
|
|
})
|
|
|
|
|
|
|
|
@api.model
|
|
|
|
def search_multi(self, name, model, operator, value):
|
|
|
|
""" Return a domain for the records that match the given condition. """
|
[FIX] search: searching fields.property not set
When searching if a many2one property field is not set, there may be less
results since only the ones with a reference set to NULL are returned.
We should also get those not in the table.
This commit change this case so instead of returning ['id', 'in', {matching non-set ids}],
the ['id', 'not in', {matching set ids}] is returned.
e.g: if (1, 3, 8) are set, (5, 9) are not set. ['id', 'not in', (1, 3, 8)] would
be returned instead of ['id', 'in', (5, 9)] which might not select all non-set
property fields.
closes #6044
opw-631057
2015-03-30 10:14:28 +00:00
|
|
|
default_matches = False
|
|
|
|
|
2014-07-06 14:44:26 +00:00
|
|
|
field = self.env[model]._fields[name]
|
|
|
|
if field.type == 'many2one':
|
|
|
|
comodel = field.comodel_name
|
|
|
|
def makeref(value):
|
|
|
|
return value and '%s,%s' % (comodel, value)
|
[FIX] search: searching fields.property not set
When searching if a many2one property field is not set, there may be less
results since only the ones with a reference set to NULL are returned.
We should also get those not in the table.
This commit change this case so instead of returning ['id', 'in', {matching non-set ids}],
the ['id', 'not in', {matching set ids}] is returned.
e.g: if (1, 3, 8) are set, (5, 9) are not set. ['id', 'not in', (1, 3, 8)] would
be returned instead of ['id', 'in', (5, 9)] which might not select all non-set
property fields.
closes #6044
opw-631057
2015-03-30 10:14:28 +00:00
|
|
|
if operator == "=":
|
|
|
|
value = makeref(value)
|
|
|
|
# if searching properties not set, search those not in those set
|
|
|
|
if value is False:
|
|
|
|
default_matches = True
|
|
|
|
elif operator in ('!=', '<=', '<', '>', '>='):
|
2014-07-06 14:44:26 +00:00
|
|
|
value = makeref(value)
|
|
|
|
elif operator in ('in', 'not in'):
|
|
|
|
value = map(makeref, value)
|
|
|
|
elif operator in ('=like', '=ilike', 'like', 'not like', 'ilike', 'not ilike'):
|
|
|
|
# most probably inefficient... but correct
|
|
|
|
target = self.env[comodel]
|
|
|
|
target_names = target.name_search(value, operator=operator, limit=None)
|
|
|
|
target_ids = map(itemgetter(0), target_names)
|
|
|
|
operator, value = 'in', map(makeref, target_ids)
|
|
|
|
|
|
|
|
# retrieve the properties that match the condition
|
|
|
|
domain = self._get_domain(name, model)
|
|
|
|
if domain is None:
|
|
|
|
raise Exception()
|
|
|
|
props = self.search(domain + [(TYPE2FIELD[field.type], operator, value)])
|
|
|
|
|
|
|
|
# retrieve the records corresponding to the properties that match
|
|
|
|
good_ids = []
|
|
|
|
for prop in props:
|
|
|
|
if prop.res_id:
|
|
|
|
res_model, res_id = prop.res_id.split(',')
|
|
|
|
good_ids.append(int(res_id))
|
|
|
|
else:
|
|
|
|
default_matches = True
|
|
|
|
|
|
|
|
if default_matches:
|
|
|
|
# exclude all records with a property that does not match
|
|
|
|
all_ids = []
|
|
|
|
props = self.search(domain + [('res_id', '!=', False)])
|
|
|
|
for prop in props:
|
|
|
|
res_model, res_id = prop.res_id.split(',')
|
|
|
|
all_ids.append(int(res_id))
|
|
|
|
bad_ids = list(set(all_ids) - set(good_ids))
|
|
|
|
return [('id', 'not in', bad_ids)]
|
|
|
|
else:
|
|
|
|
return [('id', 'in', good_ids)]
|
2010-05-05 10:02:41 +00:00
|
|
|
|
2008-07-23 15:01:27 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|