Revised the PeriodChooser utility to find the start of the data by itself. It can do that. It's child classes are all doing the same thing. There is no need to do that in its child classes.

This commit is contained in:
依瑪貓 2023-03-09 17:20:52 +08:00
parent 5132141c68
commit 3d2e40865e

View File

@ -35,11 +35,8 @@ from .urls import journal_url, ledger_url, income_expenses_url, \
class PeriodChooser(ABC): class PeriodChooser(ABC):
"""The period chooser.""" """The period chooser."""
def __init__(self, start: date | None): def __init__(self):
"""Constructs a period chooser. """Constructs a period chooser."""
:param start: The start of the period.
"""
# Shortcut periods # Shortcut periods
self.this_month_url: str = self._url_for(ThisMonth()) self.this_month_url: str = self._url_for(ThisMonth())
@ -61,6 +58,10 @@ class PeriodChooser(ABC):
self.url_template: str = self._url_for(TemplatePeriod()) self.url_template: str = self._url_for(TemplatePeriod())
"""The URL template.""" """The URL template."""
first: Transaction | None \
= Transaction.query.order_by(Transaction.date).first()
start: date | None = None if first is None else first.date
# Attributes # Attributes
self.data_start: date | None = start self.data_start: date | None = start
"""The start of the data.""" """The start of the data."""
@ -105,12 +106,6 @@ class PeriodChooser(ABC):
class JournalPeriodChooser(PeriodChooser): class JournalPeriodChooser(PeriodChooser):
"""The journal period chooser.""" """The journal period chooser."""
def __init__(self):
"""Constructs the journal period chooser."""
first: Transaction | None \
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return journal_url(period) return journal_url(period)
@ -124,9 +119,7 @@ class LedgerPeriodChooser(PeriodChooser):
"""The currency.""" """The currency."""
self.account: Account = account self.account: Account = account
"""The account.""" """The account."""
first: Transaction | None \ super().__init__()
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return ledger_url(self.currency, self.account, period) return ledger_url(self.currency, self.account, period)
@ -141,9 +134,7 @@ class IncomeExpensesPeriodChooser(PeriodChooser):
"""The currency.""" """The currency."""
self.account: IncomeExpensesAccount = account self.account: IncomeExpensesAccount = account
"""The account.""" """The account."""
first: Transaction | None \ super().__init__()
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return income_expenses_url(self.currency, self.account, period) return income_expenses_url(self.currency, self.account, period)
@ -156,9 +147,7 @@ class TrialBalancePeriodChooser(PeriodChooser):
"""Constructs the trial balance period chooser.""" """Constructs the trial balance period chooser."""
self.currency: Currency = currency self.currency: Currency = currency
"""The currency.""" """The currency."""
first: Transaction | None \ super().__init__()
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return trial_balance_url(self.currency, period) return trial_balance_url(self.currency, period)
@ -171,9 +160,7 @@ class IncomeStatementPeriodChooser(PeriodChooser):
"""Constructs the income statement period chooser.""" """Constructs the income statement period chooser."""
self.currency: Currency = currency self.currency: Currency = currency
"""The currency.""" """The currency."""
first: Transaction | None \ super().__init__()
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return income_statement_url(self.currency, period) return income_statement_url(self.currency, period)
@ -186,9 +173,7 @@ class BalanceSheetPeriodChooser(PeriodChooser):
"""Constructs the balance sheet period chooser.""" """Constructs the balance sheet period chooser."""
self.currency: Currency = currency self.currency: Currency = currency
"""The currency.""" """The currency."""
first: Transaction | None \ super().__init__()
= Transaction.query.order_by(Transaction.date).first()
super().__init__(None if first is None else first.date)
def _url_for(self, period: Period) -> str: def _url_for(self, period: Period) -> str:
return balance_sheet_url(self.currency, period) return balance_sheet_url(self.currency, period)