Revised the txn_form view to constructs the new transaction form without having to bother the make_txn_form_from_model() utility, in order not to mess up with the is_bound form property, to replace the need for the should_validate template variable, and removed all the should_validate template variables from the transaction form templates in the accounting application.

This commit is contained in:
依瑪貓 2020-08-12 11:59:50 +08:00
parent 5fc337b39d
commit 05892b9655
7 changed files with 41 additions and 44 deletions

View File

@ -294,7 +294,7 @@ class TransactionForm(forms.Form):
int: The total amount of the credit records. int: The total amount of the credit records.
""" """
return sum([int(x.data["amount"]) for x in self.debit_records return sum([int(x.data["amount"]) for x in self.debit_records
if "amount" not in x.errors]) if "amount" in x.data and "amount" not in x.errors])
def credit_total(self): def credit_total(self):
"""Returns the total amount of the credit records. """Returns the total amount of the credit records.
@ -303,7 +303,7 @@ class TransactionForm(forms.Form):
int: The total amount of the credit records. int: The total amount of the credit records.
""" """
return sum([int(x.data["amount"]) for x in self.credit_records return sum([int(x.data["amount"]) for x in self.credit_records
if "amount" not in x.errors]) if "amount" in x.data and "amount" not in x.errors])
class AccountForm(forms.Form): class AccountForm(forms.Form):

View File

