From bc792c145fe16a603094af626a01b03bf4fbc6b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Thu, 9 Mar 2023 19:40:34 +0800 Subject: [PATCH] Replaced the Period.get_instance method with the get_period function in the "accounting.report.period.parser" module. Changed the parse_spec function in the "accounting.report.period.parser" to private. --- src/accounting/report/converters.py | 4 +-- src/accounting/report/period/__init__.py | 1 + src/accounting/report/period/parser.py | 33 ++++++++++++++++++- src/accounting/report/period/period.py | 28 ---------------- src/accounting/report/utils/report_chooser.py | 5 ++- src/accounting/report/views.py | 14 ++++---- 6 files changed, 44 insertions(+), 41 deletions(-) diff --git a/src/accounting/report/converters.py b/src/accounting/report/converters.py index d455097..8611bc6 100644 --- a/src/accounting/report/converters.py +++ b/src/accounting/report/converters.py @@ -23,7 +23,7 @@ from flask import abort from werkzeug.routing import BaseConverter from accounting.models import Account -from accounting.report.period import Period +from .period import Period, get_period from .utils.income_expense_account import IncomeExpensesAccount @@ -38,7 +38,7 @@ class PeriodConverter(BaseConverter): :return: The corresponding period. """ try: - return Period.get_instance(value) + return get_period(value) except ValueError: abort(404) diff --git a/src/accounting/report/period/__init__.py b/src/accounting/report/period/__init__.py index 54234cd..f8a4708 100644 --- a/src/accounting/report/period/__init__.py +++ b/src/accounting/report/period/__init__.py @@ -18,4 +18,5 @@ """ from .chooser import PeriodChooser +from .parser import get_period from .period import Period diff --git a/src/accounting/report/period/parser.py b/src/accounting/report/period/parser.py index d870228..0c23867 100644 --- a/src/accounting/report/period/parser.py +++ b/src/accounting/report/period/parser.py @@ -20,12 +20,43 @@ import calendar import re from datetime import date +import typing as t + +from .period import Period, ThisMonth, LastMonth, SinceLastMonth, ThisYear, \ + LastYear, Today, Yesterday, AllTime DATE_SPEC_RE: str = r"(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?" """The regular expression of a date specification.""" -def parse_spec(text: str) -> tuple[date | None, date | None]: +def get_period(spec: str | None = None) -> Period: + """Returns a period instance. + + :param spec: The period specification, or omit for the default. + :return: The period instance. + :raise ValueError: When the period specification is invalid. + """ + if spec is None: + return ThisMonth() + named_periods: dict[str, t.Type[t.Callable[[], Period]]] = { + "this-month": lambda: ThisMonth(), + "last-month": lambda: LastMonth(), + "since-last-month": lambda: SinceLastMonth(), + "this-year": lambda: ThisYear(), + "last-year": lambda: LastYear(), + "today": lambda: Today(), + "yesterday": lambda: Yesterday(), + "all-time": lambda: AllTime(), + } + if spec in named_periods: + return named_periods[spec]() + start, end = __parse_spec(spec) + if start is not None and end is not None and start > end: + raise ValueError + return Period(start, end) + + +def __parse_spec(text: str) -> tuple[date | None, date | None]: """Parses the period specification. :param text: The period specification. diff --git a/src/accounting/report/period/period.py b/src/accounting/report/period/period.py index f0b9e72..ed6f23e 100644 --- a/src/accounting/report/period/period.py +++ b/src/accounting/report/period/period.py @@ -26,7 +26,6 @@ from datetime import date, timedelta from accounting.locale import gettext from .description import get_desc -from .parser import parse_spec from .specification import get_spec @@ -101,33 +100,6 @@ class Period: and self.end == date(self.start.year, 12, 31) self.is_a_day = self.start == self.end - @classmethod - def get_instance(cls, spec: str | None = None) -> t.Self: - """Returns a period instance. - - :param spec: The period specification, or omit for the default. - :return: The period instance. - :raise ValueError: When the period is invalid. - """ - if spec is None: - return ThisMonth() - named_periods: dict[str, t.Type[t.Callable[[], Period]]] = { - "this-month": lambda: ThisMonth(), - "last-month": lambda: LastMonth(), - "since-last-month": lambda: SinceLastMonth(), - "this-year": lambda: ThisYear(), - "last-year": lambda: LastYear(), - "today": lambda: Today(), - "yesterday": lambda: Yesterday(), - "all-time": lambda: AllTime(), - } - if spec in named_periods: - return named_periods[spec]() - start, end = parse_spec(spec) - if start is not None and end is not None and start > end: - raise ValueError - return cls(start, end) - def is_year(self, year: int) -> bool: """Returns whether the period is the specific year period. diff --git a/src/accounting/report/utils/report_chooser.py b/src/accounting/report/utils/report_chooser.py index 1200048..46172dd 100644 --- a/src/accounting/report/utils/report_chooser.py +++ b/src/accounting/report/utils/report_chooser.py @@ -28,7 +28,7 @@ from flask_babel import LazyString from accounting import db from accounting.locale import gettext from accounting.models import Currency, Account -from accounting.report.period import Period +from accounting.report.period import Period, get_period from accounting.template_globals import default_currency_code from .income_expense_account import IncomeExpensesAccount from .option_link import OptionLink @@ -53,8 +53,7 @@ class ReportChooser: """ self.__active_report: ReportType = active_report """The currently active report.""" - self.__period: Period = Period.get_instance() if period is None \ - else period + self.__period: Period = get_period() if period is None else period """The period.""" self.__currency: Currency = db.session.get( Currency, default_currency_code()) \ diff --git a/src/accounting/report/views.py b/src/accounting/report/views.py index 7e20be2..2bac806 100644 --- a/src/accounting/report/views.py +++ b/src/accounting/report/views.py @@ -20,7 +20,7 @@ from flask import Blueprint, request, Response from accounting.models import Currency, Account -from accounting.report.period import Period +from accounting.report.period import Period, get_period from accounting.utils.permission import has_permission, can_view from .reports import Journal, Ledger, IncomeExpenses, TrialBalance, \ IncomeStatement, BalanceSheet, Search @@ -39,7 +39,7 @@ def get_default_journal_list() -> str | Response: :return: The journal in the default period. """ - return __get_journal_list(Period.get_instance()) + return __get_journal_list(get_period()) @bp.get("journal/", endpoint="journal") @@ -76,7 +76,7 @@ def get_default_ledger_list(currency: Currency, account: Account) \ :param account: The account. :return: The ledger in the default period. """ - return __get_ledger_list(currency, account, Period.get_instance()) + return __get_ledger_list(currency, account, get_period()) @bp.get("ledger///", @@ -121,7 +121,7 @@ def get_default_income_expenses_list(currency: Currency, :param account: The account. :return: The income and expenses log in the default period. """ - return __get_income_expenses_list(currency, account, Period.get_instance()) + return __get_income_expenses_list(currency, account, get_period()) @bp.get( @@ -166,7 +166,7 @@ def get_default_trial_balance_list(currency: Currency) -> str | Response: :param currency: The currency. :return: The trial balance in the default period. """ - return __get_trial_balance_list(currency, Period.get_instance()) + return __get_trial_balance_list(currency, get_period()) @bp.get("trial-balance//", @@ -206,7 +206,7 @@ def get_default_income_statement_list(currency: Currency) -> str | Response: :param currency: The currency. :return: The income statement in the default period. """ - return __get_income_statement_list(currency, Period.get_instance()) + return __get_income_statement_list(currency, get_period()) @bp.get("income-statement//", @@ -246,7 +246,7 @@ def get_default_balance_sheet_list(currency: Currency) -> str | Response: :param currency: The currency. :return: The balance sheet in the default period. """ - return __get_balance_sheet_list(currency, Period.get_instance()) + return __get_balance_sheet_list(currency, get_period()) @bp.get("balance-sheet//",