Replaced the invalid_period() method in Period.Parser with ValueError in the Mia core application.
This commit is contained in:
parent
95cd08a887
commit
95e0173126
@ -52,15 +52,16 @@ class PeriodConverter:
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Period: The period.
|
Period: The period.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: When the period specification is invalid.
|
||||||
"""
|
"""
|
||||||
first_txn = Transaction.objects.order_by("date").first()
|
first_txn = Transaction.objects.order_by("date").first()
|
||||||
data_start = first_txn.date if first_txn is not None else None
|
data_start = first_txn.date if first_txn is not None else None
|
||||||
last_txn = Transaction.objects.order_by("-date").first()
|
last_txn = Transaction.objects.order_by("-date").first()
|
||||||
data_end = last_txn.date if last_txn is not None else None
|
data_end = last_txn.date if last_txn is not None else None
|
||||||
period = Period(value, data_start, data_end)
|
# Raises ValueError
|
||||||
if period.error is not None:
|
return Period(value, data_start, data_end)
|
||||||
raise ValueError
|
|
||||||
return period
|
|
||||||
|
|
||||||
def to_url(self, value):
|
def to_url(self, value):
|
||||||
"""Returns the specification of a period.
|
"""Returns the specification of a period.
|
||||||
|
@ -36,8 +36,12 @@ class Period:
|
|||||||
spec (str): The current period specification
|
spec (str): The current period specification
|
||||||
data_start (datetime.date): The available first day of the data.
|
data_start (datetime.date): The available first day of the data.
|
||||||
data_end (datetime.date): The available last day of the data.
|
data_end (datetime.date): The available last day of the data.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: When the period specification is invalid.
|
||||||
"""
|
"""
|
||||||
def __init__(self, spec=None, data_start=None, data_end=None):
|
def __init__(self, spec=None, data_start=None, data_end=None):
|
||||||
|
# Raises ValueError
|
||||||
self._period = self.Parser(spec)
|
self._period = self.Parser(spec)
|
||||||
self._data_start = data_start
|
self._data_start = data_start
|
||||||
self._data_end = data_end
|
self._data_end = data_end
|
||||||
@ -407,6 +411,9 @@ class Period:
|
|||||||
Args:
|
Args:
|
||||||
spec (str|None): The period specification.
|
spec (str|None): The period specification.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
ValueError: When the period specification is invalid.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
spec (str): The currently-using period specification.
|
spec (str): The currently-using period specification.
|
||||||
start (datetime.date): The start of the period.
|
start (datetime.date): The start of the period.
|
||||||
@ -433,11 +440,8 @@ class Period:
|
|||||||
if m is not None:
|
if m is not None:
|
||||||
year = int(m.group(1))
|
year = int(m.group(1))
|
||||||
month = int(m.group(2))
|
month = int(m.group(2))
|
||||||
try:
|
# Raises ValueError
|
||||||
self.start = datetime.date(year, month, 1)
|
self.start = datetime.date(year, month, 1)
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.end = self._month_last_day(self.start)
|
self.end = self._month_last_day(self.start)
|
||||||
self.description = self._month_text(year, month)
|
self.description = self._month_text(year, month)
|
||||||
return
|
return
|
||||||
@ -446,11 +450,8 @@ class Period:
|
|||||||
if m is not None:
|
if m is not None:
|
||||||
year = int(m.group(1))
|
year = int(m.group(1))
|
||||||
month = int(m.group(2))
|
month = int(m.group(2))
|
||||||
try:
|
# Raises ValueError
|
||||||
self.start = datetime.date(year, month, 1)
|
self.start = datetime.date(year, month, 1)
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.end = self._month_last_day(timezone.localdate())
|
self.end = self._month_last_day(timezone.localdate())
|
||||||
self.description = gettext("Since %s")\
|
self.description = gettext("Since %s")\
|
||||||
% self._month_text(year, month)
|
% self._month_text(year, month)
|
||||||
@ -460,11 +461,8 @@ class Period:
|
|||||||
if m is not None:
|
if m is not None:
|
||||||
year = int(m.group(1))
|
year = int(m.group(1))
|
||||||
month = int(m.group(2))
|
month = int(m.group(2))
|
||||||
try:
|
# Raises ValueError
|
||||||
until_month = datetime.date(year, month, 1)
|
until_month = datetime.date(year, month, 1)
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.start = Period.Parser.VERY_START
|
self.start = Period.Parser.VERY_START
|
||||||
self.end = self._month_last_day(until_month)
|
self.end = self._month_last_day(until_month)
|
||||||
self.description = gettext("Until %s")\
|
self.description = gettext("Until %s")\
|
||||||
@ -474,11 +472,8 @@ class Period:
|
|||||||
m = re.match("^([0-9]{4})$", spec)
|
m = re.match("^([0-9]{4})$", spec)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
year = int(m.group(1))
|
year = int(m.group(1))
|
||||||
try:
|
# Raises ValueError
|
||||||
self.start = datetime.date(year, 1, 1)
|
self.start = datetime.date(year, 1, 1)
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.end = datetime.date(year, 12, 31)
|
self.end = datetime.date(year, 12, 31)
|
||||||
self.description = self._year_text(year)
|
self.description = self._year_text(year)
|
||||||
return
|
return
|
||||||
@ -486,11 +481,8 @@ class Period:
|
|||||||
m = re.match("^-([0-9]{4})$", spec)
|
m = re.match("^-([0-9]{4})$", spec)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
year = int(m.group(1))
|
year = int(m.group(1))
|
||||||
try:
|
# Raises ValueError
|
||||||
self.end = datetime.date(year, 12, 31)
|
self.end = datetime.date(year, 12, 31)
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.start = Period.Parser.VERY_START
|
self.start = Period.Parser.VERY_START
|
||||||
self.description = gettext("Until %s")\
|
self.description = gettext("Until %s")\
|
||||||
% self._year_text(year)
|
% self._year_text(year)
|
||||||
@ -505,14 +497,11 @@ class Period:
|
|||||||
m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
|
m = re.match("^([0-9]{4})-([0-9]{2})-([0-9]{2})$",
|
||||||
spec)
|
spec)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
try:
|
# Raises ValueError
|
||||||
self.start = datetime.date(
|
self.start = datetime.date(
|
||||||
int(m.group(1)),
|
int(m.group(1)),
|
||||||
int(m.group(2)),
|
int(m.group(2)),
|
||||||
int(m.group(3)))
|
int(m.group(3)))
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.end = self.start
|
self.end = self.start
|
||||||
self.description = self._date_text(self.start)
|
self.description = self._date_text(self.start)
|
||||||
return
|
return
|
||||||
@ -521,7 +510,7 @@ class Period:
|
|||||||
"-([0-9]{4})-([0-9]{2})-([0-9]{2})$"),
|
"-([0-9]{4})-([0-9]{2})-([0-9]{2})$"),
|
||||||
spec)
|
spec)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
try:
|
# Raises ValueError
|
||||||
self.start = datetime.date(
|
self.start = datetime.date(
|
||||||
int(m.group(1)),
|
int(m.group(1)),
|
||||||
int(m.group(2)),
|
int(m.group(2)),
|
||||||
@ -530,9 +519,6 @@ class Period:
|
|||||||
int(m.group(4)),
|
int(m.group(4)),
|
||||||
int(m.group(5)),
|
int(m.group(5)),
|
||||||
int(m.group(6)))
|
int(m.group(6)))
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
today = timezone.localdate()
|
today = timezone.localdate()
|
||||||
# Spans several years
|
# Spans several years
|
||||||
if self.start.year != self.end.year:
|
if self.start.year != self.end.year:
|
||||||
@ -567,20 +553,17 @@ class Period:
|
|||||||
# Until a specific day
|
# Until a specific day
|
||||||
m = re.match("^-([0-9]{4})-([0-9]{2})-([0-9]{2})$", spec)
|
m = re.match("^-([0-9]{4})-([0-9]{2})-([0-9]{2})$", spec)
|
||||||
if m is not None:
|
if m is not None:
|
||||||
try:
|
# Raises ValueError
|
||||||
self.end = datetime.date(
|
self.end = datetime.date(
|
||||||
int(m.group(1)),
|
int(m.group(1)),
|
||||||
int(m.group(2)),
|
int(m.group(2)),
|
||||||
int(m.group(3)))
|
int(m.group(3)))
|
||||||
except ValueError:
|
|
||||||
self.invalid_period()
|
|
||||||
return
|
|
||||||
self.start = Period.Parser.VERY_START
|
self.start = Period.Parser.VERY_START
|
||||||
self.description = gettext("Until %s")\
|
self.description = gettext("Until %s")\
|
||||||
% self._date_text(self.end)
|
% self._date_text(self.end)
|
||||||
return
|
return
|
||||||
# Wrong period format
|
# Wrong period format
|
||||||
self.invalid_period()
|
raise ValueError
|
||||||
|
|
||||||
def set_this_month(self):
|
def set_this_month(self):
|
||||||
"""Sets the period to this month."""
|
"""Sets the period to this month."""
|
||||||
@ -590,13 +573,6 @@ class Period:
|
|||||||
self.end = self._month_last_day(self.start)
|
self.end = self._month_last_day(self.start)
|
||||||
self.description = gettext("This Month")
|
self.description = gettext("This Month")
|
||||||
|
|
||||||
def invalid_period(self):
|
|
||||||
"""Sets the period when the period specification is
|
|
||||||
invalid.
|
|
||||||
"""
|
|
||||||
self.error = gettext("Invalid period.")
|
|
||||||
self.set_this_month()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _month_last_day(day):
|
def _month_last_day(day):
|
||||||
"""Calculates and returns the last day of a month.
|
"""Calculates and returns the last day of a month.
|
||||||
|
Loading…
Reference in New Issue
Block a user