@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2020/8/5 First written: 2020/8/5
{% endcomment %} {% endcomment %}
<li id="{{ record_type }}-{{ no }}" class="list-group-item {% if should_validate and record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}"> <li id="{{ record_type }}-{{ no }}" class="list-group-item {% if record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}">
<div id="{{ record_type }}-{{ no }}-error">{% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-error">{% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<div class="d-flex justify-content-between"> <div class="d-flex justify-content-between">
<div class="row"> <div class="row">
<div class="col-lg-6"> <div class="col-lg-6">
@ -29,7 +29,7 @@ First written: 2020/8/5
<input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" /> <input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" />
{% endif %} {% endif %}
<input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" /> <input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" />
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if should_validate and record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account" data-type="{{ record_type }}"> <select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account" data-type="{{ record_type }}">
{% if record.account is not None %} {% if record.account is not None %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option> <option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %} {% else %}
@ -37,17 +37,17 @@ First written: 2020/8/5
{% endif %} {% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option> <option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select> </select>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{{ record.account.errors.0|default:"" }}</div>
</div> </div>
<div class="col-lg-6"> <div class="col-lg-6">
<div class="row"> <div class="row">
<div class="col-sm-8"> <div class="col-sm-8">
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if should_validate and record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" data-type="{{ record_type }}" data-no="{{ no }}" /> <input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" data-type="{{ record_type }}" data-no="{{ no }}" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{{ record.summary.errors.0|default:"" }}</div>
</div> </div>
<div class="col-sm-4"> <div class="col-sm-4">
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if should_validate and record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" /> <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2020/8/5 First written: 2020/8/5
{% endcomment %} {% endcomment %}
<li id="{{ record_type }}-{{ no }}" class="list-group-item {% if should_validate and record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}"> <li id="{{ record_type }}-{{ no }}" class="list-group-item {% if record.non_field_errors %} list-group-item-danger {% endif %} draggable-record {{ record_type }}-record" data-no="{{ no }}">
<div id="{{ record_type }}-{{ no }}-error">{% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-error">{% if record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<div class="d-flex"> <div class="d-flex">
<div> <div>
<div class="row"> <div class="row">
@ -30,7 +30,7 @@ First written: 2020/8/5
<input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" /> <input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" />
{% endif %} {% endif %}
<input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" /> <input id="{{ record_type }}-{{ no }}-ord" class="{{ record_type }}-ord" type="hidden" name="{{ record_type }}-{{ no }}-ord" value="{{ order }}" />
<select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if should_validate and record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account" data-type="{{ record_type }}"> <select id="{{ record_type }}-{{ no }}-account" class="form-control record-account {{ record_type }}-account {% if record.account.errors %} is-invalid {% endif %}" name="{{ record_type }}-{{ no }}-account" data-type="{{ record_type }}">
{% if record.account is not None %} {% if record.account is not None %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option> <option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %} {% else %}
@ -38,17 +38,17 @@ First written: 2020/8/5
{% endif %} {% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option> <option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select> </select>
<div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{% if should_validate %}{{ record.account.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-account-error" class="invalid-feedback">{{ record.account.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-lg-8"> <div class="col-lg-8">
<input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if should_validate and record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" data-type="{{ record_type }}" data-no="{{ no }}" /> <input id="{{ record_type }}-{{ no }}-summary" class="form-control record-summary {% if record.summary.errors %} is-invalid {% endif %}" type="text" name="{{ record_type }}-{{ no }}-summary" value="{{ record.summary.value|default:"" }}" maxlength="128" data-toggle="modal" data-target="#summary-modal" data-type="{{ record_type }}" data-no="{{ no }}" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{{ record.summary.errors.0|default:"" }}</div>
</div> </div>
<div class="col-lg-4"> <div class="col-lg-4">
<input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if should_validate and record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" /> <input id="{{ record_type }}-{{ no }}-amount" class="form-control record-amount {{ record_type }}-to-sum {% if record.amount.errors %} is-invalid {% endif %}" type="number" min="1" name="{{ record_type }}-{{ no }}-amount" value="{{ record.amount.value|default:"" }}" required="required" data-type="{{ record_type }}" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div> <div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{{ record.amount.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -62,8 +62,9 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label> <label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<input id="txn-date" class="form-control {% if should_validate and form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ form.date.value }}" required="required" /> {% now "Y-m-d" as today %}
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}</div> <input id="txn-date" class="form-control {% if form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{% if form.is_bound %}{{ form.date.value }}{% else %}{% now "Y-m-d" %}{% endif %}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ form.date.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
@ -97,8 +98,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label> <label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if should_validate and form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea> <textarea id="txn-note" class="form-control {% if form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div> <div id="txn-note-error" class="invalid-feedback">{{ form.notes.errors.0|default:"" }}</div>
</div> </div>
</div> </div>

View File

@ -62,8 +62,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label> <label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<input id="txn-date" class="form-control {% if should_validate and form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ form.date.value }}" required="required" /> <input id="txn-date" class="form-control {% if form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{% if form.is_bound %}{{ form.date.value }}{% else %}{% now "Y-m-d" %}{% endif %}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}</div> <div id="txn-date-error" class="invalid-feedback">{{ form.date.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
@ -97,8 +97,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label> <label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if should_validate and form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea> <textarea id="txn-note" class="form-control {% if form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div> <div id="txn-note-error" class="invalid-feedback">{{ form.notes.errors.0|default:"" }}</div>
</div> </div>
</div> </div>

View File

@ -62,8 +62,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label> <label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<input id="txn-date" class="form-control {% if should_validate and form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{{ form.date.value }}" required="required" /> <input id="txn-date" class="form-control {% if form.date.errors %} is-invalid {% endif %}" type="date" name="date" value="{% if form.is_bound %}{{ form.date.value }}{% else %}{% now "Y-m-d" %}{% endif %}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}</div> <div id="txn-date-error" class="invalid-feedback">{{ form.date.errors.0|default:"" }}</div>
</div> </div>
</div> </div>
@ -85,11 +85,11 @@ First written: 2020/7/23
</button> </button>
</li> </li>
<li class="list-group-item"> <li class="list-group-item">
<div id="debit-total-row" class="d-flex justify-content-between align-items-center form-control {% if should_validate and form.balance_error %} is-invalid {% endif %} balance-row"> <div id="debit-total-row" class="d-flex justify-content-between align-items-center form-control {% if form.balance_error %} is-invalid {% endif %} balance-row">
{{ _("Total")|force_escape }} {{ _("Total")|force_escape }}
<span id="debit-total" class="amount">{{ form.debit_total }}</span> <span id="debit-total" class="amount">{{ form.debit_total }}</span>
</div> </div>
<div id="debit-total-error" class="invalid-feedback balance-error">{% if should_validate %}{{ form.balance_error|default:"" }}{% endif %}</div> <div id="debit-total-error" class="invalid-feedback balance-error">{{ form.balance_error|default:"" }}</div>
</li> </li>
</ul> </ul>
</div> </div>
@ -111,11 +111,11 @@ First written: 2020/7/23
</button> </button>
</li> </li>
<li class="list-group-item"> <li class="list-group-item">
<div id="credit-total-row" class="d-flex justify-content-between align-items-center form-control {% if should_validate and form.balance_error %} is-invalid {% endif %} balance-row"> <div id="credit-total-row" class="d-flex justify-content-between align-items-center form-control {% if form.balance_error %} is-invalid {% endif %} balance-row">
{{ _("Total")|force_escape }} {{ _("Total")|force_escape }}
<span id="credit-total" class="amount">{{ form.credit_total }}</span> <span id="credit-total" class="amount">{{ form.credit_total }}</span>
</div> </div>
<div id="credit-total-error" class="invalid-feedback balance-error">{% if should_validate %}{{ form.balance_error|default:"" }}{% endif %}</div> <div id="credit-total-error" class="invalid-feedback balance-error">{{ form.balance_error|default:"" }}</div>
</li> </li>
</ul> </ul>
</div> </div>
@ -126,8 +126,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label> <label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div> </div>
<div class="col-sm-10"> <div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if should_validate and form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea> <textarea id="txn-note" class="form-control {% if form.notes.errors %} is-invalid {% endif %}" name="notes">{{ form.notes.value|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div> <div id="txn-note-error" class="invalid-feedback">{{ form.notes.errors.0|default:"" }}</div>
</div> </div>
</div> </div>

View File

@ -42,7 +42,7 @@ 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, \
strip_post, new_pk, PaginationException strip_post, new_pk, PaginationException
from mia_core.views import DeleteView from mia_core.views import DeleteView
from .forms import AccountForm from .forms import AccountForm, TransactionForm, RecordForm
from .models import Record, Transaction, Account from .models import Record, Transaction, Account
from .utils import get_cash_accounts, get_ledger_accounts, \ from .utils import get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_txn_from_post, \ find_imbalanced, find_order_holes, fill_txn_from_post, \
@ -827,16 +827,13 @@ def txn_form(request, txn_type, txn=None):
HttpResponse: The response. HttpResponse: The response.
""" """
form = make_txn_form_from_status(request, txn_type, txn) form = make_txn_form_from_status(request, txn_type, txn)
should_validate = True
if form is None: if form is None:
if txn is None: if txn is None:
txn = Transaction(date=timezone.localdate()) form = TransactionForm()
should_validate = False form.debit_records.append(RecordForm())
if len(txn.debit_records) == 0: form.credit_records.append(RecordForm())
txn.records.append(Record(ord=1, is_credit=False)) else:
if len(txn.credit_records) == 0: form = make_txn_form_from_model(txn_type, txn)
txn.records.append(Record(ord=1, is_credit=True))
form = make_txn_form_from_model(txn_type, txn)
new_record_context = {"record": Record(), new_record_context = {"record": Record(),
"record_type": "TTT", "record_type": "TTT",
"no": "NNN", "no": "NNN",
@ -852,7 +849,6 @@ def txn_form(request, txn_type, txn=None):
return render(request, F"accounting/transaction_form-{txn_type}.html", { return render(request, F"accounting/transaction_form-{txn_type}.html", {
"form": form, "form": form,
"summary_categories": get_summary_categories, "summary_categories": get_summary_categories,
"should_validate": should_validate,
"new_record_template": new_record_template, "new_record_template": new_record_template,
}) })