From 841fbc12236ecec687a174ddaf881578b0556c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Mon, 3 Aug 2020 00:20:26 +0800 Subject: [PATCH] Added the add_css and add_js template tags to add local CSS and JavaScript files to the base template in the Mia core application, and applied them in the templates of the accounting application. --- .../templates/accounting/balance-sheet.html | 3 +- .../templates/accounting/cash-summary.html | 3 +- accounting/templates/accounting/cash.html | 3 +- .../accounting/income-statement.html | 3 +- accounting/templates/accounting/journal.html | 3 +- .../templates/accounting/ledger-summary.html | 3 +- accounting/templates/accounting/ledger.html | 3 +- accounting/templates/accounting/search.html | 3 +- .../templates/accounting/trial-balance.html | 3 +- mia_core/templatetags/mia_core.py | 36 +++++++++++++++++++ 10 files changed, 45 insertions(+), 18 deletions(-) diff --git a/accounting/templates/accounting/balance-sheet.html b/accounting/templates/accounting/balance-sheet.html index 923c98e..1afe54a 100644 --- a/accounting/templates/accounting/balance-sheet.html +++ b/accounting/templates/accounting/balance-sheet.html @@ -30,8 +30,7 @@ First written: 2020/7/20 {% blocktrans asvar title with period=period.description context "Accounting|" %}Balance Sheet in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/cash-summary.html b/accounting/templates/accounting/cash-summary.html index 4b53547..76ae236 100644 --- a/accounting/templates/accounting/cash-summary.html +++ b/accounting/templates/accounting/cash-summary.html @@ -29,8 +29,7 @@ First written: 2020/7/15 {% block settings %} {% blocktrans asvar title with account=account.title|title context "Accounting|" %}Cash Summary for {{ account }}{% endblocktrans %} {% setvar "title" title %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/cash.html b/accounting/templates/accounting/cash.html index ddc1d95..b96af18 100644 --- a/accounting/templates/accounting/cash.html +++ b/accounting/templates/accounting/cash.html @@ -30,8 +30,7 @@ First written: 2020/7/1 {% blocktrans asvar title with account=account.title|title period=period.description context "Accounting|" %}Cash Account for {{ account }} in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/income-statement.html b/accounting/templates/accounting/income-statement.html index ac7233d..d2deaca 100644 --- a/accounting/templates/accounting/income-statement.html +++ b/accounting/templates/accounting/income-statement.html @@ -30,8 +30,7 @@ First written: 2020/7/19 {% blocktrans asvar title with period=period.description context "Accounting|" %}Income Statement in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/journal.html b/accounting/templates/accounting/journal.html index d5a166e..6f10523 100644 --- a/accounting/templates/accounting/journal.html +++ b/accounting/templates/accounting/journal.html @@ -30,8 +30,7 @@ First written: 2020/7/17 {% blocktrans asvar title with period=period.description context "Accounting|" %}Journal in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/ledger-summary.html b/accounting/templates/accounting/ledger-summary.html index a5221cd..a234b7f 100644 --- a/accounting/templates/accounting/ledger-summary.html +++ b/accounting/templates/accounting/ledger-summary.html @@ -29,8 +29,7 @@ First written: 2020/7/16 {% block settings %} {% blocktrans asvar title with account=account.title|title context "Accounting|" %}Ledger Summary for {{ account }}{% endblocktrans %} {% setvar "title" title %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/ledger.html b/accounting/templates/accounting/ledger.html index 8b35a3c..202751b 100644 --- a/accounting/templates/accounting/ledger.html +++ b/accounting/templates/accounting/ledger.html @@ -30,8 +30,7 @@ First written: 2020/7/16 {% blocktrans asvar title with account=account.title|title period=period.description context "Accounting|" %}Ledger for {{ account }} in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/search.html b/accounting/templates/accounting/search.html index ca1dc11..a73455a 100644 --- a/accounting/templates/accounting/search.html +++ b/accounting/templates/accounting/search.html @@ -30,8 +30,7 @@ First written: 2020/7/21 {% blocktrans asvar title with query=request.GET.q context "Accounting|" %}Search Result for “{{ query }}”{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/accounting/templates/accounting/trial-balance.html b/accounting/templates/accounting/trial-balance.html index 555561a..60cec4c 100644 --- a/accounting/templates/accounting/trial-balance.html +++ b/accounting/templates/accounting/trial-balance.html @@ -30,8 +30,7 @@ First written: 2020/7/19 {% blocktrans asvar title with period=period.description context "Accounting|" %}Trial Balance in {{ period }}{% endblocktrans %} {% setvar "title" title %} {% setvar "use_period_chooser" True %} - {% static "accounting/css/report.css" as css %} - {% setvar "css" css %} + {% static "accounting/css/report.css" as file %}{% add_css file %} {% endblock %} {% block content %} diff --git a/mia_core/templatetags/mia_core.py b/mia_core/templatetags/mia_core.py index ffba994..c5a126a 100644 --- a/mia_core/templatetags/mia_core.py +++ b/mia_core/templatetags/mia_core.py @@ -114,6 +114,42 @@ def url_keep_return(context, view_name, *args): return str(UrlBuilder(url).set("r", context.request.GET.get("r"))) +@register.simple_tag(takes_context=True) +def add_css(context, url): + """Adds a local CSS file. The file is added to the "css" template + list variable. + + Args: + context (RequestContext): The request context. + url (str): The URL or path of the CSS file. + + Returns: + str: An empty string + """ + if "css" not in context.dicts[0]: + context.dicts[0]["css"] = [] + context.dicts[0]["css"].append(url) + return "" + + +@register.simple_tag(takes_context=True) +def add_js(context, url): + """Adds a local JavaScript file. The file is added to the "js" template + list variable. + + Args: + context (RequestContext): The request context. + url (str): The URL or path of the JavaScript file. + + Returns: + str: An empty string + """ + if "js" not in context.dicts[0]: + context.dicts[0]["js"] = [] + context.dicts[0]["js"].append(url) + return "" + + @register.simple_tag(takes_context=True, name="retrieve_status") def retrieve_status_tag(context): """Returns the success message from the previously-stored status. The