Revised the import of django.utils.timezone.localdate.

This commit is contained in:
依瑪貓 2020-07-16 22:22:35 +08:00
parent 27f762a1c0
commit 5b9ac93ee5
3 changed files with 23 additions and 23 deletions

View File

@ -25,8 +25,7 @@ from django.db import connection
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse from django.urls import reverse
from django.utils import dateformat from django.utils import dateformat, timezone
from django.utils.timezone import localdate
from django.utils.translation import pgettext from django.utils.translation import pgettext
from django.views.decorators.http import require_GET from django.views.decorators.http import require_GET
@ -61,7 +60,7 @@ def cash_home(request):
and month. and month.
""" """
subject_code = settings.ACCOUNTING["DEFAULT_CASH_SUBJECT"] subject_code = settings.ACCOUNTING["DEFAULT_CASH_SUBJECT"]
period_spec = dateformat.format(localdate(), "Y-m") period_spec = dateformat.format(timezone.localdate(), "Y-m")
return HttpResponseRedirect( return HttpResponseRedirect(
reverse("accounting:cash", args=(subject_code, period_spec))) reverse("accounting:cash", args=(subject_code, period_spec)))

View File

@ -24,8 +24,7 @@ from datetime import date, timedelta
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.template import defaultfilters from django.template import defaultfilters
from django.utils import dateformat from django.utils import dateformat, timezone
from django.utils.timezone import localdate
from django.utils.translation import gettext from django.utils.translation import gettext
from mia_core.utils import Language from mia_core.utils import Language
@ -111,7 +110,7 @@ class Period:
Returns: Returns:
date: The first day of the last month. date: The first day of the last month.
""" """
today = localdate() today = timezone.localdate()
month = today.month - 1 month = today.month - 1
year = today.year year = today.year
if month < 1: if month < 1:
@ -126,7 +125,7 @@ class Period:
Returns: Returns:
date: The first day of the next month. date: The first day of the next month.
""" """
today = localdate() today = timezone.localdate()
month = today.month + 1 month = today.month + 1
year = today.year year = today.year
if month > 12: if month > 12:
@ -138,7 +137,7 @@ class Period:
def this_month(self): def this_month(self):
if self._data_start is None: if self._data_start is None:
return None return None
today = localdate() today = timezone.localdate()
first_month_start = date( first_month_start = date(
self._data_start.year, self._data_start.month, 1) self._data_start.year, self._data_start.month, 1)
if today < first_month_start: if today < first_month_start:
@ -190,7 +189,7 @@ class Period:
def this_year(self): def this_year(self):
if self._data_start is None: if self._data_start is None:
return None return None
this_year = localdate().year this_year = timezone.localdate().year
if this_year < self._data_start.year: if this_year < self._data_start.year:
return None return None
return str(this_year) return str(this_year)
@ -199,7 +198,7 @@ class Period:
def last_year(self): def last_year(self):
if self._data_start is None: if self._data_start is None:
return None return None
last_year = localdate().year - 1 last_year = timezone.localdate().year - 1
if last_year < self._data_start.year: if last_year < self._data_start.year:
return None return None
return str(last_year) return str(last_year)
@ -208,7 +207,7 @@ class Period:
def has_years_to_choose(self): def has_years_to_choose(self):
if self._data_start is None: if self._data_start is None:
return None return None
this_year = localdate().year this_year = timezone.localdate().year
if self._data_start.year < this_year - 1: if self._data_start.year < this_year - 1:
return True return True
if self._data_end.year > this_year: if self._data_end.year > this_year:
@ -219,7 +218,7 @@ class Period:
def years_to_choose(self): def years_to_choose(self):
if self._data_start is None: if self._data_start is None:
return None return None
this_year = localdate().year this_year = timezone.localdate().year
before = [str(x) for x in range( before = [str(x) for x in range(
self._data_start.year, this_year - 1)] self._data_start.year, this_year - 1)]
after = [str(x) for x in range( after = [str(x) for x in range(
@ -244,7 +243,7 @@ class Period:
def today(self): def today(self):
if self._data_start is None: if self._data_start is None:
return None return None
today = localdate() today = timezone.localdate()
if today < self._data_start or today > self._data_end: if today < self._data_start or today > self._data_end:
return None return None
return dateformat.format(today, "Y-m-d") return dateformat.format(today, "Y-m-d")
@ -253,7 +252,7 @@ class Period:
def yesterday(self): def yesterday(self):
if self._data_start is None: if self._data_start is None:
return None return None
yesterday = localdate() - timedelta(days=1) yesterday = timezone.localdate() - timedelta(days=1)
if yesterday < self._data_start or yesterday > self._data_end: if yesterday < self._data_start or yesterday > self._data_end:
return None return None
return dateformat.format(yesterday, "Y-m-d") return dateformat.format(yesterday, "Y-m-d")
@ -360,7 +359,8 @@ class Period:
except ValueError: except ValueError:
self.invalid_period() self.invalid_period()
return return
self.end = self.get_month_last_day(localdate()) self.end = self.get_month_last_day(
timezone.localdate())
self.description = gettext( self.description = gettext(
"Since %s") % self.get_month_text(year, month) "Since %s") % self.get_month_text(year, month)
return return
@ -374,7 +374,7 @@ class Period:
self.invalid_period() self.invalid_period()
return return
self.end = date(year, 12, 31) self.end = date(year, 12, 31)
today = localdate() today = timezone.localdate()
if year == today.year: if year == today.year:
self.description = gettext("This Year") self.description = gettext("This Year")
elif year == today.year - 1: elif year == today.year - 1:
@ -385,7 +385,8 @@ class Period:
# All time # All time
if spec == "-": if spec == "-":
self.start = date(2000, 1, 1) self.start = date(2000, 1, 1)
self.end = self.get_month_last_day(localdate()) self.end = self.get_month_last_day(
timezone.localdate())
self.description = gettext("All") self.description = gettext("All")
return return
# A specific date # A specific date
@ -420,7 +421,7 @@ class Period:
except ValueError: except ValueError:
self.invalid_period() self.invalid_period()
return return
today = localdate() today = timezone.localdate()
# Spans several years # Spans several years
if self.start.year != self.end.year: if self.start.year != self.end.year:
self.description = "%s-%s" % ( self.description = "%s-%s" % (
@ -456,7 +457,7 @@ class Period:
def set_this_month(self): def set_this_month(self):
"""Sets the period to this month.""" """Sets the period to this month."""
today = localdate() today = timezone.localdate()
self.spec = dateformat.format(today, "Y-m") self.spec = dateformat.format(today, "Y-m")
self.start = date(today.year, today.month, 1) self.start = date(today.year, today.month, 1)
self.end = self.get_month_last_day(self.start) self.end = self.get_month_last_day(self.start)
@ -497,7 +498,7 @@ class Period:
Returns: Returns:
str: The description of the month. str: The description of the month.
""" """
today = localdate() today = timezone.localdate()
if year == today.year and month == today.month: if year == today.year and month == today.month:
return gettext("This Month") return gettext("This Month")
prev_month = today.month - 1 prev_month = today.month - 1
@ -520,7 +521,7 @@ class Period:
Returns: Returns:
str: The description of the day. str: The description of the day.
""" """
today = localdate() today = timezone.localdate()
if day == today: if day == today:
return gettext("Today") return gettext("Today")
elif day == today - timedelta(days=1): elif day == today - timedelta(days=1):

View File

@ -23,7 +23,7 @@ from datetime import date
from django import template from django import template
from django.template import defaultfilters from django.template import defaultfilters
from django.utils.timezone import localdate from django.utils import timezone
from django.utils.translation import gettext from django.utils.translation import gettext
register = template.Library() register = template.Library()
@ -58,7 +58,7 @@ def smart_month(value):
Returns: Returns:
str: The human-friendly format of the month. str: The human-friendly format of the month.
""" """
today = localdate() today = timezone.localdate()
if value.year == today.year and value.month == today.month: if value.year == today.year and value.month == today.month:
return gettext("This Month") return gettext("This Month")
month = today.month - 1 month = today.month - 1