Moved the sort_post_txn_records utility and the logic to create the form to TransactionForm in the accounting application.

This commit is contained in:
依瑪貓
2020-08-19 13:38:05 +08:00
parent 8770770b66
commit 6f019d23c0
4 changed files with 65 additions and 61 deletions

View File

@@ -24,7 +24,7 @@ from typing import Optional, List, Dict
from django import forms
from django.core.validators import RegexValidator
from django.db.models import Q, Max
from django.db.models import Q, Max, Model
from django.db.models.functions import Length
from django.utils.translation import gettext as _
@@ -234,6 +234,67 @@ class TransactionForm(forms.Form):
self.txn_type = None
self.transaction = None
@staticmethod
def from_post(post: Dict[str, str], txn_type: str, txn: Model):
TransactionForm._sort_post_txn_records(post)
form = TransactionForm(post)
form.txn_type = txn_type
form.transaction = txn
return form
@staticmethod
def _sort_post_txn_records(post: Dict[str, str]) -> None:
"""Sorts the records in the form by their specified order, so that the
form can be used to populate the data to return to the user.
Args:
post: The POSTed form.
"""
# Collects the available record numbers
record_no = {
"debit": [],
"credit": [],
}
for key in post.keys():
m = re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
key)
if m is None:
continue
record_type = m.group(1)
no = int(m.group(2))
if no not in record_no[record_type]:
record_no[record_type].append(no)
# Sorts these record numbers by their specified orders
for record_type in record_no.keys():
orders = {}
for no in record_no[record_type]:
try:
orders[no] = int(post[F"{record_type}-{no}-ord"])
except KeyError:
orders[no] = 9999
except ValueError:
orders[no] = 9999
record_no[record_type].sort(key=lambda n: orders[n])
# Constructs the sorted new form
new_post = {}
for record_type in record_no.keys():
for i in range(len(record_no[record_type])):
old_no = record_no[record_type][i]
no = i + 1
new_post[F"{record_type}-{no}-ord"] = str(no)
for attr in ["id", "account", "summary", "amount"]:
if F"{record_type}-{old_no}-{attr}" in post:
new_post[F"{record_type}-{no}-{attr}"] \
= post[F"{record_type}-{old_no}-{attr}"]
# Purges the old form and fills it with the new form
for x in [x for x in post.keys() if re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
x)]:
del post[x]
for key in new_post.keys():
post[key] = new_post[key]
def clean(self):
"""Validates the form globally.