/usr/lib/python2.7/dist-packages/trytond/model/union.py is in tryton-server 3.4.0-3+deb8u3.
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 | # This file is part of Tryton. The COPYRIGHT file at the toplevel of this
# repository contains the full copyright notices and license terms.
from sql import Union, Column, Literal, Cast
from trytond.model import fields
from trytond.pool import Pool
class UnionMixin:
'Mixin to combine models'
@staticmethod
def union_models():
return []
@classmethod
def union_shard(cls, column, model):
models = cls.union_models()
length = len(models)
i = models.index(model)
return ((column * length) + i)
@classmethod
def union_unshard(cls, record_id):
pool = Pool()
models = cls.union_models()
length = len(models)
record_id, i = divmod(record_id, length)
Model = pool.get(models[i])
return Model(record_id)
@classmethod
def union_column(cls, name, field, table, Model):
column = Literal(None)
union_field = Model._fields.get(name)
if union_field:
column = Column(table, union_field.name)
if (isinstance(field, fields.Many2One)
and field.model_name == cls.__name__):
target_model = union_field.model_name
if target_model in cls.union_models():
column = cls.union_shard(column, target_model)
else:
column = Literal(None)
return column
@classmethod
def union_columns(cls, model):
pool = Pool()
Model = pool.get(model)
table = Model.__table__()
columns = [cls.union_shard(table.id, model).as_('id')]
for name in sorted(cls._fields.keys()):
field = cls._fields[name]
if name == 'id' or hasattr(field, 'set'):
continue
column = cls.union_column(name, field, table, Model)
columns.append(Cast(column, field.sql_type().base).as_(name))
return table, columns
@classmethod
def table_query(cls):
queries = []
for model in cls.union_models():
table, columns = cls.union_columns(model)
queries.append(table.select(*columns))
return Union(*queries)
|