/usr/lib/python2.7/dist-packages/trytond/model/fields/many2many.py is in tryton-server 3.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | #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 itertools import chain
from sql import Cast, Literal, Column
from sql.functions import Substring, Position
from .field import Field, size_validate
from ...transaction import Transaction
from ...pool import Pool
class Many2Many(Field):
'''
Define many2many field (``list``).
'''
_type = 'many2many'
def __init__(self, relation_name, origin, target, string='', order=None,
datetime_field=None, size=None, help='', required=False,
readonly=False, domain=None, states=None, on_change=None,
on_change_with=None, depends=None, context=None, loading='lazy'):
'''
:param relation_name: The name of the relation model
or the name of the target model for ModelView only.
:param origin: The name of the field to store origin ids.
:param target: The name of the field to store target ids.
:param order: a list of tuples that are constructed like this:
``('field name', 'DESC|ASC')``
allowing to specify the order of result
:param datetime_field: The name of the field that contains the datetime
value to read the target records.
'''
if datetime_field:
if depends:
depends.append(datetime_field)
else:
depends = [datetime_field]
super(Many2Many, self).__init__(string=string, help=help,
required=required, readonly=readonly, domain=domain, states=states,
on_change=on_change, on_change_with=on_change_with,
depends=depends, context=context, loading=loading)
self.relation_name = relation_name
self.origin = origin
self.target = target
self.order = order
self.datetime_field = datetime_field
self.__size = None
self.size = size
__init__.__doc__ += Field.__init__.__doc__
def _get_size(self):
return self.__size
def _set_size(self, value):
size_validate(value)
self.__size = value
size = property(_get_size, _set_size)
def get(self, ids, model, name, values=None):
'''
Return target records ordered.
'''
if values is None:
values = {}
res = {}
if not ids:
return res
for i in ids:
res[i] = []
if self.order is None:
order = [(self.target, 'ASC')]
else:
order = self.order
Relation = Pool().get(self.relation_name)
origin_field = Relation._fields[self.origin]
relations = []
for i in range(0, len(ids), Transaction().cursor.IN_MAX):
sub_ids = ids[i:i + Transaction().cursor.IN_MAX]
if origin_field._type == 'reference':
references = ['%s,%s' % (model.__name__, x) for x in sub_ids]
clause = [(self.origin, 'in', references)]
else:
clause = [(self.origin, 'in', sub_ids)]
clause += [(self.target + '.id', '!=', None)]
relations.append(Relation.search(clause, order=order))
relations = list(chain(*relations))
for relation in relations:
origin_id = getattr(relation, self.origin).id
res[origin_id].append(getattr(relation, self.target).id)
return dict((key, tuple(value)) for key, value in res.iteritems())
def set(self, ids, model, name, values):
'''
Set the values.
values: A list of tuples:
(``create``, ``[{<field name>: value}, ...]``),
(``write``, ``<ids>``, ``{<field name>: value}``),
(``delete``, ``<ids>``),
(``delete_all``),
(``unlink``, ``<ids>``),
(``add``, ``<ids>``),
(``unlink_all``),
(``set``, ``<ids>``)
'''
pool = Pool()
if not values:
return
Relation = pool.get(self.relation_name)
Target = self.get_target()
origin_field = Relation._fields[self.origin]
def search_clause(ids):
if origin_field._type == 'reference':
references = ['%s,%s' % (model.__name__, x) for x in ids]
return (self.origin, 'in', references)
else:
return (self.origin, 'in', ids)
def field_value(record_id):
if origin_field._type == 'reference':
return '%s,%s' % (model.__name__, record_id)
else:
return record_id
for act in values:
if act[0] == 'create':
to_create = []
for record_id in ids:
for new in Target.create(act[1]):
to_create.append({
self.origin: field_value(record_id),
self.target: new.id,
})
if to_create:
Relation.create(to_create)
elif act[0] == 'write':
Target.write(Target.browse(act[1]), act[2])
elif act[0] == 'delete':
Target.delete(Target.browse(act[1]))
elif act[0] == 'delete_all':
relations = Relation.search([
search_clause(ids),
])
Target.delete([getattr(r, self.target) for r in relations
if getattr(r, self.target)])
elif act[0] == 'unlink':
if isinstance(act[1], (int, long)):
target_ids = [act[1]]
else:
target_ids = list(act[1])
if not target_ids:
continue
relations = []
for i in range(0, len(target_ids),
Transaction().cursor.IN_MAX):
sub_ids = target_ids[i:i + Transaction().cursor.IN_MAX]
relations += Relation.search([
search_clause(ids),
(self.target, 'in', sub_ids),
])
Relation.delete(relations)
elif act[0] == 'add':
target_ids = list(act[1])
if not target_ids:
continue
existing_ids = []
for i in range(0, len(target_ids),
Transaction().cursor.IN_MAX):
sub_ids = target_ids[i:i + Transaction().cursor.IN_MAX]
relations = Relation.search([
search_clause(ids),
(self.target, 'in', sub_ids),
])
for relation in relations:
existing_ids.append(getattr(relation, self.target).id)
to_create = []
for new_id in (x for x in target_ids if x not in existing_ids):
for record_id in ids:
to_create.append({
self.origin: field_value(record_id),
self.target: new_id,
})
if to_create:
Relation.create(to_create)
elif act[0] == 'unlink_all':
targets = Relation.search([
search_clause(ids),
(self.target + '.id', '!=', None),
])
Relation.delete(targets)
elif act[0] == 'set':
if not act[1]:
target_ids = []
else:
target_ids = list(act[1])
targets2 = Relation.search([
search_clause(ids),
(self.target + '.id', '!=', None),
])
Relation.delete(targets2)
to_create = []
for new_id in target_ids:
for record_id in ids:
to_create.append({
self.origin: field_value(record_id),
self.target: new_id,
})
if to_create:
Relation.create(to_create)
else:
raise Exception('Bad arguments')
def get_target(self):
'Return the target model'
Relation = Pool().get(self.relation_name)
if not self.target:
return Relation
return Relation._fields[self.target].get_target()
def __set__(self, inst, value):
Target = self.get_target()
def instance(data):
if isinstance(data, Target):
return data
elif isinstance(data, dict):
return Target(**data)
else:
return Target(data)
value = [instance(x) for x in (value or [])]
super(Many2Many, self).__set__(inst, value)
def convert_domain_child(self, domain, tables):
Target = self.get_target()
table, _ = tables[None]
name, operator, ids = domain
def get_child(ids):
if not ids:
return []
children = Target.search([
(name, 'in', ids),
(name, '!=', None),
], order=[])
child_ids = get_child([c.id for c in children])
return ids + child_ids
expression = table.id.in_(ids + get_child(ids))
if operator == 'not child_of':
return ~expression
return expression
def convert_domain(self, domain, tables, Model):
pool = Pool()
Target = self.get_target()
Relation = pool.get(self.relation_name)
relation = Relation.__table__()
table, _ = tables[None]
name, operator, value = domain[:3]
origin_field = Relation._fields[self.origin]
origin = Column(relation, self.origin)
origin_where = None
if origin_field._type == 'reference':
origin_where = origin.like(Model.__name__ + ',%')
origin = Cast(Substring(origin,
Position(',', origin) + Literal(1)),
Relation.id.sql_type().base)
if '.' not in name:
if operator in ('child_of', 'not child_of'):
if Target != Model:
query = Target.search([(domain[3], 'child_of', value)],
order=[], query=True)
where = (Column(relation, self.target).in_(query)
& (Column(relation, self.origin) != None))
if origin_where:
where &= origin_where
query = relation.select(origin, where=where)
expression = table.id.in_(query)
if operator == 'not child_of':
return ~expression
return expression
if isinstance(value, basestring):
targets = Target.search([('rec_name', 'ilike', value)],
order=[])
ids = [t.id for t in targets]
elif not isinstance(value, (list, tuple)):
ids = [value]
else:
ids = value
if not ids:
expression = table.id.in_([None])
if operator == 'not child_of':
return ~expression
return expression
else:
return self.convert_domain_child(
(name, operator, ids), tables)
if value is None:
where = origin != value
if origin_where:
where &= origin_where
query = relation.select(origin, where=where)
expression = ~table.id.in_(query)
if operator == '!=':
return ~expression
return expression
else:
if isinstance(value, basestring):
target_name = 'rec_name'
else:
target_name = 'id'
else:
_, target_name = name.split('.', 1)
target_domain = [(target_name,) + tuple(domain[1:])]
query = Target.search(target_domain, order=[], query=True)
where = Column(relation, self.target).in_(query)
if origin_where:
where &= origin_where
query = relation.select(origin, where=where)
return table.id.in_(query)
|