Files
tryton/trytond/model/fields/reference.py
T

95 lines
3.3 KiB
Python
Raw Normal View History

2009-02-03 18:58:25 +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.
import contextlib
2012-09-24 22:16:53 +02:00
from types import NoneType
2009-02-03 18:58:25 +01:00
from trytond.model.fields.field import Field
2010-08-19 22:01:12 +02:00
from trytond.transaction import Transaction
2011-07-03 13:20:58 +02:00
from trytond.pool import Pool
2009-02-03 18:58:25 +01:00
class Reference(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'
2009-03-01 13:37:31 +01:00
def __init__(self, string='', selection=None, help='', required=False,
2011-12-22 22:03:15 +01:00
readonly=False, domain=None, states=None, select=False,
on_change=None, on_change_with=None, depends=None,
2011-01-07 10:58:43 +01:00
order_field=None, context=None, loading='eager'):
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.
'''
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,
depends=depends, order_field=order_field, context=context,
loading=loading)
2009-03-01 13:37:31 +01:00
self.selection = selection or None
2010-03-11 16:42:50 +01:00
2009-03-01 13:37:31 +01:00
__init__.__doc__ += Field.__init__.__doc__
2009-02-03 18:58:25 +01:00
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]
ref_to_check = {}
2009-02-03 18:58:25 +01:00
for i in ids:
if not (i in res):
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
if ref_id < 0:
continue
res[i] = ref_model + ',' + str(ref_id)
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
with contextlib.nested(Transaction().set_context(active_test=False),
Transaction().set_user(0)):
for ref_model, (ref_ids, ids) in ref_to_check.iteritems():
2012-09-24 22:16:53 +02:00
try:
pool.get(ref_model)
except KeyError:
res.update(dict((i, None) for i in ids))
continue
2012-09-24 22:16:53 +02:00
Ref = pool.get(ref_model)
refs = Ref.search([
('id', 'in', list(ref_ids)),
], order=[])
2012-09-24 22:16:53 +02:00
refs = map(str, refs)
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
if not isinstance(value, (Model, NoneType)):
if isinstance(value, basestring):
target, id_ = value.split(',')
else:
target, id_ = value
Target = Pool().get(target)
value = Target(id_)
super(Reference, self).__set__(inst, value)