From 9ab325a1c7502136f5c26de7ba49d31f73b7257c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Tue, 28 Jul 2020 22:11:54 +0800 Subject: [PATCH] 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. --- .../accounting/transactions/expense/form.html | 23 ++++++---- .../accounting/transactions/income/form.html | 23 ++++++---- .../transactions/transfer/form.html | 46 +++++++++++-------- mia_core/templatetags/mia_core.py | 21 +++++++++ 4 files changed, 73 insertions(+), 40 deletions(-) diff --git a/accounting/templates/accounting/transactions/expense/form.html b/accounting/templates/accounting/transactions/expense/form.html index 1868067..15e973f 100644 --- a/accounting/templates/accounting/transactions/expense/form.html +++ b/accounting/templates/accounting/transactions/expense/form.html @@ -52,8 +52,8 @@ First written: 2020/7/23
- -
{{ errors.date|default:"" }}
+ +
{{ errors|dict:"date"|default:"" }}
@@ -68,7 +68,8 @@ First written: 2020/7/23 {% endif %} - {% if x.account is not None %} {% else %} @@ -76,17 +77,19 @@ First written: 2020/7/23 {% endif %} -
{{ errors.debit.account|index:forloop.counter }}
+
{{ errors|dict:field|default:"" }}
- -
{{ errors.debit.summary|index:forloop.counter }}
+ {% str_format "debit-{}-summary" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
- -
{{ errors.debit.amount|index:forloop.counter }}
+ {% str_format "debit-{}-amount" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
@@ -133,8 +136,8 @@ First written: 2020/7/23
- -
{{ errors.notes|default:"" }}
+ +
{{ errors|dict:"notes"|default:"" }}
diff --git a/accounting/templates/accounting/transactions/income/form.html b/accounting/templates/accounting/transactions/income/form.html index 21bc487..26d778e 100644 --- a/accounting/templates/accounting/transactions/income/form.html +++ b/accounting/templates/accounting/transactions/income/form.html @@ -52,8 +52,8 @@ First written: 2020/7/23
- -
{{ errors.date|default:"" }}
+ +
{{ errors|dict:"date"|default:"" }}
@@ -68,7 +68,8 @@ First written: 2020/7/23 {% endif %} - {% if x.account is not None %} {% else %} @@ -76,17 +77,19 @@ First written: 2020/7/23 {% endif %} -
{{ errors.credit.account|index:forloop.counter }}
+
{{ errors|dict:field|default:"" }}
- -
{{ errors.credit.summary|index:forloop.counter }}
+ {% str_format "credit-{}-summary" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
- -
{{ errors.credit.amount|index:forloop.counter }}
+ {% str_format "credit-{}-amount" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
@@ -133,8 +136,8 @@ First written: 2020/7/23
- -
{{ errors.notes|default:"" }}
+ +
{{ errors|dict:"notes"|default:"" }}
diff --git a/accounting/templates/accounting/transactions/transfer/form.html b/accounting/templates/accounting/transactions/transfer/form.html index 33b3421..209e9cf 100644 --- a/accounting/templates/accounting/transactions/transfer/form.html +++ b/accounting/templates/accounting/transactions/transfer/form.html @@ -55,8 +55,8 @@ First written: 2020/7/23
- -
{{ errors.date|default:"" }}
+ +
{{ errors|dict:"date"|default:"" }}
@@ -74,7 +74,8 @@ First written: 2020/7/23 {% endif %} - {% if x.account is not None %} {% else %} @@ -82,17 +83,19 @@ First written: 2020/7/23 {% endif %} -
{{ errors.debit.account|index:forloop.counter }}
+
{{ errors|dict:field|default:"" }}
- -
{{ errors.debit.summary|index:forloop.counter }}
+ {% str_format "debit-{}-summary" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
- -
{{ errors.debit.amount|index:forloop.counter }}
+ {% str_format "debit-{}-amount" forloop.counter as field %} + +
{{ errors|dict:field|default:"" }}
@@ -116,11 +119,11 @@ First written: 2020/7/23
  • -
    +
    {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} {{ item.debit_total }}
    -
    {{ errors.balance }}
    +
    {{ errors|dict:"balance"|default:"" }}
  • @@ -138,7 +141,8 @@ First written: 2020/7/23 {% endif %} - {% if x.account is not None %} {% else %} @@ -146,17 +150,19 @@ First written: 2020/7/23 {% endif %} -
    {{ errors.credit.account|index:forloop.counter }}
    +
    {{ errors|dict:field|default:"" }}
    - -
    {{ errors.credit.summary|index:forloop.counter }}
    + {% str_format "credit-{}-summary" forloop.counter as field %} + +
    {{ errors|dict:field|default:"" }}
    - -
    {{ errors.credit.amount|index:forloop.counter }}
    + {% str_format "credit-{}-amount" forloop.counter as field %} + +
    {{ errors|dict:field|default:"" }}
    @@ -180,11 +186,11 @@ First written: 2020/7/23
  • -
    +
    {% trans "Total" context "Accounting|" as text %}{{ text|force_escape }} {{ item.credit_total }}
    -
    {{ errors.balance }}
    +
    {{ errors|dict:"balance"|default:"" }}
  • @@ -195,8 +201,8 @@ First written: 2020/7/23
    - -
    {{ errors.notes|default:"" }}
    + +
    {{ errors|dict:"notes"|default:"" }}
    diff --git a/mia_core/templatetags/mia_core.py b/mia_core/templatetags/mia_core.py index 2db2350..4c51cd7 100644 --- a/mia_core/templatetags/mia_core.py +++ b/mia_core/templatetags/mia_core.py @@ -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]