Renamed utility methods *transaction* to *txn*, and the corresponding parameters and variables, to avoid name conflict in the accounting application.

This commit is contained in:
依瑪貓 2020-08-02 03:06:31 +08:00
parent 7be26e7447
commit 96eb033fb4
2 changed files with 57 additions and 57 deletions

View File

@ -283,17 +283,17 @@ def find_order_holes(records):
record.has_order_hole = record.transaction.date in holes record.has_order_hole = record.transaction.date in holes
def fill_transaction_from_post(transaction, post): def fill_txn_from_post(txn, post):
"""Fills the transaction from the POSTed data. """Fills the transaction from the POSTed data.
Args: Args:
transaction (Transaction): The transaction. txn (Transaction): The transaction.
post (dict): The POSTed data. post (dict): The POSTed data.
""" """
if "date" in post: if "date" in post:
transaction.date = post["date"] txn.date = post["date"]
if "notes" in post: if "notes" in post:
transaction.notes = post["notes"] txn.notes = post["notes"]
# The records # The records
max_no = _find_max_record_no(post) max_no = _find_max_record_no(post)
records = [] records = []
@ -303,7 +303,7 @@ def fill_transaction_from_post(transaction, post):
record = Record( record = Record(
ord=no, ord=no,
is_credit=(rec_type == "credit"), is_credit=(rec_type == "credit"),
transaction=transaction) transaction=txn)
if F"{rec_type}-{no}-id" in post: if F"{rec_type}-{no}-id" in post:
record.pk = post[F"{rec_type}-{no}-id"] record.pk = post[F"{rec_type}-{no}-id"]
if F"{rec_type}-{no}-account" in post: if F"{rec_type}-{no}-account" in post:
@ -313,22 +313,22 @@ def fill_transaction_from_post(transaction, post):
if F"{rec_type}-{no}-amount" in post: if F"{rec_type}-{no}-amount" in post:
record.amount = post[F"{rec_type}-{no}-amount"] record.amount = post[F"{rec_type}-{no}-amount"]
records.append(record) records.append(record)
transaction.records = records txn.records = records
def sort_form_transaction_records(form): def sort_post_txn_records(post):
"""Sorts the records in the form by their specified order, so that the """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. form can be used to populate the data to return to the user.
Args: Args:
form (dict): The POSTed form. post (dict): The POSTed form.
""" """
# Collects the available record numbers # Collects the available record numbers
record_no = { record_no = {
"debit": [], "debit": [],
"credit": [], "credit": [],
} }
for key in form.keys(): for key in post.keys():
m = re.match( m = re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)", "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
key) key)
@ -343,47 +343,47 @@ def sort_form_transaction_records(form):
orders = {} orders = {}
for no in record_no[record_type]: for no in record_no[record_type]:
try: try:
orders[no] = int(form[F"{record_type}-{no}-ord"]) orders[no] = int(post[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
record_no[record_type].sort(key=lambda n: orders[n]) record_no[record_type].sort(key=lambda n: orders[n])
# Constructs the sorted new form # Constructs the sorted new form
new_form = {} new_post = {}
for record_type in record_no.keys(): for record_type in record_no.keys():
for i in range(len(record_no[record_type])): for i in range(len(record_no[record_type])):
old_no = record_no[record_type][i] old_no = record_no[record_type][i]
no = i + 1 no = i + 1
new_form[F"{record_type}-{no}-ord"] = no new_post[F"{record_type}-{no}-ord"] = no
for attr in ["id", "account", "summary", "amount"]: for attr in ["id", "account", "summary", "amount"]:
if F"{record_type}-{old_no}-{attr}" in form: if F"{record_type}-{old_no}-{attr}" in post:
new_form[F"{record_type}-{no}-{attr}"]\ new_post[F"{record_type}-{no}-{attr}"]\
= form[F"{record_type}-{old_no}-{attr}"] = post[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 post.keys() if re.match(
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)", "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)",
x)]: x)]:
del form[x] del post[x]
for key in new_form.keys(): for key in new_post.keys():
form[key] = new_form[key] post[key] = new_post[key]
def make_transaction_form_from_model(transaction, exists): def make_txn_form_from_model(txn, exists):
"""Converts a transaction data model to a transaction form. """Converts a transaction data model to a transaction form.
Args: Args:
transaction (Transaction): The transaction data model. txn (Transaction): The transaction data model.
exists (bool): Whether the current transaction exists. exists (bool): Whether the current transaction exists.
Returns: Returns:
TransactionForm: The transaction form. TransactionForm: The transaction form.
""" """
transaction_form = TransactionForm( form = TransactionForm(
{x: str(getattr(transaction, x)) for x in ["date", "notes"] {x: str(getattr(txn, x)) for x in ["date", "notes"]
if getattr(transaction, x) is not None}) if getattr(txn, x) is not None})
transaction_form.transaction = transaction if exists else None form.transaction = txn if exists else None
for record in transaction.records: for record in txn.records:
data = {x: getattr(record, x) data = {x: getattr(record, x)
for x in ["summary", "amount"] for x in ["summary", "amount"]
if getattr(record, x) is not None} if getattr(record, x) is not None}
@ -393,31 +393,31 @@ def make_transaction_form_from_model(transaction, exists):
except AttributeError: except AttributeError:
pass pass
record_form = RecordForm(data) record_form = RecordForm(data)
record_form.transaction = transaction_form.transaction record_form.transaction = form.transaction
record_form.is_credit = record.is_credit record_form.is_credit = record.is_credit
if record.is_credit: if record.is_credit:
transaction_form.credit_records.append(record_form) form.credit_records.append(record_form)
else: else:
transaction_form.debit_records.append(record_form) form.debit_records.append(record_form)
return transaction_form return form
def make_transaction_form_from_post(post, txn_type, transaction): def make_txn_form_from_post(post, txn_type, txn):
"""Converts the POSTed data to a transaction form. """Converts the POSTed data to a transaction form.
Args: Args:
post (dict[str]): The POSTed data. post (dict[str]): The POSTed data.
txn_type (str): The transaction type. txn_type (str): The transaction type.
transaction (Transaction|None): The current transaction, or None txn (Transaction|None): The current transaction, or None
if there is no current transaction. if there is no current transaction.
Returns: Returns:
TransactionForm: The transaction form. TransactionForm: The transaction form.
""" """
transaction_form = TransactionForm( form = TransactionForm(
{x: post[x] for x in ("date", "notes") if x in post}) {x: post[x] for x in ("date", "notes") if x in post})
transaction_form.transaction = transaction form.transaction = txn
transaction_form.txn_type = txn_type form.txn_type = txn_type
# The records # The records
max_no = _find_max_record_no(post) max_no = _find_max_record_no(post)
if max_no["debit"] == 0: if max_no["debit"] == 0:
@ -429,27 +429,27 @@ def make_transaction_form_from_post(post, txn_type, transaction):
is_credit = (rec_type == "credit") is_credit = (rec_type == "credit")
for i in range(max_no[rec_type]): for i in range(max_no[rec_type]):
no = i + 1 no = i + 1
record = RecordForm( record_form = RecordForm(
{x: post[F"{rec_type}-{no}-{x}"] {x: post[F"{rec_type}-{no}-{x}"]
for x in ["id", "account", "summary", "amount"] for x in ["id", "account", "summary", "amount"]
if F"{rec_type}-{no}-{x}" in post}) if F"{rec_type}-{no}-{x}" in post})
record.transaction = transaction_form.transaction record_form.transaction = form.transaction
record.is_credit = is_credit record_form.is_credit = is_credit
records.append(record) records.append(record_form)
if rec_type == "debit": if rec_type == "debit":
transaction_form.debit_records = records form.debit_records = records
else: else:
transaction_form.credit_records = records form.credit_records = records
return transaction_form return form
def make_transaction_form_from_status(request, txn_type, transaction): def make_txn_form_from_status(request, txn_type, txn):
"""Converts the previously-stored status to a transaction form. """Converts the previously-stored status to a transaction form.
Args: Args:
request (HttpRequest): The request. request (HttpRequest): The request.
txn_type (str): The transaction type. txn_type (str): The transaction type.
transaction (Transaction|None): The current transaction, or None txn (Transaction|None): The current transaction, or None
if there is no current transaction. if there is no current transaction.
Returns: Returns:
@ -461,8 +461,8 @@ def make_transaction_form_from_status(request, txn_type, transaction):
return None return None
if "form" not in status: if "form" not in status:
return return
return make_transaction_form_from_post( return make_txn_form_from_post(
status["form"], txn_type, transaction) status["form"], txn_type, txn)
def _find_max_record_no(post): def _find_max_record_no(post):
@ -484,8 +484,8 @@ def _find_max_record_no(post):
"^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$", "^(debit|credit)-([1-9][0-9]*)-(id|ord|account|summary|amount)$",
key) key)
if m is not None: if m is not None:
rec_type = m.group(1) record_type = m.group(1)
no = int(m.group(2)) no = int(m.group(2))
if max_no[rec_type] < no: if max_no[record_type] < no:
max_no[rec_type] = no max_no[record_type] = no
return max_no return max_no

