From a58f6d6d11192341cabecb0850a189af5bc02d4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 16 Jul 2020 23:03:37 +0800 Subject: [PATCH] Moved the core custom template filters and tags to the template tag library in the Mia core application. --- accounting/models.py | 2 +- accounting/templates/accounting/cash.html | 1 + .../templates/accounting/cash_summary.html | 1 + accounting/templates/accounting/ledger.html | 1 + mia_core/template_filters.py | 71 ------------------- .../mia_core/include/period-chooser.html | 1 + mia_core/templatetags/__init__.py | 0 .../mia_core.py} | 50 ++++++++++++- 8 files changed, 53 insertions(+), 74 deletions(-) delete mode 100644 mia_core/template_filters.py create mode 100644 mia_core/templatetags/__init__.py rename mia_core/{template_tags.py => templatetags/mia_core.py} (61%) diff --git a/accounting/models.py b/accounting/models.py index ecf8334..c9e3ba9 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -22,7 +22,7 @@ from django.conf import settings from django.db import models from django.urls import reverse -from mia_core.template_filters import smart_month +from mia_core.templatetags.mia_core import smart_month from mia_core.utils import get_multi_language_attr diff --git a/accounting/templates/accounting/cash.html b/accounting/templates/accounting/cash.html index 0485bbd..4872370 100644 --- a/accounting/templates/accounting/cash.html +++ b/accounting/templates/accounting/cash.html @@ -22,6 +22,7 @@ First written: 2020/7/1 {% endcomment %} {% load i18n %} {% load humanize %} +{% load mia_core %} {% load accounting %} {% block settings %} diff --git a/accounting/templates/accounting/cash_summary.html b/accounting/templates/accounting/cash_summary.html index 50f690a..3b86d39 100644 --- a/accounting/templates/accounting/cash_summary.html +++ b/accounting/templates/accounting/cash_summary.html @@ -22,6 +22,7 @@ First written: 2020/7/15 {% endcomment %} {% load i18n %} {% load humanize %} +{% load mia_core %} {% load accounting %} {% block settings %} diff --git a/accounting/templates/accounting/ledger.html b/accounting/templates/accounting/ledger.html index 0ee31a2..9e9e9af 100644 --- a/accounting/templates/accounting/ledger.html +++ b/accounting/templates/accounting/ledger.html @@ -22,6 +22,7 @@ First written: 2020/7/16 {% endcomment %} {% load i18n %} {% load humanize %} +{% load mia_core %} {% load accounting %} {% block settings %} diff --git a/mia_core/template_filters.py b/mia_core/template_filters.py deleted file mode 100644 index 32be91d..0000000 --- a/mia_core/template_filters.py +++ /dev/null @@ -1,71 +0,0 @@ -# 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. - -"""The template filters of the Mia core application. - -""" - -from datetime import date - -from django import template -from django.template import defaultfilters -from django.utils import timezone -from django.utils.translation import gettext - -register = template.Library() - - -@register.filter -def smart_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("星期", "") - - -@register.filter -def smart_month(value): - """Formats the month for human friendliness. - - Args: - value (datetime.date): The month. - - Returns: - str: The human-friendly format of the month. - """ - today = timezone.localdate() - if value.year == today.year and value.month == today.month: - return gettext("This Month") - month = today.month - 1 - year = today.year - if month < 1: - month = 12 - year = year - 1 - if value.year == year and value.month == month: - return gettext("Last Month") - return defaultfilters.date(value, "Y/n") diff --git a/mia_core/templates/mia_core/include/period-chooser.html b/mia_core/templates/mia_core/include/period-chooser.html index 71c6e82..9a2b0e1 100644 --- a/mia_core/templates/mia_core/include/period-chooser.html +++ b/mia_core/templates/mia_core/include/period-chooser.html @@ -20,6 +20,7 @@ Author: imacat@mail.imacat.idv.tw (imacat) First written: 2020/7/10 {% endcomment %} {% load i18n %} +{% load mia_core %} diff --git a/mia_core/templatetags/__init__.py b/mia_core/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/mia_core/template_tags.py b/mia_core/templatetags/mia_core.py similarity index 61% rename from mia_core/template_tags.py rename to mia_core/templatetags/mia_core.py index c3ba2e4..774378c 100644 --- a/mia_core/template_tags.py +++ b/mia_core/templatetags/mia_core.py @@ -1,4 +1,4 @@ -# The template tags of the Mia project. +# The core application of the Mia project. # by imacat , 2020/7/1 # Copyright (c) 2020 imacat. @@ -15,12 +15,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -"""The template tags of the Mia core application. +"""The template tags and filters of the Mia core application. """ +from datetime import date from django import template +from django.template import defaultfilters from django.urls import reverse +from django.utils import timezone +from django.utils.translation import gettext from mia_core.utils import UrlBuilder @@ -83,3 +87,45 @@ def url_period(context, period_spec): request.resolver_match.url_name) subject_code = request.resolver_match.kwargs["subject_code"] return reverse(viewname, args=[subject_code, period_spec]) + + +@register.filter +def smart_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("星期", "") + + +@register.filter +def smart_month(value): + """Formats the month for human friendliness. + + Args: + value (datetime.date): The month. + + Returns: + str: The human-friendly format of the month. + """ + today = timezone.localdate() + if value.year == today.year and value.month == today.month: + return gettext("This Month") + month = today.month - 1 + year = today.year + if month < 1: + month = 12 + year = year - 1 + if value.year == year and value.month == month: + return gettext("Last Month") + return defaultfilters.date(value, "Y/n")