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.
"""
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):
"""Returns the total amount of the credit records.
@ -303,7 +303,7 @@ class TransactionForm(forms.Form):
int: The total amount of the 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):

View File

@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2020/8/5
{% 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 }}">
<div id="{{ record_type }}-{{ no }}-error">{% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<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 record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<div class="d-flex justify-content-between">
<div class="row">
<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 }}" />
{% endif %}
<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 %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %}
@ -37,17 +37,17 @@ First written: 2020/8/5
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</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 class="col-lg-6">
<div class="row">
<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 }}" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div>
<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">{{ record.summary.errors.0|default:"" }}</div>
</div>
<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 }}" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div>
<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">{{ record.amount.errors.0|default:"" }}</div>
</div>
</div>
</div>

View File

@ -20,8 +20,8 @@ Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2020/8/5
{% 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 }}">
<div id="{{ record_type }}-{{ no }}-error">{% if should_validate and record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<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 record.non_field_errors %}{{ record.non_field_errors.0 }}{% endif %}</div>
<div class="d-flex">
<div>
<div class="row">
@ -30,7 +30,7 @@ First written: 2020/8/5
<input type="hidden" name="{{ record_type }}-{{ no }}-id" value="{{ record.id.value }}" />
{% endif %}
<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 %}
<option value="{{ record.account.value|default:"" }}" selected="selected">{{ record.account.value|default:"" }} {{ record.account_title|default:"" }}</option>
{% else %}
@ -38,17 +38,17 @@ First written: 2020/8/5
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</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="row">
<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 }}" />
<div id="{{ record_type }}-{{ no }}-summary-error" class="invalid-feedback">{% if should_validate %}{{ record.summary.errors.0|default:"" }}{% endif %}</div>
<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">{{ record.summary.errors.0|default:"" }}</div>
</div>
<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 }}" />
<div id="{{ record_type }}-{{ no }}-amount-error" class="invalid-feedback">{% if should_validate %}{{ record.amount.errors.0|default:"" }}{% endif %}</div>
<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">{{ record.amount.errors.0|default:"" }}</div>
</div>
</div>
</div>

View File

@ -62,8 +62,9 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div>
<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" />
<div id="txn-date-error" class="invalid-feedback">{% if should_validate %}{{ form.date.errors.0|default:"" }}{% endif %}</div>
{% now "Y-m-d" as today %}
<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>
@ -97,8 +98,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div>
<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>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div>
<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">{{ form.notes.errors.0|default:"" }}</div>
</div>
</div>

View File

@ -62,8 +62,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div>
<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" />
<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>
@ -97,8 +97,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div>
<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>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div>
<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">{{ form.notes.errors.0|default:"" }}</div>
</div>
</div>

View File

@ -62,8 +62,8 @@ First written: 2020/7/23
<label for="txn-date">{{ _("Date:")|force_escape }}</label>
</div>
<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" />
<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>
@ -85,11 +85,11 @@ First written: 2020/7/23
</button>
</li>
<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 }}
<span id="debit-total" class="amount">{{ form.debit_total }}</span>
</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>
</ul>
</div>
@ -111,11 +111,11 @@ First written: 2020/7/23
</button>
</li>
<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 }}
<span id="credit-total" class="amount">{{ form.credit_total }}</span>
</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>
</ul>
</div>
@ -126,8 +126,8 @@ First written: 2020/7/23
<label for="txn-note">{{ _("Notes:")|force_escape }}</label>
</div>
<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>
<div id="txn-note-error" class="invalid-feedback">{% if should_validate %}{{ form.notes.errors.0|default:"" }}{% endif %}</div>
<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">{{ form.notes.errors.0|default:"" }}</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, \
strip_post, new_pk, PaginationException
from mia_core.views import DeleteView
from .forms import AccountForm
from .forms import AccountForm, TransactionForm, RecordForm
from .models import Record, Transaction, Account
from .utils import get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes, fill_txn_from_post, \
@ -827,15 +827,12 @@ def txn_form(request, txn_type, txn=None):
HttpResponse: The response.
"""
form = make_txn_form_from_status(request, txn_type, txn)
should_validate = True
if form is None:
if txn is None:
txn = Transaction(date=timezone.localdate())
should_validate = False
if len(txn.debit_records) == 0:
txn.records.append(Record(ord=1, is_credit=False))
if len(txn.credit_records) == 0:
txn.records.append(Record(ord=1, is_credit=True))
form = TransactionForm()
form.debit_records.append(RecordForm())
form.credit_records.append(RecordForm())
else:
form = make_txn_form_from_model(txn_type, txn)
new_record_context = {"record": Record(),
"record_type": "TTT",
@ -852,7 +849,6 @@ def txn_form(request, txn_type, txn=None):
return render(request, F"accounting/transaction_form-{txn_type}.html", {
"form": form,
"summary_categories": get_summary_categories,
"should_validate": should_validate,
"new_record_template": new_record_template,
})