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

90 lines
3.4 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.
2010-08-19 22:01:12 +02:00
from __future__ import with_statement
import contextlib
2009-02-03 18:58:25 +01:00
from trytond.model.fields.field import Field
2009-09-28 11:21:11 +02:00
from trytond.tools import safe_eval
2010-08-19 22:01:12 +02:00
from trytond.transaction import Transaction
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-01-03 12:39:29 +01:00
readonly=False, domain=None, states=None, change_default=False,
select=0, 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,
required=required, readonly=readonly, domain=domain,
2011-01-03 12:39:29 +01:00
states=states, change_default=change_default, select=select,
on_change=on_change, on_change_with=on_change_with,
2011-01-07 10:58:43 +01:00
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
'''
2009-09-13 15:37:11 +02:00
Replace removed reference id by False.
2009-03-01 13:37:31 +01:00
:param ids: a list of ids
:param model: a string with the name of the model
:param name: a string with the name of the field
2009-09-13 15:37:11 +02:00
:param values: a dictionary with the read values
2009-03-01 13:37:31 +01:00
:return: a dictionary with ids as key and values as value
'''
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] = False
continue
if not res[i]:
continue
ref_model, ref_id = res[i].split(',', 1)
if not ref_model:
continue
try:
2009-09-28 11:21:11 +02:00
ref_id = safe_eval(ref_id)
2010-07-08 19:53:39 +02:00
except Exception:
2009-02-03 18:58:25 +01:00
pass
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 not ref_id:
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():
if ref_model not in model.pool.object_name_list():
res.update(dict((i, False) for i in ids))
continue
ref_obj = model.pool.get(ref_model)
ref_ids = ref_obj.search([
('id', 'in', list(ref_ids)),
], order=[])
refs = [ref_model + ',' + str(ref_id) for ref_id in ref_ids]
for i in ids:
if res[i] not in refs:
res[i] = False
2009-02-03 18:58:25 +01:00
return res