Added the period_spec function to be used to compose the download file name, to replace the spec property of the Period utility.

This commit is contained in:
2023-03-08 11:55:52 +08:00
parent b0a4a735f3
commit 617dd29f23
7 changed files with 51 additions and 12 deletions

View File

@ -19,11 +19,14 @@
"""
import csv
from abc import ABC, abstractmethod
from datetime import timedelta
from decimal import Decimal
from io import StringIO
from flask import Response
from accounting.report.period import Period
class BaseCSVRow(ABC):
"""The base CSV row."""
@ -52,3 +55,36 @@ def csv_download(filename: str, rows: list[BaseCSVRow]) -> Response:
response.headers["Content-Disposition"] \
= f"attachment; filename={filename}"
return response
def period_spec(period: Period) -> str:
"""Constructs the period specification to be used in the filename.
:param period: The period.
:return: The period specification to be used in the filename.
"""
start: str | None = None
if period.start is not None:
if period.start.month == 1 and period.start.day == 1:
start = str(period.start.year)
elif period.start.day == 1:
start = period.start.strftime("%Y%m")
else:
start = period.start.strftime("%Y%m%d")
end: str | None = None
if period.end is not None:
if period.end.month == 12 and period.end.day == 31:
end = str(period.end.year)
elif (period.end + timedelta(days=1)).day == 1:
end = period.end.strftime("%Y%m")
else:
end = period.end.strftime("%Y%m%d")
if start == end:
return start
if period.start is None and period.end is None:
return "all-time"
if period.start is None:
return f"until-{end}"
if period.end is None:
return f"since-{start}"
return f"{start}-{end}"