2022-08-09 13:08:41 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# This file is part of the cashbook-module from m-ds for Tryton.
|
|
|
|
# The COPYRIGHT file at the top level of this repository contains the
|
|
|
|
# full copyright notices and license terms.
|
|
|
|
|
2023-06-03 17:13:05 +00:00
|
|
|
from trytond.model import (
|
2023-12-23 09:36:58 +00:00
|
|
|
MultiValueMixin, ValueMixin, fields, Unique, Index)
|
2022-08-09 13:08:41 +00:00
|
|
|
from trytond.transaction import Transaction
|
2022-09-22 13:40:23 +00:00
|
|
|
from trytond.pool import Pool
|
2023-05-18 10:15:53 +00:00
|
|
|
from sql import With
|
2022-09-22 13:40:23 +00:00
|
|
|
from sql.functions import Function
|
2023-11-29 14:19:38 +00:00
|
|
|
from .const import DEF_NONE
|
|
|
|
|
2023-03-04 20:24:19 +00:00
|
|
|
|
2022-12-23 17:01:02 +00:00
|
|
|
class ArrayAgg(Function):
|
|
|
|
"""input values, including nulls, concatenated into an array.
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
_function = 'ARRAY_AGG'
|
|
|
|
|
|
|
|
# end ArrayAgg
|
|
|
|
|
2023-05-18 10:15:53 +00:00
|
|
|
|
2022-12-23 17:01:02 +00:00
|
|
|
class ArrayAppend(Function):
|
2022-09-22 13:40:23 +00:00
|
|
|
""" sql: array_append
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
_function = 'ARRAY_APPEND'
|
|
|
|
|
|
|
|
# end ArrayApppend
|
|
|
|
|
|
|
|
|
|
|
|
class ArrayToString(Function):
|
|
|
|
""" sql: array_to_string
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
_function = 'ARRAY_TO_STRING'
|
|
|
|
|
|
|
|
# end ArrayToString
|
|
|
|
|
|
|
|
|
2022-12-23 17:59:05 +00:00
|
|
|
class AnyInArray(Function):
|
|
|
|
""" sql: array_to_string
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
_function = 'ANY'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self._function + '(' + ', '.join(
|
|
|
|
map(self._format, self.args)) + '::int[])'
|
|
|
|
|
|
|
|
# end AnyInArray
|
|
|
|
|
|
|
|
|
2022-09-22 13:40:23 +00:00
|
|
|
class Array(Function):
|
|
|
|
""" sql: array-type
|
|
|
|
"""
|
|
|
|
__slots__ = ()
|
|
|
|
_function = 'ARRAY'
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self._function + '[' + ', '.join(
|
|
|
|
map(self._format, self.args)) + ']'
|
|
|
|
|
|
|
|
# end Array
|
|
|
|
|
|
|
|
|
2022-12-23 17:01:02 +00:00
|
|
|
def sub_ids_hierarchical(model_name):
|
|
|
|
""" get table with id and sub-ids
|
|
|
|
"""
|
|
|
|
Model2 = Pool().get(model_name)
|
|
|
|
tab_mod = Model2.__table__()
|
|
|
|
tab_mod2 = Model2.__table__()
|
|
|
|
|
|
|
|
lines = With('parent', 'id', recursive=True)
|
|
|
|
lines.query = tab_mod.select(
|
|
|
|
tab_mod.id, tab_mod.id,
|
2023-05-18 10:15:53 +00:00
|
|
|
) | tab_mod2.join(
|
|
|
|
lines,
|
|
|
|
condition=lines.id == tab_mod2.parent,
|
|
|
|
).select(lines.parent, tab_mod2.id)
|
2022-12-23 17:01:02 +00:00
|
|
|
lines.query.all_ = True
|
|
|
|
|
|
|
|
query = lines.select(
|
|
|
|
lines.parent,
|
|
|
|
ArrayAgg(lines.id).as_('subids'),
|
|
|
|
group_by=[lines.parent],
|
2023-05-18 10:15:53 +00:00
|
|
|
with_=[lines])
|
2022-12-23 17:01:02 +00:00
|
|
|
return query
|
|
|
|
|
|
|
|
|
2022-09-22 13:40:23 +00:00
|
|
|
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),
|
2023-11-29 14:19:38 +00:00
|
|
|
where=tab_mod.parent == DEF_NONE,
|
2022-09-22 13:40:23 +00:00
|
|
|
)
|
2023-05-18 10:15:53 +00:00
|
|
|
lines.query |= tab_mod2.join(
|
|
|
|
lines,
|
|
|
|
condition=lines.id == tab_mod2.parent,
|
2022-09-22 13:40:23 +00:00
|
|
|
).select(
|
2023-05-18 10:15:53 +00:00
|
|
|
tab_mod2.id,
|
|
|
|
tab_mod2.name,
|
|
|
|
ArrayAppend(lines.name_path, tab_mod2.name),
|
2022-09-22 13:40:23 +00:00
|
|
|
)
|
|
|
|
lines.query.all_ = True
|
|
|
|
|
|
|
|
query = lines.select(
|
|
|
|
ArrayToString(lines.name_path, '/').as_('rec_name'),
|
2023-05-18 10:15:53 +00:00
|
|
|
where=table.id == lines.id,
|
|
|
|
with_=[lines])
|
2022-09-22 13:40:23 +00:00
|
|
|
return [query]
|
2022-08-09 13:08:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserValueMixin(ValueMixin):
|
2023-05-18 10:15:53 +00:00
|
|
|
iduser = fields.Many2One(
|
|
|
|
model_name='res.user', string="User",
|
2023-06-03 17:13:05 +00:00
|
|
|
ondelete='CASCADE', required=True)
|
2022-08-09 13:08:41 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def __setup__(cls):
|
|
|
|
super(UserValueMixin, cls).__setup__()
|
|
|
|
tab_val = cls.__table__()
|
2023-06-03 17:13:05 +00:00
|
|
|
cls._sql_indexes.update({
|
|
|
|
Index(
|
|
|
|
tab_val,
|
|
|
|
(tab_val.iduser, Index.Equality())),
|
|
|
|
})
|
2022-08-09 13:08:41 +00:00
|
|
|
cls._sql_constraints.extend([
|
|
|
|
('val_uniq',
|
|
|
|
Unique(tab_val, tab_val.iduser),
|
|
|
|
'cashbook.msg_setting_already_exists'),
|
|
|
|
])
|
|
|
|
|
|
|
|
# end UserValueMixin
|
|
|
|
|
|
|
|
|
|
|
|
class UserMultiValueMixin(MultiValueMixin):
|
|
|
|
|
|
|
|
def updt_multivalue_pattern(self, pattern):
|
|
|
|
""" add values to pattern
|
|
|
|
"""
|
|
|
|
pattern.setdefault('iduser', Transaction().user)
|
|
|
|
return pattern
|
|
|
|
|
|
|
|
def get_multivalue(self, name, **pattern):
|
|
|
|
Value = self.multivalue_model(name)
|
|
|
|
if issubclass(Value, UserValueMixin):
|
|
|
|
pattern = self.updt_multivalue_pattern(pattern)
|
|
|
|
return super(UserMultiValueMixin, self).get_multivalue(name, **pattern)
|
|
|
|
|
|
|
|
def set_multivalue(self, name, value, **pattern):
|
|
|
|
Value = self.multivalue_model(name)
|
|
|
|
if issubclass(Value, UserValueMixin):
|
|
|
|
pattern = self.updt_multivalue_pattern(pattern)
|
2023-05-18 10:15:53 +00:00
|
|
|
return super(
|
|
|
|
UserMultiValueMixin, self).set_multivalue(name, value, **pattern)
|
2022-08-09 13:08:41 +00:00
|
|
|
|
|
|
|
# end UserMultiValueMixin
|