asset: abfrage updatezeitpunkt optimiert, anzeige der vortagsprozente, farbe für zeilen

This commit is contained in:
Frederik Jaeckel 2022-11-25 11:00:03 +01:00
parent 34faf91476
commit cba2117d13
5 changed files with 117 additions and 98 deletions

114
asset.py
View file

@ -6,12 +6,12 @@
from trytond.model import ModelView, ModelSQL, fields
from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pyson import Eval, Bool, And
from trytond.pyson import Eval, Bool, And, If
from trytond.report import Report
from decimal import Decimal
from datetime import time
from sql.functions import CurrentTime, CurrentDate
from sql.conditionals import Case
from sql.functions import CurrentDate, CurrentTimestamp
from sql.conditionals import Case, Coalesce
from sql import Literal
@ -76,9 +76,9 @@ class Asset(ModelSQL, ModelView):
states={
'readonly': ~Bool(Eval('updtsource')),
}, depends=['updtsource'])
updtneeded = fields.Function(fields.Boolean(string='Course update needed',
nextupdtate = fields.Function(fields.DateTime(string='Next Update',
readonly=True),
'on_change_with_updtneeded', searcher='search_updtneeded')
'get_nextupdtates', searcher='search_nextupdtate')
# percentage change
change_today = fields.Function(fields.Numeric(string='Previous Day',
@ -102,6 +102,15 @@ class Asset(ModelSQL, ModelView):
readonly=True, digits=(16,1)),
'get_percentage_change')
@classmethod
def view_attributes(cls):
return super().view_attributes() + [
('/tree', 'visual',
If(Eval('change_today', 0) < 0, 'warning',
If(Eval('change_today', 0) > 0, 'success', '')
)),
]
@classmethod
def default_currency(cls):
""" currency of company
@ -218,8 +227,6 @@ class Asset(ModelSQL, ModelView):
for name in names:
result[name][record[0]] = values[name]
print('-- result:', result)
return result
@ -320,48 +327,70 @@ class Asset(ModelSQL, ModelView):
if self.company.currency.id != self.currency.id:
return self.company.currency.id
@fields.depends('id')
def on_change_with_updtneeded(self, name=None):
""" get state of update
"""
Asset2 = Pool().get('investment.asset')
if self.id:
if Asset2.search_count([
('updtneeded', '=', True),
('id', '=', self.id)
]) == 1:
return True
return False
@classmethod
def search_updtneeded(cls, names, clause):
""" search for assets to update
def get_next_update_datetime_sql(cls):
""" get sql for datetime of next planned update
"""
pool = Pool()
Asset2 = pool.get('investment.asset')
Asset = pool.get('investment.asset')
Rate = pool.get('investment.rate')
tab_asset = Asset2.__table__()
tab_asset = Asset.__table__()
tab_rate = Rate.__table__()
context = Transaction().context
query_date = context.get('qdate', CurrentDate() - Literal(1))
query = tab_asset.join(tab_rate,
condition=tab_asset.id == tab_rate.asset,
type_ = 'LEFT OUTER',
).select(
tab_asset.id,
((Coalesce(tab_rate.date, query_date) + Literal(1)) + \
tab_asset.updttime).as_('updttime'),
distinct_on = [tab_asset.id],
order_by = [tab_asset.id, tab_rate.date.desc],
where=(tab_asset.updtsource != None),
)
return query
@classmethod
def get_nextupdtates(cls, assets, names):
""" get timestamp of next update
"""
Asset2 = Pool().get('investment.asset')
tab_updt = Asset2.get_next_update_datetime_sql()
cursor = Transaction().connection.cursor()
query = tab_updt.select(
tab_updt.id,
tab_updt.updttime,
where=tab_updt.id.in_([x.id for x in assets]),
)
cursor.execute(*query)
records = cursor.fetchall()
result = {x:{y.id: None for y in assets} for x in names}
for record in records:
(id1, updt) = record
r1 = {'nextupdtate': updt}
for n in names:
result[n][id1] = r1[n]
return result
@classmethod
def search_nextupdtate(cls, names, clause):
""" search for assets to update
"""
Asset2 = Pool().get('investment.asset')
tab_updt = Asset2.get_next_update_datetime_sql()
Operator = fields.SQL_OPERATORS[clause[1]]
context = Transaction().context
query_date = context.get('qdate', CurrentDate())
query_time = context.get('qtime', CurrentTime())
query = tab_asset.join(tab_rate,
condition=(tab_asset.id==tab_rate.asset) & \
(tab_rate.date == query_date),
type_ = 'LEFT OUTER',
).select(tab_asset.id,
where=Operator(
Case(
((tab_rate.id == None) & \
(tab_asset.updtsource != None) & \
(tab_asset.updttime <= query_time), True),
default_ = False,
),
clause[2]),
query = tab_updt.select(
tab_updt.id,
where=Operator(tab_updt.updttime, clause[2]),
)
return [('id', 'in', query)]
@ -473,8 +502,9 @@ class Asset(ModelSQL, ModelView):
Asset2 = pool.get('investment.asset')
OnlineSource = pool.get('investment.source')
query_time = context.get('qdatetime', CurrentTimestamp())
for asset in Asset2.search([
('updtneeded', '=', True),
('updttime', '<=', query_time),
]):
OnlineSource.update_rate(asset)