Renamed the variables in the sort_form_transaction_records() utility in the accounting application.

This commit is contained in:
依瑪貓 2020-07-29 09:40:51 +08:00
parent 758af556fc
commit aa35e45426

View File

@ -363,7 +363,7 @@ def sort_form_transaction_records(form):
form (dict): The POSTed form. form (dict): The POSTed form.
""" """
# Collects the available record numbers # Collects the available record numbers
rec_no = { record_no = {
"debit": [], "debit": [],
"credit": [], "credit": [],
} }
@ -373,32 +373,32 @@ def sort_form_transaction_records(form):
key) key)
if m is None: if m is None:
continue continue
rec_type = m.group(1) record_type = m.group(1)
no = int(m.group(2)) no = int(m.group(2))
if no not in rec_no[rec_type]: if no not in record_no[record_type]:
rec_no[rec_type].append(no) record_no[record_type].append(no)
# Sorts these record numbers by their specified orders # Sorts these record numbers by their specified orders
for rec_type in rec_no.keys(): for record_type in record_no.keys():
orders = {} orders = {}
for no in rec_no[rec_type]: for no in record_no[record_type]:
try: try:
orders[no] = int(form[F"{rec_type}-{no}-ord"]) orders[no] = int(form[F"{record_type}-{no}-ord"])
except KeyError: except KeyError:
orders[no] = 9999 orders[no] = 9999
except ValueError: except ValueError:
orders[no] = 9999 orders[no] = 9999
rec_no[rec_type].sort(key=lambda x: orders[x]) record_no[record_type].sort(key=lambda x: orders[x])
# Constructs the sorted new form # Constructs the sorted new form
new_form = {} new_form = {}
for rec_type in rec_no.keys(): for record_type in record_no.keys():
for i in range(len(rec_no[rec_type])): for i in range(len(record_no[record_type])):
old_no = rec_no[rec_type][i] old_no = record_no[record_type][i]
no = i + 1 no = i + 1
new_form[F"{rec_type}-{no}-ord"] = no new_form[F"{record_type}-{no}-ord"] = no
for attr in ["sn", "account", "summary", "amount"]: for attr in ["sn", "account", "summary", "amount"]:
if F"{rec_type}-{old_no}-{attr}" in form: if F"{record_type}-{old_no}-{attr}" in form:
new_form[F"{rec_type}-{no}-{attr}"]\ new_form[F"{record_type}-{no}-{attr}"]\
= form[F"{rec_type}-{old_no}-{attr}"] = form[F"{record_type}-{old_no}-{attr}"]
# Purges the old form and fills it with the new form # Purges the old form and fills it with the new form
for x in [x for x in form.keys() if re.match( for x in [x for x in form.keys() if re.match(
"^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)", "^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)",