kategorie: hierarchische sortierung, sequence-spalte entfernt
This commit is contained in:
parent
7fd42c0b42
commit
64e9bab592
6 changed files with 88 additions and 245 deletions
85
category.py
85
category.py
|
@ -3,14 +3,46 @@
|
|||
# The COPYRIGHT file at the top level of this repository contains the
|
||||
# full copyright notices and license terms.
|
||||
|
||||
from trytond.model import ModelView, ModelSQL, fields, Unique, Exclude, tree, sequence_ordered
|
||||
from trytond.model import ModelView, ModelSQL, fields, Unique, Exclude, tree
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.pool import Pool
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
sel_categorytype = [
|
||||
|
@ -19,7 +51,7 @@ sel_categorytype = [
|
|||
]
|
||||
|
||||
|
||||
class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
|
||||
class Category(tree(separator='/'), ModelSQL, ModelView):
|
||||
'Category'
|
||||
__name__ = 'cashbook.category'
|
||||
|
||||
|
@ -38,7 +70,6 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
|
|||
|
||||
company = fields.Many2One(string='Company', model_name='company.company',
|
||||
required=True, ondelete="RESTRICT")
|
||||
sequence = fields.Integer(string='Sequence', select=True)
|
||||
parent = fields.Many2One(string="Parent",
|
||||
model_name='cashbook.category', ondelete='RESTRICT',
|
||||
left='left', right='right')
|
||||
|
@ -47,10 +78,15 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
|
|||
left = fields.Integer(string='Left', required=True, select=True)
|
||||
right = fields.Integer(string='Right', required=True, select=True)
|
||||
|
||||
@classmethod
|
||||
def __register__(cls, module_name):
|
||||
super(Category, cls).__register__(module_name)
|
||||
cls.migrate_sequence(module_name)
|
||||
|
||||
@classmethod
|
||||
def __setup__(cls):
|
||||
super(Category, cls).__setup__()
|
||||
cls._order.insert(0, ('name', 'ASC'))
|
||||
cls._order.insert(0, ('rec_name', 'ASC'))
|
||||
t = cls.__table__()
|
||||
cls._sql_constraints.extend([
|
||||
('name_uniq',
|
||||
|
@ -64,6 +100,13 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
|
|||
'cashbook.msg_category_name_unique'),
|
||||
])
|
||||
|
||||
@classmethod
|
||||
def migrate_sequence(cls, module_name):
|
||||
""" remove colum 'sequence'
|
||||
"""
|
||||
table = cls.__table_handler__(module_name)
|
||||
table.drop_column('sequence')
|
||||
|
||||
@classmethod
|
||||
def default_cattype(cls):
|
||||
return 'out'
|
||||
|
@ -83,28 +126,30 @@ class Category(tree(separator='/'), sequence_ordered(), ModelSQL, ModelView):
|
|||
@staticmethod
|
||||
def order_rec_name(tables):
|
||||
""" 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 = select(tab_cat.id, tab_cat.name, array[])
|
||||
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
|
||||
|
||||
# ~ 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"
|
||||
query = categories.select(
|
||||
ArrayToString(categories.name_path, '/').as_('rec_name'),
|
||||
where = table.id==categories.id,
|
||||
with_ = [categories])
|
||||
return [query]
|
||||
|
||||
@fields.depends('parent', '_parent_parent.cattype')
|
||||
def on_change_with_parent_cattype(self, name=None):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue