Revised the way to import from the datetime package, to avoid name conflict with the common "date" and "time" names.

This commit is contained in:
2023-04-26 13:17:31 +08:00
parent c11ae23885
commit 359c335662
30 changed files with 156 additions and 152 deletions

View File

@ -20,8 +20,8 @@ This file is largely taken from the NanoParma ERP project, first written in
2021/9/16 by imacat (imacat@nanoparma.com).
"""
import datetime as dt
import typing as t
from datetime import date
from accounting.models import JournalEntry
from .period import Period
@ -63,10 +63,10 @@ class PeriodChooser:
first: JournalEntry | None \
= JournalEntry.query.order_by(JournalEntry.date).first()
start: date | None = None if first is None else first.date
start: dt.date | None = None if first is None else first.date
# Attributes
self.data_start: date | None = start
self.data_start: dt.date | None = start
"""The start of the data."""
self.has_data: bool = start is not None
"""Whether there is any data."""
@ -80,8 +80,8 @@ class PeriodChooser:
"""The available years."""
if self.has_data:
today: date = date.today()
self.has_last_month = start < date(today.year, today.month, 1)
today: dt.date = dt.date.today()
self.has_last_month = start < dt.date(today.year, today.month, 1)
self.has_last_year = start.year < today.year
self.has_yesterday = start < today
if start.year < today.year - 1:

View File

@ -17,12 +17,12 @@
"""The period description composer.
"""
from datetime import date, timedelta
import datetime as dt
from accounting.locale import gettext
def get_desc(start: date | None, end: date | None) -> str:
def get_desc(start: dt.date | None, end: dt.date | None) -> str:
"""Returns the period description.
:param start: The start of the period.
@ -46,7 +46,7 @@ def get_desc(start: date | None, end: date | None) -> str:
return __get_day_desc(start, end)
def __get_since_desc(start: date) -> str:
def __get_since_desc(start: dt.date) -> str:
"""Returns the description without the end day.
:param start: The start of the period.
@ -67,7 +67,7 @@ def __get_since_desc(start: date) -> str:
return gettext("since %(start)s", start=get_start_desc())
def __get_until_desc(end: date) -> str:
def __get_until_desc(end: dt.date) -> str:
"""Returns the description without the start day.
:param end: The end of the period.
@ -81,14 +81,14 @@ def __get_until_desc(end: date) -> str:
"""
if end.month == 12 and end.day == 31:
return str(end.year)
if (end + timedelta(days=1)).day == 1:
if (end + dt.timedelta(days=1)).day == 1:
return __format_month(end)
return __format_day(end)
return gettext("until %(end)s", end=get_end_desc())
def __get_year_desc(start: date, end: date) -> str:
def __get_year_desc(start: dt.date, end: dt.date) -> str:
"""Returns the description as a year range.
:param start: The start of the period.
@ -105,7 +105,7 @@ def __get_year_desc(start: date, end: date) -> str:
return __get_from_to_desc(start_text, str(end.year))
def __get_month_desc(start: date, end: date) -> str:
def __get_month_desc(start: dt.date, end: dt.date) -> str:
"""Returns the description as a month range.
:param start: The start of the period.
@ -113,7 +113,7 @@ def __get_month_desc(start: date, end: date) -> str:
:return: The description as a month range.
:raise ValueError: The period is not a month range.
"""
if start.day != 1 or (end + timedelta(days=1)).day != 1:
if start.day != 1 or (end + dt.timedelta(days=1)).day != 1:
raise ValueError
start_text: str = __format_month(start)
if start.year == end.year and start.month == end.month:
@ -123,7 +123,7 @@ def __get_month_desc(start: date, end: date) -> str:
return __get_from_to_desc(start_text, __format_month(end))
def __get_day_desc(start: date, end: date) -> str:
def __get_day_desc(start: dt.date, end: dt.date) -> str:
"""Returns the description as a day range.
:param start: The start of the period.
@ -142,7 +142,7 @@ def __get_day_desc(start: date, end: date) -> str:
return __get_from_to_desc(start_text, __format_day(end))
def __format_month(month: date) -> str:
def __format_month(month: dt.date) -> str:
"""Formats a month.
:param month: The month.
@ -151,7 +151,7 @@ def __format_month(month: date) -> str:
return f"{month.year}/{month.month}"
def __format_day(day: date) -> str:
def __format_day(day: dt.date) -> str:
"""Formats a day.
:param day: The day.

View File

@ -18,14 +18,14 @@
"""
import calendar
from datetime import date
import datetime as dt
def month_end(day: date) -> date:
def month_end(day: dt.date) -> dt.date:
"""Returns the end day of month for a date.
:param day: The date.
:return: The end day of the month of that day.
"""
last_day: int = calendar.monthrange(day.year, day.month)[1]
return date(day.year, day.month, last_day)
return dt.date(day.year, day.month, last_day)

