Moved the make_txn_form_from_model utility to the from_model method in the TransactionForm in the accounting application.

This commit is contained in:
依瑪貓 2020-08-19 13:43:27 +08:00
parent 6f019d23c0
commit 790e8da908
3 changed files with 42 additions and 44 deletions

View File

@ -23,6 +23,7 @@ import re
from typing import Optional, List, Dict from typing import Optional, List, Dict
from django import forms from django import forms
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
from django.db.models import Q, Max, Model from django.db.models import Q, Max, Model
from django.db.models.functions import Length from django.db.models.functions import Length
@ -295,6 +296,46 @@ class TransactionForm(forms.Form):
for key in new_post.keys(): for key in new_post.keys():
post[key] = new_post[key] post[key] = new_post[key]
@staticmethod
def from_model(txn: Transaction, txn_type: str):
"""Converts a transaction data model to a transaction form.
Args:
txn_type: The transaction type.
txn: The transaction data model.
Returns:
The transaction form.
"""
form = TransactionForm(
{x: str(getattr(txn, x)) for x in ["date", "notes"]
if getattr(txn, x) is not None})
form.transaction = txn if txn.pk is not None else None
form.txn_type = txn_type
records = []
if txn_type != "income":
records = records + txn.debit_records
if txn_type != "expense":
records = records + txn.credit_records
for record in records:
data = {x: getattr(record, x)
for x in ["summary", "amount"]
if getattr(record, x) is not None}
if record.pk is not None:
data["id"] = record.pk
try:
data["account"] = record.account.code
except ObjectDoesNotExist:
pass
record_form = RecordForm(data)
record_form.txn_form = form
record_form.is_credit = record.is_credit
if record.is_credit:
form.credit_records.append(record_form)
else:
form.debit_records.append(record_form)
return form
def clean(self): def clean(self):
"""Validates the form globally. """Validates the form globally.

View File

@ -24,7 +24,6 @@ import re
from typing import Union, Tuple, List, Optional, Iterable, Mapping, Dict from typing import Union, Tuple, List, Optional, Iterable, Mapping, Dict
from django.conf import settings from django.conf import settings
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Q, Sum, Case, When, F, Count, Max, Min, Value, \ from django.db.models import Q, Sum, Case, When, F, Count, Max, Min, Value, \
CharField CharField
from django.db.models.functions import StrIndex, Left from django.db.models.functions import StrIndex, Left
@ -35,7 +34,6 @@ from django.utils.translation import gettext as _
from mia_core.period import Period from mia_core.period import Period
from mia_core.templatetags.mia_core import smart_month from mia_core.templatetags.mia_core import smart_month
from mia_core.utils import new_pk from mia_core.utils import new_pk
from .forms import TransactionForm, RecordForm
from .models import Account, Transaction, Record from .models import Account, Transaction, Record
AccountData = Tuple[Union[str, int], str, str, str] AccountData = Tuple[Union[str, int], str, str, str]
@ -580,47 +578,6 @@ def fill_txn_from_post(txn_type: str, txn: Transaction,
txn.records = records txn.records = records
def make_txn_form_from_model(txn_type: str,
txn: Transaction) -> TransactionForm:
"""Converts a transaction data model to a transaction form.
Args:
txn_type: The transaction type.
txn: The transaction data model.
Returns:
The transaction form.
"""
form = TransactionForm(
{x: str(getattr(txn, x)) for x in ["date", "notes"]
if getattr(txn, x) is not None})
form.transaction = txn if txn.pk is not None else None
form.txn_type = txn_type
records = []
if txn_type != "income":
records = records + txn.debit_records
if txn_type != "expense":
records = records + txn.credit_records
for record in records:
data = {x: getattr(record, x)
for x in ["summary", "amount"]
if getattr(record, x) is not None}
if record.pk is not None:
data["id"] = record.pk
try:
data["account"] = record.account.code
except ObjectDoesNotExist:
pass
record_form = RecordForm(data)
record_form.txn_form = form
record_form.is_credit = record.is_credit
if record.is_credit:
form.credit_records.append(record_form)
else:
form.debit_records.append(record_form)
return form
def _find_max_record_no(txn_type: str, def _find_max_record_no(txn_type: str,
post: Mapping[str, str]) -> Dict[str, int]: post: Mapping[str, str]) -> Dict[str, int]:
"""Finds the max debit and record numbers from the POSTed form. """Finds the max debit and record numbers from the POSTed form.

View File

@ -827,7 +827,7 @@ class TransactionFormView(FormView):
def make_form_from_model(self, obj: Transaction) -> TransactionForm: def make_form_from_model(self, obj: Transaction) -> TransactionForm:
"""Creates and returns the form from a data model.""" """Creates and returns the form from a data model."""
return utils.make_txn_form_from_model(self.txn_type, obj) return TransactionForm.from_model(obj, self.txn_type)
def fill_model_from_form(self, obj: Transaction, def fill_model_from_form(self, obj: Transaction,
form: TransactionForm) -> None: form: TransactionForm) -> None: