2014-12-02 12:54:01 +01:00
|
|
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
|
# this repository contains the full copyright notices and license terms.
|
2015-03-15 16:26:10 +01:00
|
|
|
import warnings
|
|
|
|
|
|
2014-03-18 19:59:04 +01:00
|
|
|
from sql import Cast, Literal, Query, Expression
|
2013-09-06 22:32:37 +02:00
|
|
|
from sql.functions import Substring, Position
|
2012-09-24 22:16:53 +02:00
|
|
|
|
2019-02-13 10:37:40 +01:00
|
|
|
from trytond.pyson import PYSONEncoder
|
2018-04-03 13:20:37 +02:00
|
|
|
from .field import (Field, search_order_validate, context_validate,
|
2019-04-04 19:06:09 +02:00
|
|
|
with_inactive_records, instantiate_context)
|
2017-09-26 01:25:26 +02:00
|
|
|
from .selection import SelectionMixin
|
2013-09-06 22:32:37 +02:00
|
|
|
from ...transaction import Transaction
|
|
|
|
|
from ...pool import Pool
|
2017-02-01 16:11:00 +01:00
|
|
|
from ...rpc import RPC
|
2009-02-03 18:58:25 +01:00
|
|
|
|
|
|
|
|
|
2019-02-13 10:37:40 +01:00
|
|
|
class Reference(SelectionMixin, Field):
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
2009-09-13 15:37:11 +02:00
|
|
|
Define a reference field (``str``).
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
2009-02-03 18:58:25 +01:00
|
|
|
_type = 'reference'
|
2017-03-31 12:44:22 +02:00
|
|
|
_sql_type = 'VARCHAR'
|
2009-02-03 18:58:25 +01:00
|
|
|
|
2019-04-15 16:11:00 +02:00
|
|
|
def __init__(self, string='', selection=None, sort=True,
|
|
|
|
|
selection_change_with=None, translate=True, search_order=None,
|
|
|
|
|
search_context=None, help='', required=False, readonly=False,
|
|
|
|
|
domain=None, states=None, select=False, on_change=None,
|
|
|
|
|
on_change_with=None, depends=None, context=None, loading='lazy',
|
|
|
|
|
datetime_field=None):
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
|
|
|
|
:param selection: A list or a function name that returns a list.
|
2009-09-13 15:37:11 +02:00
|
|
|
The list must be a list of tuples. First member is an internal name
|
2009-03-01 13:37:31 +01:00
|
|
|
of model and the second is the user name of model.
|
2019-04-15 16:11:00 +02:00
|
|
|
:param sort: A boolean to sort or not the selections.
|
2016-10-21 12:52:26 +02:00
|
|
|
:param datetime_field: The name of the field that contains the datetime
|
|
|
|
|
value to read the target records.
|
2018-03-21 17:59:55 +01:00
|
|
|
:param search_order: The order to use when searching for a record
|
|
|
|
|
:param search_context: The context to use when searching for a record
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
2016-10-21 12:52:26 +02:00
|
|
|
if datetime_field:
|
|
|
|
|
if depends:
|
|
|
|
|
depends.append(datetime_field)
|
|
|
|
|
else:
|
|
|
|
|
depends = [datetime_field]
|
2009-03-01 13:37:31 +01:00
|
|
|
super(Reference, self).__init__(string=string, help=help,
|
2011-12-22 22:03:15 +01:00
|
|
|
required=required, readonly=readonly, domain=domain, states=states,
|
|
|
|
|
select=select, on_change=on_change, on_change_with=on_change_with,
|
2013-09-06 22:32:37 +02:00
|
|
|
depends=depends, context=context, loading=loading)
|
2016-10-21 12:52:26 +02:00
|
|
|
self.datetime_field = datetime_field
|
2009-03-01 13:37:31 +01:00
|
|
|
self.selection = selection or None
|
2015-03-15 16:26:10 +01:00
|
|
|
self.selection_change_with = set()
|
|
|
|
|
if selection_change_with:
|
|
|
|
|
warnings.warn('selection_change_with argument is deprecated, '
|
|
|
|
|
'use the depends decorator',
|
|
|
|
|
DeprecationWarning, stacklevel=2)
|
|
|
|
|
self.selection_change_with |= set(selection_change_with)
|
2019-04-15 16:11:00 +02:00
|
|
|
self.sort = sort
|
|
|
|
|
self.translate_selection = translate
|
2018-03-21 17:59:55 +01:00
|
|
|
self.__search_order = None
|
|
|
|
|
self.search_order = search_order
|
|
|
|
|
self.__search_context = None
|
|
|
|
|
self.search_context = search_context or {}
|
|
|
|
|
|
2009-03-01 13:37:31 +01:00
|
|
|
__init__.__doc__ += Field.__init__.__doc__
|
2009-02-03 18:58:25 +01:00
|
|
|
|
2018-03-21 17:59:55 +01:00
|
|
|
@property
|
|
|
|
|
def search_order(self):
|
|
|
|
|
return self.__search_order
|
|
|
|
|
|
|
|
|
|
@search_order.setter
|
|
|
|
|
def search_order(self, value):
|
|
|
|
|
search_order_validate(value)
|
|
|
|
|
self.__search_order = value
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def search_context(self):
|
|
|
|
|
return self.__search_context
|
|
|
|
|
|
|
|
|
|
@search_context.setter
|
|
|
|
|
def search_context(self, value):
|
|
|
|
|
context_validate(value)
|
|
|
|
|
self.__search_context = value
|
|
|
|
|
|
2017-02-01 16:11:00 +01:00
|
|
|
def set_rpc(self, model):
|
|
|
|
|
super(Reference, self).set_rpc(model)
|
|
|
|
|
if not isinstance(self.selection, (list, tuple)):
|
|
|
|
|
assert hasattr(model, self.selection), \
|
|
|
|
|
'Missing %s on model %s' % (self.selection, model.__name__)
|
|
|
|
|
instantiate = 0 if self.selection_change_with else None
|
|
|
|
|
model.__rpc__.setdefault(
|
|
|
|
|
self.selection, RPC(instantiate=instantiate))
|
|
|
|
|
|
2010-08-19 22:01:12 +02:00
|
|
|
def get(self, ids, model, name, values=None):
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
2012-09-24 22:16:53 +02:00
|
|
|
Replace removed reference id by None.
|
2009-03-01 13:37:31 +01:00
|
|
|
'''
|
2011-07-03 13:20:58 +02:00
|
|
|
pool = Pool()
|
2009-02-03 18:58:25 +01:00
|
|
|
if values is None:
|
|
|
|
|
values = {}
|
|
|
|
|
res = {}
|
|
|
|
|
for i in values:
|
|
|
|
|
res[i['id']] = i[name]
|
2010-10-06 20:09:01 +02:00
|
|
|
ref_to_check = {}
|
2009-02-03 18:58:25 +01:00
|
|
|
for i in ids:
|
|
|
|
|
if not (i in res):
|
2012-03-23 17:52:36 +01:00
|
|
|
res[i] = None
|
2009-02-03 18:58:25 +01:00
|
|
|
continue
|
|
|
|
|
if not res[i]:
|
|
|
|
|
continue
|
|
|
|
|
ref_model, ref_id = res[i].split(',', 1)
|
|
|
|
|
if not ref_model:
|
|
|
|
|
continue
|
|
|
|
|
try:
|
|
|
|
|
ref_id = int(ref_id)
|
2010-07-08 19:53:39 +02:00
|
|
|
except Exception:
|
2009-02-03 18:58:25 +01:00
|
|
|
continue
|
2011-09-20 15:40:48 +02:00
|
|
|
if ref_id < 0:
|
|
|
|
|
continue
|
2009-02-17 11:40:31 +01:00
|
|
|
res[i] = ref_model + ',' + str(ref_id)
|
2010-10-06 20:09:01 +02:00
|
|
|
ref_to_check.setdefault(ref_model, (set(), []))
|
|
|
|
|
ref_to_check[ref_model][0].add(ref_id)
|
|
|
|
|
ref_to_check[ref_model][1].append(i)
|
|
|
|
|
|
|
|
|
|
# Check if reference ids still exist
|
2014-01-31 00:02:03 +01:00
|
|
|
with Transaction().set_context(active_test=False), \
|
2014-09-17 21:43:53 +02:00
|
|
|
Transaction().set_context(_check_access=False):
|
2018-06-26 19:11:19 +02:00
|
|
|
for ref_model, (ref_ids, ids) in ref_to_check.items():
|
2012-09-24 22:16:53 +02:00
|
|
|
try:
|
|
|
|
|
pool.get(ref_model)
|
|
|
|
|
except KeyError:
|
|
|
|
|
res.update(dict((i, None) for i in ids))
|
2010-10-06 20:09:01 +02:00
|
|
|
continue
|
2012-09-24 22:16:53 +02:00
|
|
|
Ref = pool.get(ref_model)
|
|
|
|
|
refs = Ref.search([
|
2010-10-06 20:09:01 +02:00
|
|
|
('id', 'in', list(ref_ids)),
|
|
|
|
|
], order=[])
|
2018-06-26 19:11:19 +02:00
|
|
|
refs = list(map(str, refs))
|
2010-10-06 20:09:01 +02:00
|
|
|
for i in ids:
|
|
|
|
|
if res[i] not in refs:
|
2012-09-24 22:16:53 +02:00
|
|
|
res[i] = None
|
2009-02-03 18:58:25 +01:00
|
|
|
return res
|
2012-09-24 22:16:53 +02:00
|
|
|
|
|
|
|
|
def __set__(self, inst, value):
|
|
|
|
|
from ..model import Model
|
2016-02-07 16:46:16 +01:00
|
|
|
if not isinstance(value, (Model, type(None))):
|
2018-06-26 19:11:19 +02:00
|
|
|
if isinstance(value, str):
|
2014-05-10 20:28:58 +02:00
|
|
|
target, value = value.split(',')
|
2012-09-24 22:16:53 +02:00
|
|
|
else:
|
2014-05-10 20:28:58 +02:00
|
|
|
target, value = value
|
2012-09-24 22:16:53 +02:00
|
|
|
Target = Pool().get(target)
|
2019-04-04 19:06:09 +02:00
|
|
|
ctx = instantiate_context(self, inst)
|
|
|
|
|
with Transaction().set_context(ctx):
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
|
value = Target(**value)
|
|
|
|
|
else:
|
|
|
|
|
value = Target(value)
|
2012-09-24 22:16:53 +02:00
|
|
|
super(Reference, self).__set__(inst, value)
|
2013-09-06 22:32:37 +02:00
|
|
|
|
2017-03-31 12:44:22 +02:00
|
|
|
def sql_format(self, value):
|
2018-06-26 19:11:19 +02:00
|
|
|
if not isinstance(value, (str, Query, Expression)):
|
2013-09-06 22:32:37 +02:00
|
|
|
try:
|
|
|
|
|
value = '%s,%s' % tuple(value)
|
|
|
|
|
except TypeError:
|
|
|
|
|
pass
|
2017-03-31 12:44:22 +02:00
|
|
|
return super(Reference, self).sql_format(value)
|
2013-09-06 22:32:37 +02:00
|
|
|
|
2018-04-03 14:55:01 +02:00
|
|
|
@with_inactive_records
|
2013-09-06 22:32:37 +02:00
|
|
|
def convert_domain(self, domain, tables, Model):
|
|
|
|
|
if '.' not in domain[0]:
|
|
|
|
|
return super(Reference, self).convert_domain(domain, tables, Model)
|
|
|
|
|
pool = Pool()
|
|
|
|
|
name, operator, value, target = domain[:4]
|
|
|
|
|
Target = pool.get(target)
|
|
|
|
|
table, _ = tables[None]
|
|
|
|
|
name, target_name = name.split('.', 1)
|
2014-03-18 19:59:04 +01:00
|
|
|
assert name == self.name
|
|
|
|
|
column = self.sql_column(table)
|
2013-09-06 22:32:37 +02:00
|
|
|
target_domain = [(target_name,) + tuple(domain[1:3])
|
|
|
|
|
+ tuple(domain[4:])]
|
|
|
|
|
if 'active' in Target._fields:
|
|
|
|
|
target_domain.append(('active', 'in', [True, False]))
|
|
|
|
|
query = Target.search(target_domain, order=[], query=True)
|
|
|
|
|
return (Cast(Substring(column,
|
|
|
|
|
Position(',', column) + Literal(1)),
|
|
|
|
|
Model.id.sql_type().base).in_(query)
|
|
|
|
|
& column.ilike(target + ',%'))
|
2019-02-13 10:37:40 +01:00
|
|
|
|
|
|
|
|
def definition(self, model, language):
|
|
|
|
|
encoder = PYSONEncoder()
|
|
|
|
|
definition = super().definition(model, language)
|
|
|
|
|
definition['datetime_field'] = self.datetime_field
|
|
|
|
|
definition['search_context'] = encoder.encode(self.search_context)
|
|
|
|
|
definition['search_order'] = encoder.encode(self.search_order)
|
|
|
|
|
return definition
|