book/category: hierarchische sortierung, form optimiert

This commit is contained in:
Frederik Jaeckel 2022-09-22 15:40:23 +02:00
parent 1c22aac197
commit 7999440de7
5 changed files with 76 additions and 62 deletions

View file

@ -10,39 +10,7 @@ from trytond.pyson import Eval, If, Bool
from trytond.exceptions import UserError
from trytond.i18n import gettext
from sql.operators import Equal
from sql.functions import Function
from sql import With, Literal
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
from .model import order_name_hierarchical
sel_categorytype = [
@ -128,28 +96,7 @@ class Category(tree(separator='/'), ModelSQL, ModelView):
""" order by pos
a recursive sorting
"""
Category2 = Pool().get('cashbook.category')
tab_cat = Category2.__table__()
tab_cat2 = Category2.__table__()
table, _ = tables[None]
categories = With('id', 'name', 'name_path', recursive=True)
categories.query = tab_cat.select(
tab_cat.id, tab_cat.name, Array(tab_cat.name),
where = tab_cat.parent==None,
)
categories.query |= tab_cat2.join(categories,
condition=categories.id==tab_cat2.parent,
).select(
tab_cat2.id, tab_cat2.name, ArrayApppend(categories.name_path, tab_cat2.name),
)
categories.query.all_ = True
query = categories.select(
ArrayToString(categories.name_path, '/').as_('rec_name'),
where = table.id==categories.id,
with_ = [categories])
return [query]
return order_name_hierarchical('cashbook.category', tables)
@fields.depends('parent', '_parent_parent.cattype')
def on_change_with_parent_cattype(self, name=None):