View File

@ -18,9 +18,9 @@
"""
import calendar
import datetime as dt
import re
import typing as t
from datetime import date
from .period import Period
from .shortcuts import ThisMonth, LastMonth, SinceLastMonth, ThisYear, \
@ -57,7 +57,7 @@ def get_period(spec: str | None = None) -> Period:
return Period(start, end)
def __parse_spec(text: str) -> tuple[date | None, date | None]:
def __parse_spec(text: str) -> tuple[dt.date | None, dt.date | None]:
"""Parses the period specification.
:param text: The period specification.
@ -84,7 +84,7 @@ def __parse_spec(text: str) -> tuple[date | None, date | None]:
raise ValueError
def __get_start(year: str, month: str | None, day: str | None) -> date:
def __get_start(year: str, month: str | None, day: str | None) -> dt.date:
"""Returns the start of the period from the date representation.
:param year: The year.
@ -94,13 +94,13 @@ def __get_start(year: str, month: str | None, day: str | None) -> date:
:raise ValueError: When the date is invalid.
"""
if day is not None:
return date(int(year), int(month), int(day))
return dt.date(int(year), int(month), int(day))
if month is not None:
return date(int(year), int(month), 1)
return date(int(year), 1, 1)
return dt.date(int(year), int(month), 1)
return dt.date(int(year), 1, 1)
def __get_end(year: str, month: str | None, day: str | None) -> date:
def __get_end(year: str, month: str | None, day: str | None) -> dt.date:
"""Returns the end of the period from the date representation.
:param year: The year.
@ -110,10 +110,10 @@ def __get_end(year: str, month: str | None, day: str | None) -> date:
:raise ValueError: When the date is invalid.
"""
if day is not None:
return date(int(year), int(month), int(day))
return dt.date(int(year), int(month), int(day))
if month is not None:
year_n: int = int(year)
month_n: int = int(month)
day_n: int = calendar.monthrange(year_n, month_n)[1]
return date(year_n, month_n, day_n)
return date(int(year), 12, 31)
return dt.date(year_n, month_n, day_n)
return dt.date(int(year), 12, 31)

View File

@ -20,8 +20,8 @@ This file is largely taken from the NanoParma ERP project, first written in
2021/9/16 by imacat (imacat@nanoparma.com).
"""
import datetime as dt
import typing as t
from datetime import date, timedelta
from .description import get_desc
from .month_end import month_end
@ -31,15 +31,15 @@ from .specification import get_spec
class Period:
"""A date period."""
def __init__(self, start: date | None, end: date | None):
def __init__(self, start: dt.date | None, end: dt.date | None):
"""Constructs a new date period.
:param start: The start date, or None from the very beginning.
:param end: The end date, or None till no end.
"""
self.start: date | None = start
self.start: dt.date | None = start
"""The start of the period."""
self.end: date | None = end
self.end: dt.date | None = end
"""The end of the period."""
self.is_default: bool = False
"""Whether the is the default period."""
@ -95,8 +95,8 @@ class Period:
self.is_a_month = self.start.day == 1 \
and self.end == month_end(self.start)
self.is_type_month = self.is_a_month
self.is_a_year = self.start == date(self.start.year, 1, 1) \
and self.end == date(self.start.year, 12, 31)
self.is_a_year = self.start == dt.date(self.start.year, 1, 1) \
and self.end == dt.date(self.start.year, 12, 31)
self.is_a_day = self.start == self.end
def is_year(self, year: int) -> bool:
@ -126,4 +126,4 @@ class Period:
"""
if self.start is None:
return None
return Period(None, self.start - timedelta(days=1))
return Period(None, self.start - dt.timedelta(days=1))

View File

