book/category: hierarchische sortierung, form optimiert
This commit is contained in:
parent
1c22aac197
commit
7999440de7
5 changed files with 76 additions and 62 deletions
62
model.py
62
model.py
|
@ -5,6 +5,68 @@
|
|||
|
||||
from trytond.model import MultiValueMixin, ValueMixin, fields, Unique
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
from sql import With, Literal
|
||||
from sql.functions import Function
|
||||
|
||||
|
||||
class ArrayApppend(Function):
|
||||
""" sql: array_append
|
||||
"""
|
||||
__slots__ = ()
|
||||
_function = 'ARRAY_APPEND'
|
||||
|
||||
# end ArrayApppend
|
||||
|
||||
|
||||
class ArrayToString(Function):
|
||||
""" sql: array_to_string
|
||||
"""
|
||||
__slots__ = ()
|
||||
_function = 'ARRAY_TO_STRING'
|
||||
|
||||
# end ArrayToString
|
||||
|
||||
|
||||
class Array(Function):
|
||||
""" sql: array-type
|
||||
"""
|
||||
__slots__ = ()
|
||||
_function = 'ARRAY'
|
||||
|
||||
def __str__(self):
|
||||
return self._function + '[' + ', '.join(
|
||||
map(self._format, self.args)) + ']'
|
||||
|
||||
# end Array
|
||||
|
||||
|
||||
def order_name_hierarchical(model_name, tables):
|
||||
""" order by pos
|
||||
a recursive sorting
|
||||
"""
|
||||
Model2 = Pool().get(model_name)
|
||||
tab_mod = Model2.__table__()
|
||||
tab_mod2 = Model2.__table__()
|
||||
table, _ = tables[None]
|
||||
|
||||
lines = With('id', 'name', 'name_path', recursive=True)
|
||||
lines.query = tab_mod.select(
|
||||
tab_mod.id, tab_mod.name, Array(tab_mod.name),
|
||||
where = tab_mod.parent==None,
|
||||
)
|
||||
lines.query |= tab_mod2.join(lines,
|
||||
condition=lines.id==tab_mod2.parent,
|
||||
).select(
|
||||
tab_mod2.id, tab_mod2.name, ArrayApppend(lines.name_path, tab_mod2.name),
|
||||
)
|
||||
lines.query.all_ = True
|
||||
|
||||
query = lines.select(
|
||||
ArrayToString(lines.name_path, '/').as_('rec_name'),
|
||||
where = table.id==lines.id,
|
||||
with_ = [lines])
|
||||
return [query]
|
||||
|
||||
|
||||
class UserValueMixin(ValueMixin):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue