Added the fill_transaction_from_form() utility and moved most of the code in fill_transaction_from_previous_form() to there, added the sort_form_transaction_records() utility, and applied the two utilities in the transaction_store() view in the accounting application.
This commit is contained in:
		| @@ -294,19 +294,13 @@ 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_previous_form(request, transaction): | def fill_transaction_from_form(transaction, form): | ||||||
|     """Fills the transaction from the previously-stored form. |     """Fills the transaction from the form. | ||||||
|  |  | ||||||
|     Args: |     Args: | ||||||
|         request (HttpRequest): The request. |  | ||||||
|         transaction (Transaction): The transaction. |         transaction (Transaction): The transaction. | ||||||
|  |         form (dict): The form | ||||||
|     """ |     """ | ||||||
|     status = retrieve_status(request) |  | ||||||
|     if status is None: |  | ||||||
|         return |  | ||||||
|     if "form" not in status: |  | ||||||
|         return |  | ||||||
|     form = status["form"] |  | ||||||
|     if "date" in form: |     if "date" in form: | ||||||
|         transaction.date = form["date"] |         transaction.date = form["date"] | ||||||
|     if "notes" in form: |     if "notes" in form: | ||||||
| @@ -353,3 +347,93 @@ def fill_transaction_from_previous_form(request, transaction): | |||||||
|             record.amount = form[F"credit-{no}-amount"] |             record.amount = form[F"credit-{no}-amount"] | ||||||
|         records.append(record) |         records.append(record) | ||||||
|     transaction.records = records |     transaction.records = records | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def fill_transaction_from_previous_form(request, transaction): | ||||||
|  |     """Fills the transaction from the previously-stored form. | ||||||
|  |  | ||||||
|  |     Args: | ||||||
|  |         request (HttpRequest): The request | ||||||
|  |         transaction (Transaction): The transaction. | ||||||
|  |     """ | ||||||
|  |     status = retrieve_status(request) | ||||||
|  |     if status is None: | ||||||
|  |         return | ||||||
|  |     if "form" not in status: | ||||||
|  |         return | ||||||
|  |     form = status["form"] | ||||||
|  |     fill_transaction_from_form(transaction, form) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | def sort_form_transaction_records(form): | ||||||
|  |     """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: | ||||||
|  |         form (dict): The POSTed form. | ||||||
|  |     """ | ||||||
|  |     # Collects the available record numbers | ||||||
|  |     debit_no = [] | ||||||
|  |     credit_no = [] | ||||||
|  |     for key in form.keys(): | ||||||
|  |         m = re.match( | ||||||
|  |             "^debit-([1-9][0-9]*)-(sn|ord|account|summary|amount)", key) | ||||||
|  |         if m is not None: | ||||||
|  |             no = int(m.group(1)) | ||||||
|  |             if no not in debit_no: | ||||||
|  |                 debit_no.append(no) | ||||||
|  |         m = re.match( | ||||||
|  |             "^credit-([1-9][0-9]*)-(sn|ord|account|summary|amount)", key) | ||||||
|  |         if m is not None: | ||||||
|  |             no = int(m.group(1)) | ||||||
|  |             if no not in credit_no: | ||||||
|  |                 credit_no.append(no) | ||||||
|  |     # Sorts these record numbers by their specified orders | ||||||
|  |     debit_orders = {} | ||||||
|  |     for no in debit_no: | ||||||
|  |         try: | ||||||
|  |             debit_orders[no] = int(form[F"debit-{no}-ord"]) | ||||||
|  |         except KeyError: | ||||||
|  |             debit_orders[no] = 9999 | ||||||
|  |         except ValueError: | ||||||
|  |             debit_orders[no] = 9999 | ||||||
|  |     debit_no.sort(key=lambda x: debit_orders[x]) | ||||||
|  |     credit_orders = {} | ||||||
|  |     for no in credit_no: | ||||||
|  |         try: | ||||||
|  |             credit_orders[no] = int(form[F"credit-{no}-ord"]) | ||||||
|  |         except KeyError: | ||||||
|  |             credit_orders[no] = 9999 | ||||||
|  |         except ValueError: | ||||||
|  |             credit_orders[no] = 9999 | ||||||
|  |     credit_no.sort(key=lambda x: credit_orders[x]) | ||||||
|  |     # Constructed the sorted new form | ||||||
|  |     new_form = {} | ||||||
|  |     for i in range(len(debit_no)): | ||||||
|  |         old_no = debit_no[i] | ||||||
|  |         no = i + 1 | ||||||
|  |         new_form[F"debit-{no}-ord"] = no | ||||||
|  |         for attr in ["sn", "account", "summary", "amount"]: | ||||||
|  |             if F"debit-{old_no}-{attr}" in form: | ||||||
|  |                 new_form[F"debit-{no}-{attr}"]\ | ||||||
|  |                     = form[F"debit-{old_no}-{attr}"] | ||||||
|  |     for i in range(len(credit_no)): | ||||||
|  |         old_no = credit_no[i] | ||||||
|  |         no = i + 1 | ||||||
|  |         new_form[F"credit-{no}-ord"] = no | ||||||
|  |         for attr in ["sn", "account", "summary", "amount"]: | ||||||
|  |             if F"credit-{old_no}-{attr}" in form: | ||||||
|  |                 new_form[F"credit-{no}-{attr}"]\ | ||||||
|  |                     = form[F"credit-{old_no}-{attr}"] | ||||||
|  |     # Purges the old form and fills it with the new form | ||||||
|  |     old_keys = [] | ||||||
|  |     for key in form.keys(): | ||||||
|  |         m = re.match( | ||||||
|  |             "^(debit|credit)-([1-9][0-9]*)-(sn|ord|account|summary|amount)", | ||||||
|  |             key) | ||||||
|  |         if m is not None: | ||||||
|  |             old_keys.append(key) | ||||||
|  |     for x in old_keys: | ||||||
|  |         del form[x] | ||||||
|  |     for key in new_form.keys(): | ||||||
|  |         form[key] = new_form[key] | ||||||
|   | |||||||
| @@ -30,10 +30,11 @@ from django.utils import dateformat, timezone | |||||||
| from django.utils.translation import pgettext, gettext_noop | from django.utils.translation import pgettext, gettext_noop | ||||||
| from django.views.decorators.http import require_GET, require_POST | from django.views.decorators.http import require_GET, require_POST | ||||||
|  |  | ||||||
| from mia_core.status import success_redirect | from mia_core.status import success_redirect, retrieve_status, error_redirect | ||||||
| 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_previous_form |     find_imbalanced, find_order_holes, fill_transaction_from_form, \ | ||||||
|  |     sort_form_transaction_records, fill_transaction_from_previous_form | ||||||
| from mia_core.digest_auth import digest_login_required | from mia_core.digest_auth import digest_login_required | ||||||
| from mia_core.period import Period | from mia_core.period import Period | ||||||
| from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder | from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder | ||||||
| @@ -901,6 +902,23 @@ def transaction_store(request, type, transaction=None): | |||||||
|     """ |     """ | ||||||
|     if transaction is None: |     if transaction is None: | ||||||
|         transaction = Transaction() |         transaction = Transaction() | ||||||
|  |     form = request.POST.dict() | ||||||
|  |     sort_form_transaction_records(form) | ||||||
|  |     fill_transaction_from_form(transaction, form) | ||||||
|  |     # TODO: To be done. | ||||||
|  |     if transaction.pk is None: | ||||||
|  |         url = reverse("accounting:transactions.create", args=(type,)) | ||||||
|  |     else: | ||||||
|  |         url = reverse("accounting:transactions.edit", args=(type, transaction)) | ||||||
|  |     return error_redirect( | ||||||
|  |         request, | ||||||
|  |         str(UrlBuilder(url).add_param("r", request.GET.get("r"))), | ||||||
|  |         form, | ||||||
|  |         { | ||||||
|  |             "date": "The date is error.", | ||||||
|  |             "debit-2-amount": "The amount is error.", | ||||||
|  |         } | ||||||
|  |     ) | ||||||
|     return success_redirect( |     return success_redirect( | ||||||
|         request, |         request, | ||||||
|         str(UrlBuilder(reverse("accounting:transactions.show", |         str(UrlBuilder(reverse("accounting:transactions.show", | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user