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

87 lines
3.1 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.
from trytond.model.fields.field import Field
2009-09-28 11:21:11 +02:00
from trytond.tools import safe_eval
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,
readonly=False, domain=None, states=None, priority=0,
change_default=False, select=0, on_change=None,
2009-03-01 13:37:31 +01:00
on_change_with=None, depends=None, order_field=None, context=None):
'''
: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,
states=states, priority=priority,
change_default=change_default, select=select,
on_change=on_change, on_change_with=on_change_with,
depends=depends, order_field=order_field, context=context)
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
def get(self, cursor, user, ids, model, name, values=None, context=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 cursor: the database cursor
:param user: the user id
: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
:param context: the context
:return: a dictionary with ids as key and values as value
'''
2009-02-03 18:58:25 +01:00
if context is None:
context = {}
if values is None:
values = {}
res = {}
for i in values:
res[i['id']] = i[name]
ref_id_found = {}
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
if ref_model not in model.pool.object_name_list():
2009-02-03 18:58:25 +01:00
continue
ref_obj = model.pool.get(ref_model)
ref_id_found.setdefault(ref_model, set())
2009-02-03 18:58:25 +01:00
try:
2009-09-28 11:21:11 +02:00
ref_id = safe_eval(ref_id)
2009-02-03 18:58:25 +01:00
except:
pass
try:
ref_id = int(ref_id)
except:
continue
ctx = context.copy()
ctx['active_test'] = False
if ref_id \
and ref_id not in ref_id_found[ref_model] \
2009-02-03 18:58:25 +01:00
and not ref_obj.search(cursor, user, [
('id', '=', ref_id),
], order=[], context=ctx):
2009-02-03 18:58:25 +01:00
ref_id = False
if ref_id:
ref_id_found[ref_model].add(ref_id)
res[i] = ref_model + ',' + str(ref_id)
2009-02-03 18:58:25 +01:00
return res