formatting, line: test for delete of party

This commit is contained in:
Frederik Jaeckel 2023-05-18 12:15:53 +02:00
parent 78f160bf0b
commit 619a17bcd6
16 changed files with 701 additions and 516 deletions

View file

@ -8,19 +8,21 @@ from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.cache import MemoryCache
from trytond.config import config
from datetime import timedelta, datetime
from datetime import timedelta
from decimal import Decimal
from sql import With, Literal
from sql import With
from sql.functions import Function
from sql.conditionals import Coalesce
import copy
if config.get('cashbook', 'memcache', default='yes').lower() in ['yes', '1', 'true']:
if config.get('cashbook', 'memcache', default='yes').lower() \
in ['yes', '1', 'true']:
ENABLE_CACHE = True
else:
ENABLE_CACHE = False
if config.get('cashbook', 'sync', default='yes').lower() in ['yes', '1', 'true']:
if config.get('cashbook', 'sync', default='yes').lower() \
in ['yes', '1', 'true']:
ENABLE_CACHESYNC = True
else:
ENABLE_CACHESYNC = False
@ -36,6 +38,7 @@ class ArrayAgg(Function):
# end ArrayAgg
class ArrayAppend(Function):
""" sql: array_append
"""
@ -87,14 +90,14 @@ class MemCache(Model):
_cashbook_value_cache = MemoryCache(
'cashbook.book.valuecache',
context = False,
duration = timedelta(seconds=60*60*4))
context=False,
duration=timedelta(seconds=60*60*4))
@classmethod
def read_value(cls, cache_key):
""" read values from cache
"""
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return None
return copy.deepcopy(cls._cashbook_value_cache.get(cache_key))
@ -102,23 +105,24 @@ class MemCache(Model):
def store_result(cls, records, cache_keys, values, skip_records=[]):
""" store result to cache
"""
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return
for record in records:
if record not in skip_records:
continue
data = {x:values[x][record.id]
for x in values.keys()
if record.id in values[x].keys()}
cls._cashbook_value_cache.set(cache_keys[record.id], copy.deepcopy(data))
if ENABLE_CACHESYNC == True:
data = {
x: values[x][record.id]
for x in values.keys() if record.id in values[x].keys()}
cls._cashbook_value_cache.set(
cache_keys[record.id], copy.deepcopy(data))
if ENABLE_CACHESYNC is True:
cls._cashbook_value_cache.sync(Transaction())
@classmethod
def store_value(cls, cache_key, values):
""" store values to cache
"""
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return
cls._cashbook_value_cache.set(cache_key, copy.deepcopy(values))
@ -126,7 +130,7 @@ class MemCache(Model):
def read_from_cache(cls, records, cache_keys, names, result):
""" get stored values from memcache
"""
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return (records, result)
todo_records = []
@ -141,7 +145,7 @@ class MemCache(Model):
if result[name][record.id] is None:
result[name][record.id] = Decimal('0.0')
result[name][record.id] += values[name]
else :
else:
todo_records.append(record)
return (todo_records, result)
@ -152,7 +156,7 @@ class MemCache(Model):
pool = Pool()
cursor = Transaction().connection.cursor()
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return '-'
fname = [name, str(record.id)]
@ -173,15 +177,17 @@ class MemCache(Model):
tab_model = Model.__table__()
tab_query = Model.search(line['query'], query=True)
qu1 = tab_model.join(tab_query,
condition=tab_query.id==tab_model.id,
qu1 = tab_model.join(
tab_query,
condition=tab_query.id == tab_model.id,
).select(
tab_model.id,
tab_model.write_date,
tab_model.create_date,
limit = 1,
order_by = [
Coalesce(tab_model.write_date, tab_model.create_date).desc,
limit=1,
order_by=[
Coalesce(
tab_model.write_date, tab_model.create_date).desc,
tab_model.id.desc,
],
)
@ -193,7 +199,7 @@ class MemCache(Model):
records[0][1],
records[0][2],
))
else :
else:
fname.append('0')
if 'cachekey' in line.keys():
@ -216,11 +222,12 @@ class MemCache(Model):
def record_update(cls, cache_key, record):
""" update cache-value
"""
if ENABLE_CACHE == False:
if ENABLE_CACHE is False:
return
cls.store_value(cache_key,
cls.store_value(
cache_key,
cls.genkey(record.id, record.write_date, record.create_date)
if record is not None else None)
if record is not None else None)
# end mem_cache
@ -235,18 +242,17 @@ def sub_ids_hierarchical(model_name):
lines = With('parent', 'id', recursive=True)
lines.query = tab_mod.select(
tab_mod.id, tab_mod.id,
) | tab_mod2.join(lines,
condition=lines.id==tab_mod2.parent,
).select(
lines.parent, tab_mod2.id,
)
) | tab_mod2.join(
lines,
condition=lines.id == tab_mod2.parent,
).select(lines.parent, tab_mod2.id)
lines.query.all_ = True
query = lines.select(
lines.parent,
ArrayAgg(lines.id).as_('subids'),
group_by=[lines.parent],
with_ = [lines])
with_=[lines])
return query
@ -262,24 +268,28 @@ def order_name_hierarchical(model_name, tables):
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,
where=tab_mod.parent == None,
)
lines.query |= tab_mod2.join(lines,
condition=lines.id==tab_mod2.parent,
lines.query |= tab_mod2.join(
lines,
condition=lines.id == tab_mod2.parent,
).select(
tab_mod2.id, tab_mod2.name, ArrayAppend(lines.name_path, tab_mod2.name),
tab_mod2.id,
tab_mod2.name,
ArrayAppend(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])
where=table.id == lines.id,
with_=[lines])
return [query]
class UserValueMixin(ValueMixin):
iduser = fields.Many2One(model_name='res.user', string="User",
iduser = fields.Many2One(
model_name='res.user', string="User",
select=True, ondelete='CASCADE', required=True)
@classmethod
@ -313,6 +323,7 @@ class UserMultiValueMixin(MultiValueMixin):
Value = self.multivalue_model(name)
if issubclass(Value, UserValueMixin):
pattern = self.updt_multivalue_pattern(pattern)
return super(UserMultiValueMixin, self).set_multivalue(name, value, **pattern)
return super(
UserMultiValueMixin, self).set_multivalue(name, value, **pattern)
# end UserMultiValueMixin