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.

This commit is contained in:
依瑪貓 2023-03-09 19:40:34 +08:00
parent 4432484acd
commit bc792c145f
6 changed files with 44 additions and 41 deletions

View File

@ -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)

View File

@ -18,4 +18,5 @@
"""
from .chooser import PeriodChooser
from .parser import get_period
from .period import Period

View File

@ -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.

View File

@ -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.

View File

@ -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()) \

View File

@ -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/<period:period>", 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/<currency:currency>/<account:account>/<period:period>",
@ -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/<currency:currency>/<period:period>",
@ -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/<currency:currency>/<period:period>",
@ -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/<currency:currency>/<period:period>",