From 73c961cfee250504cf0e56d34be6873c05e8a51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 2 Aug 2020 02:37:18 +0800 Subject: [PATCH] Moved the common codes in the make_transaction_form_from_post() and fill_transaction_from_post() utilities to the new _find_max_record_no() utility, to avoid duplicated codes in the accounting application. --- accounting/utils.py | 54 +++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/accounting/utils.py b/accounting/utils.py index 7af810d..f7a6890 100644 --- a/accounting/utils.py +++ b/accounting/utils.py @@ -295,19 +295,7 @@ def fill_transaction_from_post(transaction, post): if "notes" in post: transaction.notes = post["notes"] # The records - max_no = { - "debit": 0, - "credit": 0, - } - for key in post.keys(): - m = re.match( - "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$", - key) - if m is not None: - rec_type = m.group(1) - no = int(m.group(2)) - if max_no[rec_type] < no: - max_no[rec_type] = no + max_no = _find_max_record_no(post) records = [] for rec_type in max_no.keys(): for i in range(max_no[rec_type]): @@ -431,19 +419,7 @@ def make_transaction_form_from_post(post, txn_type, transaction): transaction_form.transaction = transaction transaction_form.txn_type = txn_type # The records - max_no = { - "debit": 0, - "credit": 0, - } - for key in post.keys(): - m = re.match( - "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$", - key) - if m is not None: - rec_type = m.group(1) - no = int(m.group(2)) - if max_no[rec_type] < no: - max_no[rec_type] = no + max_no = _find_max_record_no(post) if max_no["debit"] == 0: max_no["debit"] = 1 if max_no["credit"] == 0: @@ -487,3 +463,29 @@ def make_transaction_form_from_status(request, txn_type, transaction): return return make_transaction_form_from_post( status["form"], txn_type, transaction) + + +def _find_max_record_no(post): + """Finds the max debit and record numbers from the POSTed form. + + Args: + post (dict[str,str]): The POSTed data. + + Returns: + dict[str,int]: The max debit and record numbers from the POSTed form. + + """ + max_no = { + "debit": 0, + "credit": 0, + } + for key in post.keys(): + m = re.match( + "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$", + key) + if m is not None: + rec_type = m.group(1) + no = int(m.group(2)) + if max_no[rec_type] < no: + max_no[rec_type] = no + return max_no