def _make_combined_transactions(self):
"""
Combine all Transactions and ScheduledTransactions from
``self._data_cache`` into one ordered list of similar dicts, adding
dates to the monthly ScheduledTransactions as appropriate and excluding
ScheduledTransactions that have been converted to real Transactions.
Store the finished list back into ``self._data_cache``.
"""
unordered = []
# ScheduledTransaction ID to count of real trans for each
st_ids = defaultdict(int)
for t in self._data_cache['transactions']:
unordered.append(self._trans_dict(t))
if t.scheduled_trans_id is not None:
st_ids[t.scheduled_trans_id] += 1
for t in self._data_cache['st_date']:
if t.id not in st_ids:
unordered.append(self._trans_dict(t))
for t in self._data_cache['st_monthly']:
if t.id not in st_ids:
unordered.append(self._trans_dict(t))
ordered = []
for t in self._data_cache['st_per_period']:
d = self._trans_dict(t)
for _ in range(0, (t.num_per_period - st_ids[t.id])):
ordered.append(d)
for t in sorted(unordered, key=lambda k: k['date']):
ordered.append(t)
def sortkey(k):
d = k.get('date', None)
if d is None:
d = date.min
return d, k['amount']
return sorted(ordered, key=sortkey)
评论列表
文章目录