Changed the PeriodChooser utility from abstract to real, and replaced the various trivial subclasses with the get_url callable as the parameter.

This commit is contained in:
依瑪貓 2023-03-09 17:43:21 +08:00
parent 9635448f18
commit 1fd50e23d9
7 changed files with 40 additions and 121 deletions

View File

@ -31,7 +31,7 @@ from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import BalanceSheetPeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import ledger_url, balance_sheet_url, income_statement_url from .utils.urls import ledger_url, balance_sheet_url, income_statement_url
@ -317,8 +317,8 @@ class PageParams(BasePageParams):
"""The liabilities.""" """The liabilities."""
self.owner_s_equity: Section = owner_s_equity self.owner_s_equity: Section = owner_s_equity
"""The owner's equity.""" """The owner's equity."""
self.period_chooser: BalanceSheetPeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= BalanceSheetPeriodChooser(currency) lambda x: balance_sheet_url(currency, x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -34,7 +34,7 @@ from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import IncomeExpensesPeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import income_expenses_url from .utils.urls import income_expenses_url
@ -289,8 +289,8 @@ class PageParams(BasePageParams):
"""The report entries.""" """The report entries."""
self.total: ReportEntry | None = total self.total: ReportEntry | None = total
"""The total entry.""" """The total entry."""
self.period_chooser: IncomeExpensesPeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= IncomeExpensesPeriodChooser(currency, account) lambda x: income_expenses_url(currency, account, x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -31,7 +31,7 @@ from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import IncomeStatementPeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import ledger_url, income_statement_url from .utils.urls import ledger_url, income_statement_url
@ -160,8 +160,8 @@ class PageParams(BasePageParams):
"""True if there is any data, or False otherwise.""" """True if there is any data, or False otherwise."""
self.sections: list[Section] = sections self.sections: list[Section] = sections
"""The sections in the income statement.""" """The sections in the income statement."""
self.period_chooser: IncomeStatementPeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= IncomeStatementPeriodChooser(currency) lambda x: income_statement_url(currency, x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -31,9 +31,10 @@ from accounting.utils.pagination import Pagination
from .utils.base_page_params import BasePageParams from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.period_choosers import JournalPeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import journal_url
class ReportEntry: class ReportEntry:
@ -122,8 +123,8 @@ class PageParams(BasePageParams):
"""The pagination.""" """The pagination."""
self.entries: list[JournalEntry] = entries self.entries: list[JournalEntry] = entries
"""The entries.""" """The entries."""
self.period_chooser: JournalPeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= JournalPeriodChooser() lambda x: journal_url(x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -33,7 +33,7 @@ from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import LedgerPeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import ledger_url from .utils.urls import ledger_url
@ -264,8 +264,8 @@ class PageParams(BasePageParams):
"""The entries.""" """The entries."""
self.total: ReportEntry | None = total self.total: ReportEntry | None = total
"""The total entry.""" """The total entry."""
self.period_chooser: LedgerPeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= LedgerPeriodChooser(currency, account) lambda x: ledger_url(currency, account, x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -30,7 +30,7 @@ from .utils.base_page_params import BasePageParams
from .utils.base_report import BaseReport from .utils.base_report import BaseReport
from .utils.csv_export import BaseCSVRow, csv_download, period_spec from .utils.csv_export import BaseCSVRow, csv_download, period_spec
from .utils.option_link import OptionLink from .utils.option_link import OptionLink
from .utils.period_choosers import TrialBalancePeriodChooser from .utils.period_choosers import PeriodChooser
from .utils.report_chooser import ReportChooser from .utils.report_chooser import ReportChooser
from .utils.report_type import ReportType from .utils.report_type import ReportType
from .utils.urls import ledger_url, trial_balance_url from .utils.urls import ledger_url, trial_balance_url
@ -121,8 +121,8 @@ class PageParams(BasePageParams):
"""The accounts in the trial balance.""" """The accounts in the trial balance."""
self.total: Total = total self.total: Total = total
"""The total of the trial balance.""" """The total of the trial balance."""
self.period_chooser: TrialBalancePeriodChooser \ self.period_chooser: PeriodChooser = PeriodChooser(
= TrialBalancePeriodChooser(currency) lambda x: trial_balance_url(currency, x))
"""The period chooser.""" """The period chooser."""
@property @property

View File

@ -20,42 +20,45 @@ This file is largely taken from the NanoParma ERP project, first written in
2021/9/16 by imacat (imacat@nanoparma.com). 2021/9/16 by imacat (imacat@nanoparma.com).
""" """
from abc import ABC, abstractmethod import typing as t
from datetime import date from datetime import date
from accounting.models import Currency, Account, Transaction from accounting.models import Transaction
from accounting.report.income_expense_account import IncomeExpensesAccount
from accounting.report.period import YearPeriod, Period, ThisMonth, \ from accounting.report.period import YearPeriod, Period, ThisMonth, \
LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \ LastMonth, SinceLastMonth, ThisYear, LastYear, Today, Yesterday, \
TemplatePeriod TemplatePeriod
from .urls import journal_url, ledger_url, income_expenses_url, \
trial_balance_url, income_statement_url, balance_sheet_url
class PeriodChooser(ABC): class PeriodChooser:
"""The period chooser.""" """The period chooser."""
def __init__(self): def __init__(self, get_url: t.Callable[[Period], str]):
"""Constructs a period chooser.""" """Constructs a period chooser.
:param get_url: The callback to return the URL of the current report in
a period.
"""
self.__get_url: t.Callable[[Period], str] = get_url
"""The callback to return the URL of the current report in a period."""
# Shortcut periods # Shortcut periods
self.this_month_url: str = self._url_for(ThisMonth()) self.this_month_url: str = get_url(ThisMonth())
"""The URL for this month.""" """The URL for this month."""
self.last_month_url: str = self._url_for(LastMonth()) self.last_month_url: str = get_url(LastMonth())
"""The URL for last month.""" """The URL for last month."""
self.since_last_month_url: str = self._url_for(SinceLastMonth()) self.since_last_month_url: str = get_url(SinceLastMonth())
"""The URL since last mint.""" """The URL since last mint."""
self.this_year_url: str = self._url_for(ThisYear()) self.this_year_url: str = get_url(ThisYear())
"""The URL for this year.""" """The URL for this year."""
self.last_year_url: str = self._url_for(LastYear()) self.last_year_url: str = get_url(LastYear())
"""The URL for last year.""" """The URL for last year."""
self.today_url: str = self._url_for(Today()) self.today_url: str = get_url(Today())
"""The URL for today.""" """The URL for today."""
self.yesterday_url: str = self._url_for(Yesterday()) self.yesterday_url: str = get_url(Yesterday())
"""The URL for yesterday.""" """The URL for yesterday."""
self.all_url: str = self._url_for(Period(None, None)) self.all_url: str = get_url(Period(None, None))
"""The URL for all period.""" """The URL for all period."""
self.url_template: str = self._url_for(TemplatePeriod()) self.url_template: str = get_url(TemplatePeriod())
"""The URL template.""" """The URL template."""
first: Transaction | None \ first: Transaction | None \
@ -85,95 +88,10 @@ class PeriodChooser(ABC):
self.available_years \ self.available_years \
= reversed(range(start.year, today.year - 1)) = reversed(range(start.year, today.year - 1))
@abstractmethod
def _url_for(self, period: Period) -> str:
"""Returns the URL for a period.
:param period: The period.
:return: The URL for the period.
"""
pass
def year_url(self, year: int) -> str: def year_url(self, year: int) -> str:
"""Returns the period URL of a year. """Returns the period URL of a year.
:param year: The year :param year: The year
:return: The period URL of the year. :return: The period URL of the year.
""" """
return self._url_for(YearPeriod(year)) return self.__get_url(YearPeriod(year))
class JournalPeriodChooser(PeriodChooser):
"""The journal period chooser."""
def _url_for(self, period: Period) -> str:
return journal_url(period)
class LedgerPeriodChooser(PeriodChooser):
"""The ledger period chooser."""
def __init__(self, currency: Currency, account: Account):
"""Constructs the ledger period chooser."""
self.currency: Currency = currency
"""The currency."""
self.account: Account = account
"""The account."""
super().__init__()
def _url_for(self, period: Period) -> str:
return ledger_url(self.currency, self.account, period)
class IncomeExpensesPeriodChooser(PeriodChooser):
"""The income and expenses log period chooser."""
def __init__(self, currency: Currency, account: IncomeExpensesAccount):
"""Constructs the income and expenses log period chooser."""
self.currency: Currency = currency
"""The currency."""
self.account: IncomeExpensesAccount = account
"""The account."""
super().__init__()
def _url_for(self, period: Period) -> str:
return income_expenses_url(self.currency, self.account, period)
class TrialBalancePeriodChooser(PeriodChooser):
"""The trial balance period chooser."""
def __init__(self, currency: Currency):
"""Constructs the trial balance period chooser."""
self.currency: Currency = currency
"""The currency."""
super().__init__()
def _url_for(self, period: Period) -> str:
return trial_balance_url(self.currency, period)
class IncomeStatementPeriodChooser(PeriodChooser):
"""The income statement period chooser."""
def __init__(self, currency: Currency):
"""Constructs the income statement period chooser."""
self.currency: Currency = currency
"""The currency."""
super().__init__()
def _url_for(self, period: Period) -> str:
return income_statement_url(self.currency, period)
class BalanceSheetPeriodChooser(PeriodChooser):
"""The balance sheet period chooser."""
def __init__(self, currency: Currency):
"""Constructs the balance sheet period chooser."""
self.currency: Currency = currency
"""The currency."""
super().__init__()
def _url_for(self, period: Period) -> str:
return balance_sheet_url(self.currency, period)