Revised the period parser.

This commit is contained in:
依瑪貓 2020-07-08 07:40:07 +08:00
parent 6f604f1c86
commit 76df32a76d

View File

@ -35,7 +35,7 @@ class PeriodParser:
period_spec (str): The period specification.
Attributes:
spec (str): The period specification.
spec (str): The currently-using period specification.
start (date): The start of the period.
end (date): The end of the period.
description (str): The text description of the period.
@ -53,45 +53,47 @@ class PeriodParser:
# A specific month
m = re.match("^([0-9]{4})-([0-9]{2})$", period_spec)
if m is not None:
year = int(m.group(1))
month = int(m.group(2))
try:
year = int(m.group(1))
month = int(m.group(2))
self.set_month_range(year, month)
self.description = self.get_month_description(
year, month)
self.start = date(year, month, 1)
except ValueError:
self.invalid_period()
return
self.end = self.get_month_last_day(self.start)
self.description = self.get_month_text(year, month)
return
# From a specific month
m = re.match("^([0-9]{4})-([0-9]{2})-$", period_spec)
if m is not None:
year = int(m.group(1))
month = int(m.group(2))
try:
year = int(m.group(1))
month = int(m.group(2))
self.start = date(year, month, 1)
self.end = self.get_month_last_day(localdate())
self.description = gettext(
"Since %s") % self.get_month_description(
year, month)
except ValueError:
self.invalid_period()
return
self.end = self.get_month_last_day(localdate())
self.description = gettext(
"Since %s") % self.get_month_text(year, month)
return
# A specific year
m = re.match("^([0-9]{4})$", period_spec)
if m is not None:
year = int(m.group(1))
try:
year = int(m.group(1))
self.start = date(year, 1, 1)
self.end = date(year, 12, 31)
today = localdate()
if year == today.year:
self.description = gettext("This Year")
elif year == today.year - 1:
self.description = gettext("Last Year")
else:
self.description = str(year)
except ValueError:
self.invalid_period()
return
self.end = date(year, 12, 31)
today = localdate()
if year == today.year:
self.description = gettext("This Year")
elif year == today.year - 1:
self.description = gettext("Last Year")
else:
self.description = str(year)
return
# All time
if period_spec == "-":
@ -106,20 +108,11 @@ class PeriodParser:
try:
self.start = date(
int(m.group(1)), int(m.group(2)), int(m.group(3)))
self.end = self.start
today = localdate()
if self.start == today:
self.description = gettext("Today")
elif self.start == today - timedelta(days=1):
self.description = gettext("Yesterday")
elif self.start.year != today.year:
self.description = defaultfilters.date(
self.start, "Y/n/j")
else:
self.description = defaultfilters.date(
self.start, "n/j")
except ValueError:
self.invalid_period()
return
self.end = self.start
self.description = self.get_date_text(self.start)
return
# A specific date period
m = re.match(("^([0-9]{4})-([0-9]{2})-([0-9]{2})"
@ -131,31 +124,40 @@ class PeriodParser:
int(m.group(1)), int(m.group(2)), int(m.group(3)))
self.end = date(
int(m.group(4)), int(m.group(5)), int(m.group(6)))
today = localdate()
if self.start.year != self.end.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "Y/n/j"))
elif self.start.month != self.end.month:
if self.start.year != today.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "n/j"))
else:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "n/j"),
defaultfilters.date(self.end, "n/j"))
else:
if self.start.year != today.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "j"))
else:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "n/j"),
defaultfilters.date(self.end, "j"))
except ValueError:
self.invalid_period()
return
today = localdate()
# Spans several years
if self.start.year != self.end.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "Y/n/j"))
# Spans several months
elif self.start.month != self.end.month:
if self.start.year != today.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "n/j"))
else:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "n/j"),
defaultfilters.date(self.end, "n/j"))
# Spans several days
elif self.start.day != self.end.day:
if self.start.year != today.year:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "Y/n/j"),
defaultfilters.date(self.end, "j"))
else:
self.description = "%s-%s" % (
defaultfilters.date(self.start, "n/j"),
defaultfilters.date(self.end, "j"))
# At the same day
else:
self.spec = dateformat.format(self.start, "Y-m-d")
self.description = self.get_date_text(self.start)
return
# Wrong period format
self.invalid_period()
@ -165,7 +167,8 @@ class PeriodParser:
self.error = pgettext("Accounting|", "Invalid period.")
today = localdate()
self.spec = dateformat.format(localdate(), "Y-m")
self.set_month_range(today.year, today.month)
self.start = date(today.year, today.month, 1)
self.end = self.get_month_last_day(self.start)
self.description = gettext("This Month")
def set_month_range(self, year, month):
@ -202,7 +205,7 @@ class PeriodParser:
return date(next_year, next_month, 1) - timedelta(days=1)
@staticmethod
def get_month_description(year, month):
def get_month_text(year, month):
"""Returns the text description of a month.
Args:
@ -210,7 +213,7 @@ class PeriodParser:
month (int): The month.
Returns:
str: The description of the month
str: The description of the month.
"""
today = localdate()
if year == today.year and month == today.month:
@ -223,4 +226,24 @@ class PeriodParser:
prev = date(prev_year, prev_month, 1)
if year == prev.year and month == prev.month:
return gettext("Last Month")
return "%d/%d" % (year, month)
return "%d/%d" % (year, month)
@staticmethod
def get_date_text(day):
"""Returns the text description of a day.
Args:
day (date): The date.
Returns:
str: The description of the day.
"""
today = localdate()
if day == today:
return gettext("Today")
elif day == today - timedelta(days=1):
return gettext("Yesterday")
elif day.year != today.year:
return defaultfilters.date(day, "Y/n/j")
else:
return defaultfilters.date(day, "n/j")