From 7fd42c0b421ded8d9058b35d5116199997dc41ee Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Mon, 29 Aug 2022 23:34:36 +0200 Subject: [PATCH] kategory: sortierung begnnen --- category.py | 27 +++++++++++++++++++++++++ tests/test_category.py | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) diff --git a/category.py b/category.py index ffaf75c..db33c78 100644 --- a/category.py +++ b/category.py @@ -10,6 +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 import With sel_categorytype = [ @@ -79,6 +80,32 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView): def default_right(): return 0 + @staticmethod + def order_rec_name(tables): + """ order by pos + """ + Category2 = Pool().get('cashbook.category') + tab_cat = Category2.__table__() + table, _ = tables[None] + + categories = With('id', 'name', 'name_path', recursive=True) + categories.query = select(tab_cat.id, tab_cat.name, array[]) + +# ~ with recursive categories (id, level, name, name_path) as ( + # ~ select "a"."id", 0, "a"."name", array["a"."name"] + # ~ from cashbook_category as "a" + # ~ where "a"."parent" is null + + # ~ union all + + # ~ select "b"."id", "c"."level" + 1, "b"."name", array_append("c"."name_path", "b"."name") + # ~ from cashbook_category as "b" + # ~ inner join categories as "c" on "c"."id" = "b"."parent" +# ~ ) +# ~ select "d"."id", array_to_string("d"."name_path", '/') as "rec_name" +# ~ from categories as "d" +# ~ order by "rec_name" + @fields.depends('parent', '_parent_parent.cattype') def on_change_with_parent_cattype(self, name=None): """ get type of parent category or None diff --git a/tests/test_category.py b/tests/test_category.py index f2e0b6e..d7910d6 100644 --- a/tests/test_category.py +++ b/tests/test_category.py @@ -28,6 +28,52 @@ class CategoryTestCase(ModuleTestCase): }]) return category + @with_transaction() + def test_category_check_rec_name(self): + """ create category, test rec_name, search, order + """ + pool = Pool() + Category = pool.get('cashbook.category') + company = self.prep_company() + + Category.create([{ + 'company': company.id, + 'name': 'Level 1', + 'cattype': 'in', + 'childs': [('create', [{ + 'company': company.id, + 'name': 'Level 2a', + 'cattype': 'in', + }, { + 'company': company.id, + 'name': 'Level 2b', + 'cattype': 'in', + }])], + }, { + 'company': company.id, + 'name': 'Level 1b', + 'cattype': 'in', + 'childs': [('create', [{ + 'company': company.id, + 'name': 'Level 1b.2a', + 'cattype': 'in', + }, { + 'company': company.id, + 'name': 'Level 1b.2b', + 'cattype': 'in', + }])], + }]) + + self.assertEqual(Category.search_count([ + ('rec_name', 'ilike', '%1b.2b%'), + ]), 1) + self.assertEqual(Category.search_count([ + ('rec_name', 'ilike', '%1b.2%'), + ]), 2) + self.assertEqual(Category.search_count([ + ('rec_name', '=', 'Level 1b/Level 1b.2b'), + ]), 1) + @with_transaction() def test_category_create_check_category_type(self): """ create category, update type of category