Revised the period parser.
This commit is contained in:
parent
6f604f1c86
commit
76df32a76d
@ -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,35 +53,39 @@ class PeriodParser:
|
||||
# A specific month
|
||||
m = re.match("^([0-9]{4})-([0-9]{2})$", period_spec)
|
||||
if m is not None:
|
||||
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)
|
||||
try:
|
||||
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:
|
||||
try:
|
||||
year = int(m.group(1))
|
||||
month = int(m.group(2))
|
||||
try:
|
||||
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:
|
||||
try:
|
||||
year = int(m.group(1))
|
||||
try:
|
||||
self.start = date(year, 1, 1)
|
||||
except ValueError:
|
||||
self.invalid_period()
|
||||
return
|
||||
self.end = date(year, 12, 31)
|
||||
today = localdate()
|
||||
if year == today.year:
|
||||
@ -90,8 +94,6 @@ class PeriodParser:
|
||||
self.description = gettext("Last Year")
|
||||
else:
|
||||
self.description = str(year)
|
||||
except ValueError:
|
||||
self.invalid_period()
|
||||
return
|
||||
# All time
|
||||
if period_spec == "-":
|
||||
@ -106,21 +108,12 @@ 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})"
|
||||
"-([0-9]{4})-([0-9]{2})-([0-9]{2})$"),
|
||||
@ -131,11 +124,16 @@ 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)))
|
||||
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" % (
|
||||
@ -145,7 +143,8 @@ class PeriodParser:
|
||||
self.description = "%s-%s" % (
|
||||
defaultfilters.date(self.start, "n/j"),
|
||||
defaultfilters.date(self.end, "n/j"))
|
||||
else:
|
||||
# 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"),
|
||||
@ -154,8 +153,11 @@ class PeriodParser:
|
||||
self.description = "%s-%s" % (
|
||||
defaultfilters.date(self.start, "n/j"),
|
||||
defaultfilters.date(self.end, "j"))
|
||||
except ValueError:
|
||||
self.invalid_period()
|
||||
# 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:
|
||||
@ -224,3 +227,23 @@ class PeriodParser:
|
||||
if year == prev.year and month == prev.month:
|
||||
return gettext("Last 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")
|
||||
|
Loading…
Reference in New Issue
Block a user