From 4f837f942f097e14b5df9e10f603e312fa4080cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Wed, 5 Aug 2020 11:33:15 +0800 Subject: [PATCH] Revised the get_summary_categories() utility to sort the returned categories by their frequencies in the accounting application. --- accounting/utils.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/accounting/utils.py b/accounting/utils.py index 618260d..fe03b20 100644 --- a/accounting/utils.py +++ b/accounting/utils.py @@ -359,12 +359,19 @@ def get_summary_categories(): key = "%s-%s" % (row["rec_type"], row["cat_type"]) if key not in categories: categories[key] = {} - # Keeps only the first account with most records if row["category"] not in categories[key]: - categories[key][row["category"]] = row["account__code"] + categories[key][row["category"]] = [] + categories[key][row["category"]].append(row) + for key in categories: + # Keeps only the first account with most records + categories[key] = [categories[key][x][0] for x in categories[key]] + # Sorts the categories by the frequency + categories[key].sort(key=lambda x: (-x["count"], x["category"])) + # Keeps only the category and the account + categories[key] = [[x["category"], x["account__code"]] + for x in categories[key]] # Converts the dictionary to a list, as the category may not be US-ASCII - return json.dumps({t: [[c, categories[t][c]] for c in categories[t]] - for t in categories.keys()}) + return json.dumps(categories) def fill_txn_from_post(txn_type, txn, post):