From 7999440de7921d756631663379a80d893568c508 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Thu, 22 Sep 2022 15:40:23 +0200 Subject: [PATCH] book/category: hierarchische sortierung, form optimiert --- book.py | 10 ++++++- category.py | 57 ++------------------------------------ model.py | 62 ++++++++++++++++++++++++++++++++++++++++++ view/book_form.xml | 2 -- view/category_form.xml | 7 ++--- 5 files changed, 76 insertions(+), 62 deletions(-) diff --git a/book.py b/book.py index a3704ed..7db0871 100644 --- a/book.py +++ b/book.py @@ -13,6 +13,7 @@ from trytond.report import Report from decimal import Decimal from sql.aggregate import Sum from sql.conditionals import Case +from .model import order_name_hierarchical STATES = { @@ -136,7 +137,7 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView): @classmethod def __setup__(cls): super(Book, cls).__setup__() - cls._order.insert(0, ('name', 'ASC')) + cls._order.insert(0, ('rec_name', 'ASC')) cls._order.insert(0, ('state', 'ASC')) t = cls.__table__() cls._sql_constraints.extend([ @@ -225,6 +226,13 @@ class Book(tree(separator='/'), Workflow, ModelSQL, ModelView): ) return [query] + @staticmethod + def order_rec_name(tables): + """ order by pos + a recursive sorting + """ + return order_name_hierarchical('cashbook.book', tables) + def get_rec_name(self, name): """ name, balance, state """ diff --git a/category.py b/category.py index 477016c..2d73748 100644 --- a/category.py +++ b/category.py @@ -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): diff --git a/model.py b/model.py index d2124f3..78bc435 100644 --- a/model.py +++ b/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): diff --git a/view/book_form.xml b/view/book_form.xml index d2ffaad..24a27b5 100644 --- a/view/book_form.xml +++ b/view/book_form.xml @@ -44,8 +44,6 @@ full copyright notices and license terms. -->