@ -17,7 +17,7 @@
"""The named shortcut periods.
"""
from datetime import date, timedelta
import datetime as dt
from accounting.locale import gettext
from .month_end import month_end
@ -27,8 +27,8 @@ from .period import Period
class ThisMonth(Period):
"""The period of this month."""
def __init__(self):
today: date = date.today()
this_month_start: date = date(today.year, today.month, 1)
today: dt.date = dt.date.today()
this_month_start: dt.date = dt.date(today.year, today.month, 1)
super().__init__(this_month_start, month_end(today))
self.is_default = True
self.is_this_month = True
@ -43,13 +43,13 @@ class ThisMonth(Period):
class LastMonth(Period):
"""The period of this month."""
def __init__(self):
today: date = date.today()
today: dt.date = dt.date.today()
year: int = today.year
month: int = today.month - 1
if month < 1:
year = year - 1
month = 12
start: date = date(year, month, 1)
start: dt.date = dt.date(year, month, 1)
super().__init__(start, month_end(start))
self.is_last_month = True
@ -63,13 +63,13 @@ class LastMonth(Period):
class SinceLastMonth(Period):
"""The period of this month."""
def __init__(self):
today: date = date.today()
today: dt.date = dt.date.today()
year: int = today.year
month: int = today.month - 1
if month < 1:
year = year - 1
month = 12
start: date = date(year, month, 1)
start: dt.date = dt.date(year, month, 1)
super().__init__(start, None)
self.is_since_last_month = True
@ -82,9 +82,9 @@ class SinceLastMonth(Period):
class ThisYear(Period):
"""The period of this year."""
def __init__(self):
year: int = date.today().year
start: date = date(year, 1, 1)
end: date = date(year, 12, 31)
year: int = dt.date.today().year
start: dt.date = dt.date(year, 1, 1)
end: dt.date = dt.date(year, 12, 31)
super().__init__(start, end)
self.is_this_year = True
@ -97,9 +97,9 @@ class ThisYear(Period):
class LastYear(Period):
"""The period of last year."""
def __init__(self):
year: int = date.today().year
start: date = date(year - 1, 1, 1)
end: date = date(year - 1, 12, 31)
year: int = dt.date.today().year
start: dt.date = dt.date(year - 1, 1, 1)
end: dt.date = dt.date(year - 1, 12, 31)
super().__init__(start, end)
self.is_last_year = True
@ -112,7 +112,7 @@ class LastYear(Period):
class Today(Period):
"""The period of today."""
def __init__(self):
today: date = date.today()
today: dt.date = dt.date.today()
super().__init__(today, today)
self.is_today = True
@ -125,7 +125,7 @@ class Today(Period):
class Yesterday(Period):
"""The period of yesterday."""
def __init__(self):
yesterday: date = date.today() - timedelta(days=1)
yesterday: dt.date = dt.date.today() - dt.timedelta(days=1)
super().__init__(yesterday, yesterday)
self.is_yesterday = True
@ -163,6 +163,6 @@ class YearPeriod(Period):
:param year: The year.
"""
start: date = date(year, 1, 1)
end: date = date(year, 12, 31)
start: dt.date = dt.date(year, 1, 1)
end: dt.date = dt.date(year, 12, 31)
super().__init__(start, end)

View File

@ -17,10 +17,10 @@
"""The period specification composer.
"""
from datetime import date, timedelta
import datetime as dt
def get_spec(start: date | None, end: date | None) -> str:
def get_spec(start: dt.date | None, end: dt.date | None) -> str:
"""Returns the period specification.
:param start: The start of the period.
@ -44,7 +44,7 @@ def get_spec(start: date | None, end: date | None) -> str:
return __get_day_spec(start, end)
def __get_since_spec(start: date) -> str:
def __get_since_spec(start: dt.date) -> str:
"""Returns the period specification without the end day.
:param start: The start of the period.
@ -57,7 +57,7 @@ def __get_since_spec(start: date) -> str:
return start.strftime("%Y-%m-%d-")
def __get_until_spec(end: date) -> str:
def __get_until_spec(end: dt.date) -> str:
"""Returns the period specification without the start day.
:param end: The end of the period.
@ -65,12 +65,12 @@ def __get_until_spec(end: date) -> str:
"""
if end.month == 12 and end.day == 31:
return end.strftime("-%Y")
if (end + timedelta(days=1)).day == 1:
if (end + dt.timedelta(days=1)).day == 1:
return end.strftime("-%Y-%m")
return end.strftime("-%Y-%m-%d")
def __get_year_spec(start: date, end: date) -> str:
def __get_year_spec(start: dt.date, end: dt.date) -> str:
"""Returns the period specification as a year range.
:param start: The start of the period.
@ -88,7 +88,7 @@ def __get_year_spec(start: date, end: date) -> str:
return f"{start_spec}-{end_spec}"
def __get_month_spec(start: date, end: date) -> str:
def __get_month_spec(start: dt.date, end: dt.date) -> str:
"""Returns the period specification as a month range.
:param start: The start of the period.
@ -96,7 +96,7 @@ def __get_month_spec(start: date, end: date) -> str:
:return: The period specification as a month range.
:raise ValueError: The period is not a month range.
"""
if start.day != 1 or (end + timedelta(days=1)).day != 1:
if start.day != 1 or (end + dt.timedelta(days=1)).day != 1:
raise ValueError
start_spec: str = start.strftime("%Y-%m")
if start.year == end.year and start.month == end.month:
@ -105,7 +105,7 @@ def __get_month_spec(start: date, end: date) -> str:
return f"{start_spec}-{end_spec}"
def __get_day_spec(start: date, end: date) -> str:
def __get_day_spec(start: dt.date, end: dt.date) -> str:
"""Returns the period specification as a day range.
:param start: The start of the period.