Added the dict template filter to obtain a value from a dictionary in the Mia core application, and applied it in the templates of the forms of the transactions in the acccounting application.

This commit is contained in:
依瑪貓 2020-07-28 22:11:54 +08:00
parent d6df2496be
commit 9ab325a1c7
4 changed files with 73 additions and 40 deletions

View File

@ -52,8 +52,8 @@ First written: 2020/7/23
<label for="txn-date">{% trans "Date:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if errors.date %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors.date|default:"" }}</div>
<input id="txn-date" class="form-control {% if errors|dict:"date" %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors|dict:"date"|default:"" }}</div>
</div>
</div>
@ -68,7 +68,8 @@ First written: 2020/7/23
<input type="hidden" name="debit-{{ forloop.counter }}-sn" value="{{ x.pk }}" />
{% endif %}
<input id="debit-{{ forloop.counter }}-ord" class="debit-ord" type="hidden" name="debit-{{ forloop.counter }}-ord" value="{{ x.ord }}" />
<select id="debit-{{ forloop.counter }}-account" class="form-control record-account debit-account {% if errors.debit.account|index:forloop.counter %} is-invalid {% endif %}" name="debit-{{ forloop.counter }}-account">
{% str_format "debit-{}-account" forloop.counter as field %}
<select id="{{ field }}" class="form-control record-account debit-account {% if errors|dict:field %} is-invalid {% endif %}" name="{{ field }}">
{% if x.account is not None %}
<option value="{{ x.account.pk }}" selected="selected">{{ x.account.code }} {{ x.account.title }}</option>
{% else %}
@ -76,17 +77,19 @@ First written: 2020/7/23
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="debit-{{ forloop.counter }}-account-error" class="invalid-feedback">{{ errors.debit.account|index:forloop.counter }}</div>
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-lg-6">
<div class="row">
<div class="col-sm-8">
<input id="debit-{{ forloop.counter }}-summary" class="form-control record-summary {% if errors.debit.summary|index:forloop.counter %} is-invalid {% endif %}" type="text" name="debit-{{ forloop.counter }}-summary" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="debit-{{ forloop.counter }}-summary-error" class="invalid-feedback">{{ errors.debit.summary|index:forloop.counter }}</div>
{% str_format "debit-{}-summary" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-summary {% if errors|dict:field %} is-invalid {% endif %}" type="text" name="{{ field }}" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-sm-4">
<input id="debit-{{ forloop.counter }}-amount" class="form-control record-amount debit-to-sum {% if errors.debit.amount|index:forloop.counter %} is-invalid {% endif %}" type="number" min="1" name="debit-{{ forloop.counter }}-amount" value="{{ x.amount|default:"" }}" required="required" />
<div id="debit-{{ forloop.counter }}-amount-error" class="invalid-feedback">{{ errors.debit.amount|index:forloop.counter }}</div>
{% str_format "debit-{}-amount" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-amount debit-to-sum {% if errors|dict:field %} is-invalid {% endif %}" type="number" min="1" name="{{ field }}" value="{{ x.amount|default:"" }}" required="required" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
</div>
@ -133,8 +136,8 @@ First written: 2020/7/23
<label for="txn-note">{% trans "Notes:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if errors.notes %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors.notes|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if errors|dict:"notes" %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors|dict:"notes"|default:"" }}</div>
</div>
</div>

View File

@ -52,8 +52,8 @@ First written: 2020/7/23
<label for="txn-date">{% trans "Date:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if errors.date %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors.date|default:"" }}</div>
<input id="txn-date" class="form-control {% if errors|dict:"date" %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors|dict:"date"|default:"" }}</div>
</div>
</div>
@ -68,7 +68,8 @@ First written: 2020/7/23
<input type="hidden" name="credit-{{ forloop.counter }}-sn" value="{{ x.pk }}" />
{% endif %}
<input id="credit-{{ forloop.counter }}-ord" class="credit-ord" type="hidden" name="credit-{{ forloop.counter }}-ord" value="{{ x.ord }}" />
<select id="credit-{{ forloop.counter }}-account" class="form-control record-account credit-account {% if errors.credit.account|index:forloop.counter %} is-invalid {% endif %}" name="credit-{{ forloop.counter }}-account">
{% str_format "credit-{}-account" forloop.counter as field %}
<select id="{{ field }}" class="form-control record-account credit-account {% if errors|dict:field %} is-invalid {% endif %}" name="{{ field }}">
{% if x.account is not None %}
<option value="{{ x.account.pk }}" selected="selected">{{ x.account.code }} {{ x.account.title }}</option>
{% else %}
@ -76,17 +77,19 @@ First written: 2020/7/23
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="credit-{{ forloop.counter }}-account-error" class="invalid-feedback">{{ errors.credit.account|index:forloop.counter }}</div>
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-lg-6">
<div class="row">
<div class="col-sm-8">
<input id="credit-{{ forloop.counter }}-summary" class="form-control record-summary {% if errors.credit.summary|index:forloop.counter %} is-invalid {% endif %}" type="text" name="credit-{{ forloop.counter }}-summary" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="credit-{{ forloop.counter }}-summary-error" class="invalid-feedback">{{ errors.credit.summary|index:forloop.counter }}</div>
{% str_format "credit-{}-summary" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-summary {% if errors|dict:field %} is-invalid {% endif %}" type="text" name="{{ field }}" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-sm-4">
<input id="credit-{{ forloop.counter }}-amount" class="form-control record-amount credit-to-sum {% if errors.credit.amount|index:forloop.counter %} is-invalid {% endif %}" type="number" min="1" name="credit-{{ forloop.counter }}-amount" value="{{ x.amount|default:"" }}" required="required" />
<div id="credit-{{ forloop.counter }}-amount-error" class="invalid-feedback">{{ errors.credit.amount|index:forloop.counter }}</div>
{% str_format "credit-{}-amount" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-amount credit-to-sum {% if errors|dict:field %} is-invalid {% endif %}" type="number" min="1" name="{{ field }}" value="{{ x.amount|default:"" }}" required="required" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
</div>
@ -133,8 +136,8 @@ First written: 2020/7/23
<label for="txn-note">{% trans "Notes:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if errors.notes %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors.notes|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if errors|dict:"notes" %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors|dict:"notes"|default:"" }}</div>
</div>
</div>

View File

@ -55,8 +55,8 @@ First written: 2020/7/23
<label for="txn-date">{% trans "Date:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<input id="txn-date" class="form-control {% if errors.date %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors.date|default:"" }}</div>
<input id="txn-date" class="form-control {% if errors|dict:"date" %} is-invalid {% endif %}" type="date" name="date" value="{{ item.date|date:"Y-m-d" }}" required="required" />
<div id="txn-date-error" class="invalid-feedback">{{ errors|dict:"date"|default:"" }}</div>
</div>
</div>
@ -74,7 +74,8 @@ First written: 2020/7/23
<input type="hidden" name="debit-{{ forloop.counter }}-sn" value="{{ x.pk }}" />
{% endif %}
<input id="debit-{{ forloop.counter }}-ord" class="debit-ord" type="hidden" name="debit-{{ forloop.counter }}-ord" value="{{ x.ord }}" />
<select id="debit-{{ forloop.counter }}-account" class="form-control record-account debit-account {% if errors.debit.account|index:forloop.counter %} is-invalid {% endif %}" name="debit-{{ forloop.counter }}-account">
{% str_format "debit-{}-account" forloop.counter as field %}
<select id="{{ field }}" class="form-control record-account debit-account {% if errors|dict:field %} is-invalid {% endif %}" name="{{ field }}">
{% if x.account is not None %}
<option value="{{ x.account.pk }}" selected="selected">{{ x.account.code }} {{ x.account.title }}</option>
{% else %}
@ -82,17 +83,19 @@ First written: 2020/7/23
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="debit-{{ forloop.counter }}-account-error" class="invalid-feedback">{{ errors.debit.account|index:forloop.counter }}</div>
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<input id="debit-{{ forloop.counter }}-summary" class="form-control record-summary {% if errors.debit.summary|index:forloop.counter %} is-invalid {% endif %}" type="text" name="debit-{{ forloop.counter }}-summary" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="debit-{{ forloop.counter }}-summary-error" class="invalid-feedback">{{ errors.debit.summary|index:forloop.counter }}</div>
{% str_format "debit-{}-summary" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-summary {% if errors|dict:field %} is-invalid {% endif %}" type="text" name="{{ field }}" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-lg-4">
<input id="debit-{{ forloop.counter }}-amount" class="form-control record-amount debit-to-sum {% if errors.debit.amount|index:forloop.counter %} is-invalid {% endif %}" type="number" min="1" name="debit-{{ forloop.counter }}-amount" value="{{ x.amount|default:"" }}" required="required" />
<div id="debit-{{ forloop.counter }}-amount-error" class="invalid-feedback">{{ errors.debit.amount|index:forloop.counter }}</div>
{% str_format "debit-{}-amount" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-amount debit-to-sum {% if errors|dict:field %} is-invalid {% endif %}" type="number" min="1" name="{{ field }}" value="{{ x.amount|default:"" }}" required="required" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
</div>
@ -116,11 +119,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 errors.balance %} is-invalid {% endif %} balance-row">
<div id="debit-total-row" class="d-flex justify-content-between align-items-center form-control {% if errors|dict:"balance" %} is-invalid {% endif %} balance-row">
{% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
<span id="debit-total" class="amount">{{ item.debit_total }}</span>
</div>
<div id="debit-total-error" class="invalid-feedback balance-error">{{ errors.balance }}</div>
<div id="debit-total-error" class="invalid-feedback balance-error">{{ errors|dict:"balance"|default:"" }}</div>
</li>
</ul>
</div>
@ -138,7 +141,8 @@ First written: 2020/7/23
<input type="hidden" name="credit-{{ forloop.counter }}-sn" value="{{ x.pk }}" />
{% endif %}
<input id="credit-{{ forloop.counter }}-ord" class="credit-ord" type="hidden" name="credit-{{ forloop.counter }}-ord" value="{{ x.ord }}" />
<select id="credit-{{ forloop.counter }}-account" class="form-control record-account credit-account {% if errors.credit.account|index:forloop.counter %} is-invalid {% endif %}" name="credit-{{ forloop.counter }}-account">
{% str_format "credit-{}-account" forloop.counter as field %}
<select id="{{ field }}" class="form-control record-account credit-account {% if errors|dict:field %} is-invalid {% endif %}" name="{{ field }}">
{% if x.account is not None %}
<option value="{{ x.account.pk }}" selected="selected">{{ x.account.code }} {{ x.account.title }}</option>
{% else %}
@ -146,17 +150,19 @@ First written: 2020/7/23
{% endif %}
<option value="">XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</option>
</select>
<div id="credit-{{ forloop.counter }}-account-error" class="invalid-feedback">{{ errors.credit.account|index:forloop.counter }}</div>
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<input id="credit-{{ forloop.counter }}-summary" class="form-control record-summary {% if errors.credit.summary|index:forloop.counter %} is-invalid {% endif %}" type="text" name="credit-{{ forloop.counter }}-summary" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="credit-{{ forloop.counter }}-summary-error" class="invalid-feedback">{{ errors.credit.summary|index:forloop.counter }}</div>
{% str_format "credit-{}-summary" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-summary {% if errors|dict:field %} is-invalid {% endif %}" type="text" name="{{ field }}" value="{{ x.summary|default:"" }}" maxlength="128" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
<div class="col-lg-4">
<input id="credit-{{ forloop.counter }}-amount" class="form-control record-amount credit-to-sum {% if errors.credit.amount|index:forloop.counter %} is-invalid {% endif %}" type="number" min="1" name="credit-{{ forloop.counter }}-amount" value="{{ x.amount|default:"" }}" required="required" />
<div id="credit-{{ forloop.counter }}-amount-error" class="invalid-feedback">{{ errors.credit.amount|index:forloop.counter }}</div>
{% str_format "credit-{}-amount" forloop.counter as field %}
<input id="{{ field }}" class="form-control record-amount credit-to-sum {% if errors|dict:field %} is-invalid {% endif %}" type="number" min="1" name="{{ field }}" value="{{ x.amount|default:"" }}" required="required" />
<div id="{{ field }}-error" class="invalid-feedback">{{ errors|dict:field|default:"" }}</div>
</div>
</div>
</div>
@ -180,11 +186,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 errors.balance %} is-invalid {% endif %} balance-row">
<div id="credit-total-row" class="d-flex justify-content-between align-items-center form-control {% if errors|dict:"balance" %} is-invalid {% endif %} balance-row">
{% trans "Total" context "Accounting|" as text %}{{ text|force_escape }}
<span id="credit-total" class="amount">{{ item.credit_total }}</span>
</div>
<div id="credit-total-error" class="invalid-feedback balance-error">{{ errors.balance }}</div>
<div id="credit-total-error" class="invalid-feedback balance-error">{{ errors|dict:"balance"|default:"" }}</div>
</li>
</ul>
</div>
@ -195,8 +201,8 @@ First written: 2020/7/23
<label for="txn-note">{% trans "Notes:" context "Accounting|" as text %}{{ text|force_escape }}</label>
</div>
<div class="col-sm-10">
<textarea id="txn-note" class="form-control {% if errors.notes %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors.notes|default:"" }}</div>
<textarea id="txn-note" class="form-control {% if errors|dict:"notes" %} is-invalid {% endif %}" name="note">{{ item.notes|default:"" }}</textarea>
<div id="txn-note-error" class="invalid-feedback">{{ errors|dict:"notes"|default:"" }}</div>
</div>
</div>

View File

@ -134,6 +134,7 @@ def retrieve_status(context):
if "success" in status:
context.dicts[0]["success"] = status["success"]
if "errors_by_field" in status:
context.dicts[0]["errors"] = status["errors_by_field"]
if "" in status["errors_by_field"]:
if "page_errors" not in context.dicts[0]:
context.dicts[0]["page_errors"] = []
@ -202,3 +203,23 @@ def index(value, arg):
if arg >= len(value):
return None
return value[arg]
@register.filter(name="dict")
def dict_value(value, arg):
"""Returns an element in a dictionary.
Args:
value (dict): The dictionary.
arg (str): The key.
Returns:
any: The element in this dictionary.
"""
if not isinstance(value, dict):
return None
if not isinstance(arg, str):
return None
if arg not in value:
return None
return value[arg]