Moved the core custom template filters and tags to the template tag library in the Mia core application.

This commit is contained in:
依瑪貓 2020-07-16 23:03:37 +08:00
parent 42e444b8c8
commit a58f6d6d11
8 changed files with 53 additions and 74 deletions

View File

@ -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

View File

@ -22,6 +22,7 @@ First written: 2020/7/1
{% endcomment %}
{% load i18n %}
{% load humanize %}
{% load mia_core %}
{% load accounting %}
{% block settings %}

View File

@ -22,6 +22,7 @@ First written: 2020/7/15
{% endcomment %}
{% load i18n %}
{% load humanize %}
{% load mia_core %}
{% load accounting %}
{% block settings %}

View File

@ -22,6 +22,7 @@ First written: 2020/7/16
{% endcomment %}
{% load i18n %}
{% load humanize %}
{% load mia_core %}
{% load accounting %}
{% block settings %}

View File

@ -1,71 +0,0 @@
# The template filters of the Mia project.
# by imacat <imacat@mail.imacat.idv.tw>, 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")

View File

@ -20,6 +20,7 @@ Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2020/7/10
{% endcomment %}
{% load i18n %}
{% load mia_core %}
<!-- the period chooser dialog -->
<!-- The Modal -->

View File

View File

@ -1,4 +1,4 @@
# The template tags of the Mia project.
# The core application of the Mia project.
# by imacat <imacat@mail.imacat.idv.tw>, 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")