diff --git a/asset.py b/asset.py index e214713..4a4ee72 100644 --- a/asset.py +++ b/asset.py @@ -458,8 +458,6 @@ class Asset(SymbolMixin, ModelSQL, ModelView): """ pool = Pool() Rate = pool.get('investment.rate') - Asset2 = pool.get('investment.asset') - tab_asset = Asset2.__table__() tab_rate1 = Rate.__table__() tab_rate2 = Rate.__table__() context = Transaction().context @@ -467,17 +465,14 @@ class Asset(SymbolMixin, ModelSQL, ModelView): query_date = context.get('qdate', CurrentDate()) where_asset = tab_rate1.date <= query_date if isinstance(asset_ids, list): - where_asset &= tab_asset.id.in_(asset_ids) + where_asset &= tab_rate1.asset.in_(asset_ids) - tab_today = tab_asset.join( - tab_rate1, - condition=tab_asset.id == tab_rate1.asset, - ).select( - tab_asset.id, + tab_today = tab_rate1.select( + tab_rate1.asset.as_('id'), tab_rate1.date, tab_rate1.rate, - distinct_on=[tab_asset.id], - order_by=[tab_asset.id, tab_rate1.date.desc], + distinct_on=[tab_rate1.asset], + order_by=[tab_rate1.asset, tab_rate1.date.desc], where=where_asset, ) @@ -598,23 +593,24 @@ class Asset(SymbolMixin, ModelSQL, ModelView): exp = Decimal(Decimal(1) / 10 ** digits_percent) asset_id_lst = [x.id for x in assets] - for x in names: - tab_percent = cls.get_percentage_sql( - days={ - 'change_day1': 0, - 'change_month1': 30, - 'change_month3': 90, - 'change_month6': 180, - 'change_month12': 365, - }[x], - asset_ids=asset_id_lst, - ) - cursor.execute(*tab_percent) - records = cursor.fetchall() + if asset_id_lst and names: + for x in names: + tab_percent = cls.get_percentage_sql( + days={ + 'change_day1': 0, + 'change_month1': 30, + 'change_month3': 90, + 'change_month6': 180, + 'change_month12': 365, + }[x], + asset_ids=asset_id_lst, + ) + cursor.execute(*tab_percent) + records = cursor.fetchall() - for record in records: - result[x][record[0]] = record[3].quantize(exp) \ - if record[3] is not None else None + for record in records: + result[x][record[0]] = record[3].quantize(exp) \ + if record[3] is not None else None return result @classmethod