View File

@ -38,9 +38,9 @@ from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \
strip_form strip_form
from .models import Record, Transaction, Account, RecordSummary from .models import Record, Transaction, Account, RecordSummary
from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \ from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_transaction_from_post, \ find_imbalanced, find_order_holes, fill_txn_from_post, \
sort_form_transaction_records, make_transaction_form_from_status, \ sort_post_txn_records, make_txn_form_from_status, \
make_transaction_form_from_model, make_transaction_form_from_post make_txn_form_from_model, make_txn_form_from_post
@method_decorator(require_GET, name="dispatch") @method_decorator(require_GET, name="dispatch")
@ -822,7 +822,7 @@ def transaction_edit(request, txn_type, txn=None):
Returns: Returns:
HttpResponse: The response. HttpResponse: The response.
""" """
form = make_transaction_form_from_status(request, txn_type, txn) form = make_txn_form_from_status(request, txn_type, txn)
if form is None: if form is None:
exists = txn is not None exists = txn is not None
if txn is None: if txn is None:
@ -831,7 +831,7 @@ def transaction_edit(request, txn_type, txn=None):
txn.records.append(Record(ord=1, is_credit=False)) txn.records.append(Record(ord=1, is_credit=False))
if len(txn.credit_records) == 0: if len(txn.credit_records) == 0:
txn.records.append(Record(ord=1, is_credit=True)) txn.records.append(Record(ord=1, is_credit=True))
form = make_transaction_form_from_model(txn, exists) form = make_txn_form_from_model(txn, exists)
return render(request, F"accounting/transactions/{txn_type}/form.html", { return render(request, F"accounting/transactions/{txn_type}/form.html", {
"item": form, "item": form,
}) })
@ -852,8 +852,8 @@ def transaction_store(request, txn_type, txn=None):
""" """
post = request.POST.dict() post = request.POST.dict()
strip_form(post) strip_form(post)
sort_form_transaction_records(post) sort_post_txn_records(post)
form = make_transaction_form_from_post(post, txn_type, txn) form = make_txn_form_from_post(post, txn_type, txn)
if not form.is_valid(): if not form.is_valid():
if txn is None: if txn is None:
url = reverse("accounting:transactions.create", args=(txn_type,)) url = reverse("accounting:transactions.create", args=(txn_type,))
@ -866,7 +866,7 @@ def transaction_store(request, txn_type, txn=None):
post) post)
if txn is None: if txn is None:
txn = Transaction() txn = Transaction()
fill_transaction_from_post(txn, post) fill_txn_from_post(txn, post)
# TODO: Stores the data # TODO: Stores the data
return success_redirect( return success_redirect(
request, request,