From a3c0f3fb823bebb45f1ce1b339b48db95a86ba76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 2 Jul 2020 02:04:35 +0800 Subject: [PATCH] Added the template helper to display the date in a human-friendly format. --- accounting/templates/accounting/cash.html | 14 ++++---- mia_core/templatefilters.py | 43 +++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 mia_core/templatefilters.py diff --git a/accounting/templates/accounting/cash.html b/accounting/templates/accounting/cash.html index 1f2c9b3..ad8133b 100644 --- a/accounting/templates/accounting/cash.html +++ b/accounting/templates/accounting/cash.html @@ -21,6 +21,7 @@ Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/7/1 {% endcomment %} {% load i18n %} +{% load humanize %} {% block settings %} {% trans "Cash Report" context "Accounting|" as title %} @@ -29,11 +30,8 @@ First written: 2020/7/1 {% block content %} -

{{ request.path }}

- -

{{ request.get_full_path }}

-

{{ request.resolver_match.url_name }}

+

{{ request.resolver_match.app_name }}

{% if records %} @@ -51,12 +49,12 @@ First written: 2020/7/1 {% for record in records %} - + - - - + + + {% endfor %} diff --git a/mia_core/templatefilters.py b/mia_core/templatefilters.py new file mode 100644 index 0000000..cd275be --- /dev/null +++ b/mia_core/templatefilters.py @@ -0,0 +1,43 @@ +# The template filters of the Mia project. +# by imacat , 2020/7/2 + +# Copyright (c) 2020 imacat. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import locale +from datetime import date + +from django import template +from django.template import defaultfilters +from django.utils.translation import gettext + +register = template.Library() + + +@register.filter(is_safe=True) +def human_date(value): + """Formats the date for human friendliness. + + Args: + value (datetime.date): The date. + + Returns: + str: The human-friendly format of the date. + """ + if value == date.today(): + return gettext("Today") + if (date.today() - value).days == 1: + return gettext("Yesterday") + if date.today().year == value.year: + return defaultfilters.date(value, "n/j(D)").replace("星期", "") + return defaultfilters.date(value, "Y/n/j(D)").replace("星期", "")
{{ record.transaction.date|date:"Y-m-d" }}{{ record.transaction.date|human_date }} {{ record.subject.title_zhtw }} {{ record.summary }}{{ record.debit_amount|default:"" }}{{ record.credit_amount|default:"" }}{{ record.amount }}{{ record.debit_amount|intcomma:False }}{{ record.credit_amount|default:""|intcomma:False }}{{ record.amount|intcomma:False }} {% trans "View" context "Accounting|" %}